Mybatis【19】-- Mybatis自关联多对多查询

注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-15-oneself-many2many,需要自取,需要配置maven环境以及mysql环境(sql语句在resource下的test.sql中),觉得有用可以点个小星星。

docsify文档地址在:https://damaer.github.io/Mybatis-Learning/#/

所谓多对多查询,就是类似于:一个学生可以选多门课程,一门可能可以有多个学生。

数据表设计如下:

与数据库对应的实体类Course.java,值得注意的是,toString()方法里面我们没有加入students属性,这是因为在Studenttostring()方法里面已经加入我们的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文件,查询的时候,查的是三张表,通过sid=studentId and cid=courseId and sid=#{xxx}关联起来。

对结果做了一个映射,除了主键以及sname,courses属性做了集合映射,也就是对Course类型进行映射。

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

【作者简介】
秦怀,公众号【秦怀杂货店】作者,技术之路不在一时,山高水长,纵使缓慢,驰而不息。个人写作方向:Java源码解析,JDBC,Mybatis,Spring,redis,分布式,剑指Offer,LeetCode等,认真写好每一篇文章,不喜欢标题党,不喜欢花里胡哨,大多写系列文章,不能保证我写的都完全正确,但是我保证所写的均经过实践或者查找资料。遗漏或者错误之处,还望指正。

2020年我写了什么?

开源编程笔记

平日时间宝贵,只能使用晚上以及周末时间学习写作,关注我,我们一起成长吧~

posted @ 2021-02-23 01:59  第十六封  阅读(110)  评论(0编辑  收藏  举报