lidaye2396

博客园 首页 新随笔 联系 订阅 管理

5、解决属性名和字段名不一致的问题

1. 问题

数据库中的字段

在这里插入图片描述

新建一个项目,拷贝之前的,测试实体类字段不一致的情况

在这里插入图片描述

测试出现问题 在这里插入图片描述

// select * from user where id = #{id}
// 类型处理器
// select id,name,pwd from user where id = #{id}
123

解决方法:

  • 起别名

<select id="getUserById" resultType="com.kuang.pojo.User">
  select id,name,pwd as password from USER where id = #{id}
</select>
123

2. resultMap

结果集映射

id name pwd

id name password

<!--结果集映射-->
<resultMap id="UserMap" type="User">
   <!--column数据库中的字段,property实体类中的属性-->
   <result column="id" property="id"></result>
   <result column="name" property="name"></result>
   <result column="pwd" property="password"></result>
</resultMap>

<select id="getUserList" resultMap="UserMap">
  select * from USER
</select>
1234567891011
    • resultMap 元素是 MyBatis 中最重要最强大的元素。

    • ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。

    • ResultMap 的优秀之处——你完全可以不用显式地配置它们。

    • 如果这个世界总是这么简单就好了。

posted on 2021-04-03 11:11  lidaye2396  阅读(34)  评论(0编辑  收藏  举报