Mybatis【Result-解决属性名和字段名不一致问题】

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

1、问题

数据库的字段

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

测试出现问题

解决方法:

起别名

  <select id="getUserById" parameterType="int" resultType="com.jiang.pojo.User">
        select id,name,pwd as password  from mybatis.user where id= #{id}
    </select>

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>

resultMap 元素是 MyBatis 中最重要最强大的元素。

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

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

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



https://www.bilibili.com/video/BV1NE411Q7Nx?p=11&spm_id_from=pageDriver

猜你喜欢

转载自blog.csdn.net/qq_48108092/article/details/124154152