Previously, when we referenced entity classes in the SQL mapping XML file, we needed to write the full class name of the entity class (package name + class name), as follows:
<!---<id= "AddUser" parametertype = "Me.gacl.domain.User" > INSERT into users (name,age) values (#{name},#{age})</insert>
Parametertype= "me.gacl.domain.User" Here write the entity class User's full class name Me.gacl.domain.User, each time write such a long list of content is very troublesome, And we want to be able to write the following form
<id= "AddUser2" parametertype= "_user"> Insert into users (name,age) values (#{name},#{age})</insert>
Parametertype= "_User" This is much easier to write, in order to achieve this effect, we need to conf.xml file for the entity class = "Me.gacl.domain.User "Define an alias as"_User", as follows:
Add the following configuration to the <configuration></configuration> tag in the Conf.xml file:
< typealiases > < type= "Me.gacl.domain.User" alias= "_user"/></ typealiases>
This allows you to define an alias of _user for the Me.gacl.domain.User class , and then _user represents the Me.gacl.domain.User class, so that any place in the SQL mapping XML file that needs to refer to the Me.gacl.domain.User class can be replaced with _user , which achieves a simplification of the entity class reference.
In addition to using <typealias type= "Me.gacl.domain.User" alias= "_user"/> This way to set an alias for an entity class individually, We can also set aliases for all entity classes under a package in bulk using the following method:
<!--Configure an alias for an entity class to configure the entity class name so that it can be used instead of entity classes when referencing entity classes, for shorthand purposes - <typealiases> <!--Configure an alias for the entity class Me.gacl.domain.User _user - <!--<typealias type= "Me.gacl.domain.User" alias= "_user"/> - <!--to configure aliases for all entity classes under the Me.gacl.domain package, mybatis The default setting alias is to remove the simple class name of the package where the class is located, such as Me.gacl.domain.User, the alias of the entity class will be set to user - < Packagename= "Me.gacl.domain"/> </typealiases>
<package name= "Me.gacl.domain"/> means setting aliases for all entity classes under this package. MyBatis The default setting alias is to remove the simple class name of the package in which the class is located, such as Me.gacl.domain.User, the alias of the entity class will be set to user.
MyBatis simplifying SQL writing, using aliases