(十九)Mybatis自關聯多對多查詢
阿新 • • 發佈:2018-11-28
注:程式碼已託管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,專案是mybatis-14-oneself-many2one,需要自取,需要配置maven環境以及mysql環境(sql語句在resource下的test.sql中),覺得有用可以點個小星星,小菜鳥在此Thanks~
所謂多對多查詢,就是類似於們,一個學生可以選多門課程,一門可能可以有多個學生。
我們的資料表:
與資料庫對應的實體類:
course.java,值得注意的是,toString方法裡面我們沒有加入students屬性,這是因為在student的tostring方法裡面已經加入我們的course這個類了,如果這裡加入就會死迴圈,只加一個就可以了。
import java.util.Set;
public class Course {
private Integer cid;
private String cname;
private Set<Student>students;
@Override
public String toString() {
return "Course [cid=" + cid + ", cname=" + cname +"]";
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this .students = students;
}
}
Student.java
import java.util.Set;
public class Student {
private Integer sid;
private String sname;
private Set<Course>courses;
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 Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", courses="
+ courses + "]";
}
}
定義的介面部分:
public interface IStudentDao {
Student selectStudentById(int id);
}
mapper.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="dao.IStudentDao">
<resultMap type="Student" id="studentMapper">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
<collection property="courses" ofType="Course">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
</collection>
</resultMap>
<select id="selectStudentById" resultMap="studentMapper">
select sid,sname,cid,cname
from student,middle,course
where sid=studentId and cid=courseId and sid=#{xxx}
</select>
</mapper>
單元測試類:
public class MyTest {
private IStudentDao dao;
private SqlSession sqlSession;
@Before
public void Before(){
sqlSession=MyBatisUtils.getSqlSession();
dao=sqlSession.getMapper(IStudentDao.class);
}
@Test
public void TestselectMinisterById(){
Student student=dao.selectStudentById(1);
System.out.println(student);
}
@After
public void after(){
if(sqlSession!=null){
sqlSession.close();
}
}
}
結果:
[service] 2018-07-16 20:25:37,846 - dao.IStudentDao.selectStudentById -843 [main] DEBUG dao.IStudentDao.selectStudentById - ==> Preparing: select sid,sname,cid,cname from student,middle,course where sid=studentId and cid=courseId and sid=?
[service] 2018-07-16 20:25:37,894 - dao.IStudentDao.selectStudentById -891 [main] DEBUG dao.IStudentDao.selectStudentById - ==> Parameters: 1(Integer)
[service] 2018-07-16 20:25:37,935 - dao.IStudentDao.selectStudentById -932 [main] DEBUG dao.IStudentDao.selectStudentById - <== Total: 2
Student [sid=1, sname=Jam, courses=[Course [cid=1, cname=JAVA], Course [cid=2, cname=C++]]]