多对一
- 级联属性映射
public class Student { private Integer sid; private String sname; private Clazz clazz; //班级 } public class Clazz { private Integer cid; private String cname; }
<resultMap id="studentResultMap" type="Student"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> <result property="clazz.cid" column="cid"/> <result property="clazz.cname" column="cname"/> </resultMap> <select id="selectBySid" resultMap="studentResultMap"> select s.*, c.* from t_student s join t_clazz c on s.cid = c.cid w here sid = #{sid} </select>
- association
<resultMap id="studentResultMap" type="Student"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> <association property="clazz" javaType="Clazz"> <id property="cid" column="cid"/> <result property="cname" column="cname"/> </association> </resultMap> <select id="selectBySid" resultMap="studentResultMap"> select s.*, c.* from t_student s join t_clazz c on s.cid = c.cid where sid = #{sid} </select>
- 分步查询
studentMapper.xml <resultMap id="studentResultMap" type="Student"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> <association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid"/> </resultMap> <select id="selectBySid" resultMap="studentResultMap"> select s.* from t_student s where sid = #{sid} </select>
分步优点:代码复用性强,支持延迟加载clazzMapper.xml <select id="selectByCid" resultType="Clazz"> select * from t_clazz where cid = #{cid} </select>
- 延迟加载
懒加载即使用到的时候才回去查询,例如Student类有Clazz类,只有使用到了Clazz班级里的属性才会去查询,只使用Student里的变量不会去查询Clazz表- 设置fetchType实现懒加载,在分步查询的基础上修改
<association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid" fetchType = "lazy"/>
- 全局开启懒加载
mybatis: configuration: lazy-loading-enabled: true #开启全局懒加载
- 如何针对性的关闭懒加载
<association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid" fetchType = "eager"/>
- 设置fetchType实现懒加载,在分步查询的基础上修改
一对多
- collection
public class Clazz { private Integer cid; private String cname; private List<Student> stus; // 一个班级对应多名学生 }
<resultMap id="clazzResultMap" type="Clazz"> <id property="cid" column="cid"/> <result property="cname" column="cname"/> <collection property="stus" ofType="Student"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> </collection> </resultMap> <select id="selectClazzAndStusByCid" resultMap="clazzResultMap"> select * from t_clazz c join t_student s on c.cid = s.cid where c.cid = #{cid} </select>
- 分步查询
<resultMap id="clazzResultMap" type="Clazz"> <id property="cid" column="cid"/> <result property="cname" column="cname"/> <!--主要看这里--> <collection property="stus" select="com.powernode.mybatis.mapper.StudentMapper.selectByCid" column="cid"/> </resultMap> <!--sql语句也变化了--> <select id="selectClazzAndStusByCid" resultMap="clazzResultMap"> select * from t_clazz c where c.cid = #{cid} </select>
List<Student> selectByCid(Integer cid);
延迟加载的机制都是一样的<select id="selectByCid" resultType="Student"> select * from t_student where cid = #{cid} </select>