mybatis中resultMap和resultType简介说明
下文笔者讲述resultMap和resultType简介及功能说明,如下所示
resultMap和resultType简介
在mybatis中 resultMap和resultType
都是用于定义
将数据库查询结果映射到Java对象
=======================================
但是这两者也有一点点不同
使用resultType时,如果结果集中有同名字段,
则默认情况下后出现的字段值会覆盖前面的字段值
使用resultMap时,每个字段可以精准的映射到指定的属性上
例resultMap的使用示例
<select id="selectExample" resultMap="combinedResult">
SELECT
t1.id as t1_id,
t2.id as t2_id,
...
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.t1_id
</select>
<resultMap id="combinedResult" type="YourJavaType">
<id property="key1" column="t1_id"/>
<!-- 其他来自table1的字段 -->
<id property="key2" column="t2_id"/>
<!-- 其他来自table2的字段 -->
</resultMap>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


