mybatis 实体类起别名

提出问题

在xml中配置实体类时,需要写包名+类名,比较繁琐,如下:

<resultMap id="UserMap" type="org.malaoshi.entity.User">
    <id property="id" column="id" ></id>
    <result property="uername" column="uername"/>
    <result property="password" column="password"/>
    <result property="createTime" column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP"/>
</resultMap>
<insert id="save" parameterType="org.malaoshi.entity.User">
    insert into user (id,username,password) values (#{id},#{username},#{password})
</insert>
<select id="selectByUsername" resultType="org.malaoshi.entity.User">
    select id,username,password from user where username like #{username}
</select>

解决

mybatis支持给实体类起别名,在mapper.xml中可以使用别名

其别名有2种方式

方式一(麻烦但灵活)

修改mybatis-config.xml

增加下面代码:

<typeAliases>
    <!-- type指定实体类的包路径+名,alias指定别名(别名不区分大小写) -->
    <typeAlias type="org.malaoshi.entity.User" alias="user"></typeAlias>
</typeAliases>

<typeAlias>标签给一个实体类起别名,属性:

  • type:指定实体类的包路径+名
  • alias:指定别名,别名不区分大小写

完整配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 给实体类起别名 -->
<typeAliases>
    <!-- type指定实体类的包路径+名,alias指定别名(别名不区分大小写) -->
    <typeAlias type="org.malaoshi.entity.User" alias="user"></typeAlias>
</typeAliases>
    <!-- 配置环境 。default名字随意起-->
    <environments default="mysql">
    <!--    id名字必须同上面default名    -->
        <environment id="mysql">
            <!-- 配置事务管理器类型,JDBC 是 JdbcTransactionFactory.class 的别名 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源。POOLED 是 PooledDataSourceFactory.class 的别名 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3308/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/userMapper.xml"></mapper>
    </mappers>

</configuration>

特点

缺点: 如果有多个实体类要配置别名,那么要将每个实体类都配置一遍

优点: 可以对每个实体类自定义别名,
如下:
<typeAlias type="org.malaoshi.entity.Application" alias="app"></typeAlias>
将实体类Application起别名为:app

使用

修改userMapper.xml配置文件

在该文件中,将org.malaoshi.entity.User可以修改成别名,如:user或User或USER等,因为别名不区分大小写

如下:

<resultMap id="UserMap" type="user">
    <id property="id" column="id" ></id>
    <result property="uername" column="uername"/>
    <result property="password" column="password"/>
    <result property="createTime" column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP"/>
</resultMap>
<insert id="save" parameterType="User">
    insert into user (id,username,password) values (#{id},#{username},#{password})
</insert>
<select id="selectByUsername" resultType="USER">
    select id,username,password from user where username like #{username}
</select>

执行测试

方式二

修改mybatis-config.xml

增加下面代码:

<!-- 给实体类起别名 -->
<typeAliases>
    <package name="org.malaoshi.entity" />
</typeAliases>

指定 实体类包路径,该路径下的类名就是其别名,且不区分大小写

完整配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 给实体类起别名 -->
    <typeAliases>
        <package name="org.malaoshi.entity" />
    </typeAliases>
    <!-- 配置环境 。default名字随意起-->
    <environments default="mysql">
    <!--    id名字必须同上面default名    -->
        <environment id="mysql">
            <!-- 配置事务管理器类型,JDBC 是 JdbcTransactionFactory.class 的别名 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源。POOLED 是 PooledDataSourceFactory.class 的别名 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3308/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/userMapper.xml"></mapper>
    </mappers>

</configuration>

使用同上

特点

缺点: 别名就是类名,不区分大小写。不能自定义别名

优点: 配置一次,就可以对该包下的所有实体类,起别名


原文出处:/show_1EF6WeaCnllb.html