MyBatis:当表字段名和实体类属性名不一致

第一种解决方法:在sql中使用别名

<select id="getRoleList" resultType="com.ttpfx.domain.Role">
    select ID as id, ROLE_NAME as name, ROLE_DESC as description from role;
</select>

第二种解决方法:使用resultMap

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ttpfx.dao.RoleDao">
    <resultMap id="roleMap" type="com.ttpfx.domain.Role">
        <id column="ID" property="id"/>
        <result column="ROLE_NAME" property="name"/>
        <result column="ROLE_DESC" property="description"/>
    </resultMap>

    <select id="getRoleList" resultMap="roleMap">
        select ID, ROLE_NAME, ROLE_DESC from role;
    </select>
</mapper>

两种方式的对比:

  1. 第一种方式执行效率更高
  2. 第二种方式开发效率更高,因为resultMap可以重复使用
posted @ 2021-03-08 11:21  ttpfx  阅读(73)  评论(0编辑  收藏  举报