Mybatis [19] – mybatis auto correlation many to many query

Time:2021-9-25

Note: the code is hosted inGitHubOn, the address is:https://github.com/Damaer/Mybatis-Learning, project ismybatis-15-oneself-many2many, self fetching and Maven environment configuration are requiredmysqlEnvironment(sqlStatement inresourceLowertest.sqlIf you feel useful, you can point a small star.

docsifyThe document address is:https://damaer.github.io/Mybatis-Learning/#/

The so-called many to many query is similar to: a student can choose multiple courses, and one may have multiple students.

The data sheet is designed as follows:

Mybatis [19] - mybatis auto correlation many to many query

Mybatis [19] - mybatis auto correlation many to many query

Mybatis [19] - mybatis auto correlation many to many query

Entity class corresponding to the databaseCourse.java, it’s worth noting that,toString()We didn’t join in the methodstudentsProperty, because inStudentoftostring()Our method has been addedCourseIf this class is added here, it will have an endless loop. Just add one.

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 + "]";
    }
}

Defined interface part:

public interface IStudentDao {
    Student selectStudentById(int id);
}

mapper.xmlWhen you query a document, you look up three tablessid=studentId and cid=courseId and sid=#{xxx}Associated.

The result is mapped, except for the primary key andsname,coursesProperty is set mapped, that is, toCourseType.

<?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>

Unit test class:

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();
    }
  }
}

result:

[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++]]]

[introduction to the author]
Qin Huai, official account.Qinhuai grocery store】The author, the road of technology is not a moment, high mountains and long rivers, even if it is slow, it will not stop. Personal writing direction: Java source code analysis, JDBC, mybatis, spring, redis, distributed, sword finger offer, leetcode, etc. I carefully write every article. I don’t like the title party and fancy. I mostly write a series of articles. I can’t guarantee that what I write is completely correct, but I guarantee that what I write has been practiced or searched for information. Please correct any omissions or mistakes.

What did I write in 2020?

Open source programming notes

Weekdays are precious. We can only use the evenings and weekends to learn writing. Pay attention to me. Let’s grow up together~

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]