【SSM】MyBatis(十一.MyBatis的高级映射和延迟加载)
文章目录
- 1.准备数据
- 2.多对一
- 2.1 方法一:级联属性映射
- 2.2 方法二:association
- 2.3 方法三:分步查询
- 2.4 一对多延迟加载
- 3. 一对多
- 3.1 方法一:collection
- 3.2 方法二:分步查询
1.准备数据
2.多对一
主表和副表
多对一:多的一方为主表,一的一方为副表。
一对多:一的一方为主表,多的一方为副表。
2.1 方法一:级联属性映射
<mapper namespace="com.sdnu.mybatis.mapper.StudentMapper">
<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="selectById" resultMap="studentResultMap">
select
s.sid, s.sname, c.cid, c.cname
from
t_stu s left join t_clazz c on s.cid = c.cid
where
s.sid = #{id}
</select>
</mapper>
2.2 方法二:association
<resultMap id="studentResultMapAssociation" type="Student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<!--
property:提供要映射的POJO类属性名
javaType:用来指定要映射的java类型
-->
<association property="clazz" javaType="Clazz">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
</association>
</resultMap>
<select id="selectByIdAssociation" resultMap="studentResultMapAssociation">
select
s.sid, s.sname, c.cid, c.cname
from
t_stu s left join t_clazz c on s.cid = c.cid
where
s.sid = #{id}
</select>
2.3 方法三:分步查询
StudentMapper.java
public interface StudentMapper {
Student selectByIdStep1(Integer sid);
}
StudentMapper.xml
<mapper namespace="com.sdnu.mybatis.mapper.StudentMapper">
<resultMap id="studentResultMapByStep" type="Student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<association property="clazz"
select="com.sdnu.mybatis.mapper.ClazzMapper.selectByIdStep2"
column="cid"/>
</resultMap>
<select id="selectByIdStep1" resultMap="studentResultMapByStep">
select sid, sname, cid from t_stu where sid = #{sid}
</select>
</mapper>
ClazzMapper.java
public interface ClazzMapper {
Clazz selectByIdStep2(Integer cid);
}
ClazzMapper.xml
<mapper namespace="com.sdnu.mybatis.mapper.ClazzMapper">
<select id="selectByIdStep2" resultType="Clazz">
select cid, cname from t_clazz where cid = #{cid}
</select>
</mapper>
分步查询好处:
● 第一个优点:代码复用性增强。
● 第二个优点:支持延迟加载。【暂时访问不到的数据可以先不查询。提高程序的执行效率。】
2.4 一对多延迟加载
fetchType=“lazy”
<resultMap id="studentResultMapByStep" type="Student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<association property="clazz"
select="com.sdnu.mybatis.mapper.ClazzMapper.selectByIdStep2"
column="cid"
fetchType="lazy"/>
</resultMap>
<select id="selectByIdStep1" resultMap="studentResultMapByStep">
select sid, sname, cid from t_stu where sid = #{sid}
</select>
一般全局开启延迟加载
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
不需要时再关掉:
fetchType=“eager”
3. 一对多
3.1 方法一:collection
<resultMap id="clazzResultMap" type="Clazz">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<!--ofType属性用来指定集合中的元素类型-->
<collection property="stus" ofType="Student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
</collection>
</resultMap>
<select id="selectByCollection" resultMap="clazzResultMap">
select s.sid, s.sname, c.cid, c.cname from t_clazz c left join t_stu s on c.cid = s.cid where c.cid = #{cid}
</select>
3.2 方法二:分步查询
ClazzMapper.java
public interface ClazzMapper {
Clazz selectByStep1(Integer cid);
}
ClazzMapper.xml
<resultMap id="clazzResultMapStep" type="Clazz">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<collection property="stus"
select="com.sdnu.mybatis.mapper.StudentMapper.selectByCidStep2"
column="cid"/>
</resultMap>
<select id="selectByStep1" resultMap="clazzResultMapStep">
select cid, cname from t_clazz where cid = #{cid}
</select>
StudentMapper.java
public interface StudentMapper {
List<Student> selectByCidStep2(Integer cid);
}
StudentMapper.xml
<mapper namespace="com.sdnu.mybatis.mapper.StudentMapper">
<select id="selectByCidStep2" resultType="Student">
select * from t_stu where cid = #{cid}
</select>
</mapper>