The typeAliases attribute in MyBatis - aliasing the entity class of the Java type

typeAliases attribute in MyBatis

1. Function

Mybatis's typeAliases function is to alias the entity class of the Java type, which is convenient for use in the mapping file. After configuring the typeAliases attribute, it can be in the mapping file
insert image description here

Two, usage

1. Usage 1: Alias ​​the specified entity class

<!--configuration核心配置文件-->
<configuration>
    <!-- 引入外部配置文件 -->
    <properties resource="db.properties">
        <property name="username" value="root"/>
        <property name="password" value="Wang118821"/>
    </properties>

    <!-- 用于给实体类起别名 -->
    <typeAliases>
        <typeAlias type="com.example.demo.pojo.User" alias="User"/>
    </typeAliases>
    <select id="getUserList" resultType="User">
        select * from mybatis.user
    </select>

Usage 2: Specify a package name, and MyBatis will search for the required Java Bean under the package name, for example: scan the package of the entity class. If there is no annotation, its default alias is the class name of this class, with the first letter lowercase ; If there is an annotation, the alias is its annotation value, see the following example:

<!--configuration核心配置文件-->
<configuration>
    <!-- 引入外部配置文件 -->
    <properties resource="db.properties">
        <property name="username" value="root"/>
        <property name="password" value="Wang118821"/>
    </properties>

    <!-- 用于给实体类起别名 -->
    <typeAliases>
        <package name="com.example.demo.pojo" />
    </typeAliases>
    <select id="getUserList" resultType="user">
        select * from mybatis.user
    </select>

At this point, you can also add annotations to the entity class

//实体类
@Alias("userInfo")
public class User {
    
    
    private Integer id;
    private String name;
    private String pwd;

    <select id="getUserList" resultType="userInfo">
        select * from mybatis.user
    </select>

Guess you like

Origin blog.csdn.net/weixin_43950588/article/details/131266687