resultMap,创建专门的一对一查询;
resultType
pojo 属性名需要和数据库中 表的 column名 一致才能映射成功。
当数据库表的列名和Pojo中属性名一致的时候(或者不一致可以使用别名解决);
以下这种情况,不使用*查询可以使用resultType ,使用resultMap需要使用*查询。

配置文件如下:
<select id="findUser1ByUsernameAndPassword" parameterType="user1" resultType="user1">
select user_id userId,age,username,password from user1 where username=#{username} and password=#{password}
</select>
resultMap;
用来解决pojo 属性名和column名字 不一致,配置resultMap ,配置resultMap标签,id=“映射的结果” type=参数类型;
在mybatis 内部处理,仍然是保持Pojo和数据库table column 名字保持一致;

<select id="findOrdersByNameAndPrice" parameterType="orders" resultMap="ordersMap">
select * from orders where name=#{name} and price=#{price}
</select>
<resultMap id="ordersMap" type="orders">
<id property="id" column="id"/><!--主键-->
<result property="name" column="name"/>
<result property="price" column="price"/>
</resultMap>
本文详细介绍了MyBatis中的resultMap和resultType的使用方法,包括如何解决POJO属性名与数据库表列名不一致的问题,以及在不同场景下如何选择使用resultMap或resultType。
4747

被折叠的 条评论
为什么被折叠?



