一、一对多
1、创建实体类
Student
package com.spring.entity;
public class Student {
private int id;
private String name;
private Classes classes;
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Classes
package com.spring.entity;
public class Classes {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、级联查询SQL
select * from student,classes where student.cid = classes.id and student.id = 1;
3、MyBatis映射逻辑
MyBatis 框架在解析结果集的时候,将结果集与某个实体类进行映射,结果集中的字段与实体类的成员变量进行映射,根据字段名与成员变量相等的规则来进行映射,能映射起来的成员变量就会完成赋值,无法映射的成员变量不会赋值。
4、Mapper代理实现自定义接口
StudentRepository
public interface StudentRepository {
public Student findById(int id);
}
StudentRepository.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.spring.repository.StudentRepository">
<resultMap id="studentMap" type="com.spring.entity.Student">
<id property="id" column="sid"></id>
<result property="name" column="sname"></result>
<!-- 映射 classes -->
<association property="classes" javaType="com.spring.entity.Classes">
<id property="id" column="cid"></id>
<result property="name" column="cname"></result>
</association>
</resultMap>
<select id="findById" parameterType="int" resultMap="studentMap">
select student.id sid,student.name sname,classes.id cid,classes.name cname from student,classes where student.cid = classes.id and student.id = #{id}
</select>
</mapper>
需要注意的是:association 标签,通过设置 javaType 属性,映射实体类,collection 标签,通过设置 ofType 属性,映射实体类。
ClassesRepository
package com.spring.repository;
import com.spring.entity.Classes;
public interface ClassesRepository {
public Classes findById(int id);
}
ClassesRepository.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.spring.repository.ClassesRepository">
<resultMap id="classesMap" type="com.spring.entity.Classes">
<id property="id" column="cid"></id>
<result property="name" column="cname"></result>
<!-- 映射 students -->
<collection property="students" ofType="com.spring.entity.Student">
<id property="id" column="sid"></id>
<result property="name" column="sname"></result>
</collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="classesMap">
select classes.id cid,classes.name cname,student.id sid,student.name sname from classes,student where classes.id = student.cid and classes.id = #{id}
</select>
</mapper>
二、多对多
多对多:其实是双向的一对多关系,借助于一张中间表,中间表和两张目标表都是一对多关系,间接完成多对多的关系映射。
一个 Customer 可以对应多个 Goods,一个 Goods 也可以对应多个 Customer,所以双方都是用 collection 标签进行映射。
1、创建实体类
Customer
package com.spring.entity;
import java.util.List;
public class Customer {
private int id;
private String name;
private List<Goods> goods;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Goods> getGoods() {
return goods;
}
public void setGoods(List<Goods> goods) {
this.goods = goods;
}
}
Goods
package com.spring.entity;
import java.util.List;
public class Goods {
private int id;
private String name;
private List<Customer> customers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
}
2、级联查询SQL
select c.id cid,c.name cname,g.id gid,g.name gname
from customer c,goods g,customer_goods cg where c.id = cg.cid and g.id = cg.gid and c.id = 1
3、Mapper代理实现自定义接口
CustomerRepository
package com.spring.repository;
import com.spring.entity.Customer;
public interface CustomerRepository {
public Customer findById(int id);
}
CustomerRepository.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.spring.repository.CustomerRepository">
<resultMap id="customerMap" type="com.spring.entity.Customer">
<id property="id" column="cid"></id>
<result property="name" column="cname"></result>
<!-- 映射 goods -->
<collection property="goods" ofType="com.spring.entity.Goods">
<id property="id" column="gid"></id>
<result property="name" column="gname"></result>
</collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="customerMap">
select c.id cid,c.name cname,g.id gid,g.name gname
from customer c,goods g,customer_goods cg where c.id = cg.cid and g.id = cg.gid and c.id = #{id}
</select>
</mapper>
GoodsRepository
package com.spring.repository;
import com.spring.entity.Goods;
public interface GoodsRepository {
public Goods findById(int id);
}
GoodsRepository.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.spring.repository.GoodsRepository">
<resultMap id="goodsMap" type="com.spring.entity.Goods">
<id property="id" column="gid"></id>
<result property="name" column="gname"></result>
<!-- 映射 customers -->
<collection property="customers" ofType="com.spring.entity.Customer">
<id property="id" column="cid"></id>
<result property="name" column="cname"></result>
</collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="goodsMap">
select c.id cid,c.name cname,g.id gid,g.name gname
from customer c,goods g,customer_goods cg where c.id = cg.cid and g.id = cg.gid and g.id = #{id}
</select>
</mapper>