Mybatis ResultMap和ResultType的差别

ResultMap和ResultType: 两者都是表示查询结果集与java对象之间的一种关系,处理查询结果集,映射到java对象。 resultMap:表示将查询结果集中的列一一映射到bean对象的各个属性。

 <resultMap id="userResultMap" type="com.xxx.User">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="user_name" property="userName"/>
 </resultMap>
   
<select id="selectedUser" resultMap="userResultMap">
        SELECT * FROM users WHERE user_id = #{user_id} AND user_name = #{user_name}
</select>

映射的查询结果集中的列标签可以根据需要灵活变化.

屏幕快照 2019-06-28 13.54.10.png

ResultType:表示的是bean中的对象类,此时可以省略掉resultMap标签的映射,但是必须保证查询结果集中的属性 和 bean对象类中的属性是一一对应的。

 <select id="selectedUser" resultType="com.xxx.User" parameterType="String">
        SELECT user_Id AS userId, user_Name AS userName FROM users WHERE user_id = #{user_id} AND user_name = #{user_name}
</select>

屏幕快照 2019-06-28 13.54.10.png

resultType跟resultMap不能同时存在。

posted @ 2020-12-18 14:23  纯洁的赤子之心  阅读(586)  评论(0)    收藏  举报