主表和副表
多对一:多的一方为主表,一的一方为副表。
一对多:一的一方为主表,多的一方为副表。
<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>
<resultMap id="studentResultMapAssociation" 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="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>
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>
分步查询好处:
● 第一个优点:代码复用性增强。
● 第二个优点:支持延迟加载。【暂时访问不到的数据可以先不查询。提高程序的执行效率。】
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”
<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="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>
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>