MyBatis alias settings
In a specific mapper.xml file, defining a lot of statement,statement requires parametertype specifying the type of input parameter, the type of mapping that needs to be resulttype to specify the output result.
One. MyBatis Default support aliases
|
Alias |
Types of mappings |
|
_byte |
Byte |
|
_long |
Long |
|
_short |
Short |
|
_int |
Int |
|
_integer |
Int |
|
_double |
Double |
|
_float |
Float |
|
_boolean |
Boolean |
|
String |
String |
|
Byte |
Byte |
|
Long |
Long |
|
Short |
Short |
|
Int |
Integer |
|
Integer |
Integer |
|
Double |
Double |
|
Float |
Float |
|
Boolean |
Boolean |
|
Date |
Date |
|
Decimal |
BigDecimal |
|
BigDecimal |
BigDecimal
|
If you enter a type full path when specifying a type, it is inconvenient to develop, you can define some aliases for the type specified by ParameterType or Resulttype, and the alias definition in mapper.xml to facilitate development.
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
- <!--Create User---
- <insert 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 it is difficult to write such a long string of content, and we want to be able to be written in the following form
- <insert id="AddUser2" parametertype="_user">
- Insert into users (name,age) values (#{name},#{age})
- </Insert>
Parametertype= "_user" is much easier to write, in order to achieve this effect, we need to define an alias "_user" for entity class = "Me.gacl.domain.User" in the Conf.xml file, as follows:
Add the following configuration to the <configuration></configuration> tag in the Conf.xml file:
- <typealiases>
- <typealias 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 later _ The 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 is the purpose of simplifying 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 the entity class when referencing the entity class, for the purpose of shorthand-
- <typealiases>
- <!--Configure an alias for the entity class Me.gacl.domain.User _user--
- <!--<typealias type= "Me.gacl.domain.User" alias= "_user"/> --
- <!--configure aliases for all entity classes under the Me.gacl.domain package, mybatis The default setting alias is to remove the simple class name after the package containing the class
- For example, Me.gacl.domain.User this entity class alias will be set to user
- -->
- <package name="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.
In the springboot we can also set the alias, this is in the configuration file one sentence:
Springboot settings for MyBatis aliases