我们以生活中一个老师教多个学生,一个学生有多个老师为例,讲解MybatisPlus如何实现多对多的映射关系。
1)创建实体 Teachers.java(教师类)
package com.oracle.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teachers extends BaseEntity implements Serializable,Cloneable {
@Id
@GeneratedValue(generator="mytid")
@GenericGenerator(name="mytid",strategy="assigned")
@Column(length = 4)
private String tid; //教师编号
private String tname; //教师姓名
}
Students.java(学生类)
package com.oracle.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity //表示这个类对应数据库中的一张表
public class Students extends BaseEntity implements Serializable,Cloneable {
//既然是表,就必须有主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //使用数据库默认的主键生成策略
@TableId(type = IdType.AUTO)
private int sid; //主键
@Column(length = 20) //规定学生的姓名不超过20个字符
private String sname; //姓名,默认是占用255个字符。
@Column(length = 2)
private String gender; //性别
@Column(length = 32)
private String birthday; //出生日期
private String school; //所在学校
private String major; //所学的专业
private boolean status; //状态 1:在籍 0:辍学
@Column(length = 4)
private String cid; //班级的编号
}
首先定义联合主键类。定义该联合主键的目的是方便今后实现Mybatis与JPA双数据源的封装。 例如:StudentsTeachersRepository定义如下
public interface StudentsTeachersRepository extends BaseRepository<StudentsTeachers, StudentsTeachersPrimaryKey> {
}
package com.oracle.entity;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.IdClass;
import java.io.Serializable;
public class StudentsTeachersPrimaryKey implements Serializable {
private int sid;
private String tid;
}
StudentsTeachers(学生教师关系类)。
package com.oracle.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import java.io.Serializable;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@IdClass(StudentsTeachersPrimaryKey.class)
public class StudentsTeachers extends BaseEntity implements Serializable,Cloneable {
//sid+tid构成了联合主键
@Id
protected int sid;
@Id
private String tid;
}
测试多对多的映射关系。由于篇幅限制Students和Teachers的Mapper和Service层代码省略,大家可以自行参考前面案例实现。
package com.oracle.service;
import com.oracle.entity.ClassRoom;
import com.oracle.entity.Students;
import com.oracle.entity.StudentsTeachers;
import com.oracle.entity.Teachers;
import com.oracle.view.StudentsView;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentsServiceTest {
@Resource
private ClassRoomService classRoomService;
@Resource
private StudentsService studentsService;
@Resource
private TeachersService teachersService;
@Resource
private StudentsTeachersService studentsTeachersService;
@Test
public void testSaveStudents(){
ClassRoom classRoom = new ClassRoom("C001","四川工业学院软件工程1班");
classRoomService.insertWithMybatis(classRoom);
Teachers t1 = new Teachers("T001","刘老师");
Teachers t2 = new Teachers("T002","陈老师");
Teachers t3 = new Teachers("T003","赵老师");
teachersService.insertWithMybatis(t1);
teachersService.insertWithMybatis(t2);
teachersService.insertWithMybatis(t3);
Students s1 = new Students(1,"张三","男","1999-10-10","四川工业学院","软件工程",true,classRoom.getCid());
Students s2 = new Students(2,"李四","女","2000-10-10","四川工业学院","软件工程",true,classRoom.getCid());
Students s3 = new Students(3,"王五","男","1999-10-10","四川工业学院","软件工程",true,classRoom.getCid());
studentsService.insertWithMybatis(s1);
studentsService.insertWithMybatis(s2);
studentsService.insertWithMybatis(s3);
StudentsTeachers st1 = new StudentsTeachers(s1.getSid(),t1.getTid());
StudentsTeachers st2 = new StudentsTeachers(s1.getSid(),t2.getTid());
StudentsTeachers st3 = new StudentsTeachers(s1.getSid(),t3.getTid());
StudentsTeachers st4 = new StudentsTeachers(s2.getSid(),t1.getTid());
StudentsTeachers st5 = new StudentsTeachers(s2.getSid(),t2.getTid());
StudentsTeachers st6 = new StudentsTeachers(s3.getSid(),t2.getTid());
StudentsTeachers st7 = new StudentsTeachers(s3.getSid(),t3.getTid());
//StudentsTeachers st1 = new StudentsTeachers(s1.getSid(),t1.getTid());
studentsTeachersService.insertWithMybatis(st1);
studentsTeachersService.insertWithMybatis(st2);
studentsTeachersService.insertWithMybatis(st3);
studentsTeachersService.insertWithMybatis(st4);
studentsTeachersService.insertWithMybatis(st5);
studentsTeachersService.insertWithMybatis(st6);
studentsTeachersService.insertWithMybatis(st7);
}
}
