resultMap、
<resultMap id=" " type="映射的类,可别名">
<id /> 查询结果按id 的字段去重,使字段唯一
column=" ";数据库的字段; property=" "; 映射类型的属性; jdbcType 数据库的数据类型
<association property="映射的类类型,一对一" javaType="">
<id /> <result />
懒加载、
association添加属性:
1)fetchType="lazy" 开启懒加载
2)select=" ";用到再调用该方法
3)column=" ";调用该方法用到的字段
<!-- ---------------------懒加载 一对一 ---------------------- -->
<resultMap id="StudentResult" type="mapper.Student" extends="BaseResultMap">
<association property="teacher" javaType="model.Teacher" select="queryBytid" column="tid" fetchType="lazy">
<id column="tid" property="tid"/>
<result column="tname" property="tname"/>
</association>
</resultMap>
<select id="queryAll" resultMap="StudentResult">
select * from student
</select>
<select id="queryBytid" resultType="model.Teacher">
select * from teacher where tid = #{tid}
</select>
<!-- --------------------一对多 ---------------- -->
<resultMap id="TeacherResult" type="mapper.Teacher" extends="BaseResultMap">
<collection property="students" ofType="model.Student" >
<id column="id" property="id"/>
<result column="sname" property="sname"/>
</collection>
</resultMap>
<select id="queryAll" resultMap="TeacherResult">
select teacher.*, student.sname from student right join teacher where student.tid=teacher.tid
</select>
本文详细介绍了 MyBatis 中 resultMap 的使用方法,包括如何实现查询结果的去重及字段映射,以及如何配置懒加载进行一对多和一对一的数据关联。通过具体的 XML 配置示例,读者可以了解到如何设置 association 和 collection 属性来优化数据加载过程。
797

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



