MyBatis的高级映射
完成属性映射,首先需要确定各个表之间的关系,下面我们建立了两张表,学生表与班级表
学生表
班级表
多对一映射
多对一映射有三种方法。在多对一映射关系中需要确定主表,确定了主表即确定了主类。
在学生类里面添加班级类属性
//学生类
package com.powernode.mybatis.pojo;
/**
* 学生类
* @author 老杜
* @version 1.0
* @since 1.0
*/
public class Student {
private Integer sid;
private String sname;
private Clazz clazz;
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", clazz=" + clazz +
'}';
}
public Student() {
}
public Student(Integer sid, String sname) {
this.sid = sid;
this.sname = sname;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
//班级类
public class Clazz {
private Integer cid;
private String cname;
级联属性映射
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.powernode.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="selectBySid" resultMap="studentResultMap">
select s.*, c.* from t_student s join t_clazz c on s.cid = c.cid where sid = #{sid}
</select>
</mapper>
StudentMapper接口
package com.cxy.mybatis.mapper;
import com.cxy.mybatis.pojo.Student;
import java.util.List;
public interface StudentMapper {
/**
* 检查是否正确与数据库进行连接
* @param sid
* @return
*/
Student selectBySid(Integer sid);
/**
* 多对一的级联属性映射
* @param sid 传入的学生id
* @return
*/
Student selectBySidWith(int sid);
}
编写测试方法
package com.powernode.mybatis.test;
import com.powernode.mybatis.mapper.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.junit.Test;
public class StudentMapperTest {
@Test
public void testSelectBySid(){
StudentMapper mapper = SqlSessionUtil.openSession().getMapper(StudentMapper.class);
Student student = mapper.selectBySid(1);
System.out.println(student);
}
}
association映射
这个和上面的级联映射大部分相同,不过在结果映射集中需要添加以下配置
StudentMapper.xml
<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>
分步映射
分步映射是用的的最多的,这样有利于代码的复用以及进行sql语句的延迟加载
分步映射其他地方不需要修改,只需要修改下面三个地方
- association中select位置填写sqlId,**这个sql id是指在ClazzMapper接口中的查询方法的权限定名称,sqlid=namespace+id,**其中column属性作为这条子sql语句的条件
<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接口中添加方法
/**
* 多对一分步查询2
* @param cid
* @return
*/
Clazz selectByCidStep2(Integer cid);
- 在ClazzMapper.xml文件中进行配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.powernode.mybatis.mapper.ClazzMapper">
<select id="selectByCid" resultType="Clazz">
select * from t_clazz where cid = #{cid}
</select>
</mapper>
多对一延迟加载
可以再association中添加fetchType=lazy;表示开启延迟加载;fetchType=eager表示关闭延迟加载
一般情况下,在项目开发中我们会进行全局上的设置,在全局上设置开启延迟加载,具体代码如下
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
如果个别功能需求需要关闭延迟加载的话,可以设置fetch=eager;
一对多映射
一对多的实现,通常是在一的一方中有List集合属性。(一的一方是主表)
在Clazz类中添加List stus; 属性。
public class Clazz {
private Integer cid;
private String cname;
private List<Student> stus;
// set get方法
// 构造方法
// toString方法
}
一对多的实现有两种方式:
-
collection
-
分步映射查询
第一种方式collection
/**
* 一对多collection查询
* @param cid
* @return 返回的是Clazz对象,里面有List<Student> stus 对象
*/
Clazz selectByCid(Integer cid);
ClazzMapper.xml文件
<resultMap id="selectByCidMap" type="Clazz">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<collection property="stus" ofType="Student">
<!--注意是ofType,表示“集合中的类型”-->
<id property="sid" column="sid"></id>
<result property="sname" column="sname"></result>
</collection>
</resultMap>
<select id="selectByCid" resultMap="selectByCidMap">
select * from t_class c join t_stu s on c.cid=s.cid where c.cid=#{cid}
</select>
第二种方式:分步查询
<resultMap id="selectByCidStep1Map" type="Clazz">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<!--主要是修改这一部分内容-->
<collection property="stus"
select="com.cxy.mybatis.mapper.StudentMapper.selectByCidStep2"
column="cid"
fetchType="eager"
>
</collection>
</resultMap>
<!--sql语句也发生了变化-->
<select id="selectByCidStep1" resultMap="selectByCidStep1Map">
select * from t_class c where c.cid=#{cid}
</select>
和多对一一样,在一对多种需要在StudentMapper接口中定义方法,
/**
* 一对多的分步查询,接收的是传过来的cid
* @param cid
* @return
*/
List<Student> selectByCidStep2(Integer cid);
<!--StudentMapper.xml文件-->
<resultMap id="selectByCidStep2Map" type="Student">
<id property="sid" column="sid"></id>
<result property="sname" column="sname"/>
</resultMap>
<select id="selectByCidStep2" resultMap="selectByCidStep2Map">
select * from t_stu where cid=#{cid}
</select>
多对多映射与一对一映射
多对多映射需要进行多表联查,往往需要借助一个中间表来实现表与表之间的联系。
多对多映射:可以分解成两个多对一映射
一对一映射:可以分解成两个一对多映射