Mybatis learning notes - one to one, one to many, many to many association query

Princess SINB 2021-04-08 03:44:57
mybatis learning notes association query


One 、 One to one association query

1、 Database data
 Insert picture description here
 Insert picture description here

2、 Entity class

public class Teacher {

String tid;
String tname;
Class classes;
public String getTid() {

return tid;
}
public void setTid(String tid) {

this.tid = tid;
}
public String getTname() {

return tname;
}
public void setTname(String tname) {

this.tname = tname;
}
public Class getClasses() {

return classes;
}
@Override
public String toString() {

return "Teacher{" +
"tid='" + tid + '\'' +
", tname='" + tname + '\'' +
", classes=" + classes +
'}';
}
public void setClasses(Class classes) {

this.classes = classes;
}
}
public class Class {

String cid;
String cname;
int StudentNum;
public String getCid() {

return cid;
}
public void setCid(String cid) {

this.cid = cid;
}
public String getCname() {

return cname;
}
public void setCname(String cname) {

this.cname = cname;
}
public int getStudentNum() {

return StudentNum;
}
public void setStudentNum(int studentNum) {

StudentNum = studentNum;
}
@Override
public String toString() {

return "Class{" +
"cid='" + cid + '\'' +
", cname='" + cname + '\'' +
", StudentNum=" + StudentNum +
'}';
}
}

3、Dao Interface corresponding method : According to the class query teacher information

 public List<Teacher> selectClass(String cid);

4、xml file select sentence

<resultMap id="TeacherToClass" type="com.atguigu.mybatis.Entry.Teacher">
<id column="tid" property="tid"></id>
<result column="tname" property="tname"></result>
<association property="classes" javaType="com.atguigu.mybatis.Entry.Class">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<result property="studentNum" column="studentNum"></result>
</association>
</resultMap>
<select id="selectClass" resultMap="TeacherToClass">
select tid,tname,class.cid,cname,studentNum
from teacher,class
where teacher.cid=class.cid and class.cid=#{cid}
</select>

5、 Test statement

 @Test
public void selectClass(){

SqlSession sqlSession= MybatisUtil.getSqlSession();
List<Teacher> teacher=sqlSession.selectList("com.atguigu.mybatis.Dao.TeacherDao.selectClass","1");
System.out.println(teacher);
}

Two 、 One to many associated queries

public class Student2 {

private int id;
private String name;
private int tid;
}
public class Teacher2 {

private int id;
private String name;
private List<Student2> students;
}
 <select id="getTeacher2" resultMap="teacherStu2">
select * from teacher where id = #{id}
</select>
<resultMap id="teacherStu2" type="com.me.domain.Teacher2" >
<result property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" javaType="ArrayList" ofType="com.me.domain.Student2" select="getStudentByTid" column="id"/>
</resultMap>
<select id="getStudentByTid" resultType="com.me.domain.Student2">
select * from student where tid = #{id}
</select>
public void getTeacher2(){

SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher2 teacher = mapper.getTeacher2(1);
System.out.println(teacher.toString());
sqlSession.close();
}

**

3、 ... and 、 Many to many associated query

1、 database
 Insert picture description here
 Insert picture description here
 Insert picture description here
2、 Entity class

import java.util.List;
public class Classes {

private Integer cid;
private String cname;
private List<Teacher> teachers;
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 List<Teacher> getTeachers() {

return teachers;
}
public void setTeachers(List<Teacher> teachers) {

this.teachers = teachers;
}
@Override
public String toString() {

return "Classes [cid=" + cid + ", cname=" + cname + ", teachers=" + teachers + "]";
}
}
import java.util.List;
public class Teacher {

private Integer tid;
private String tname;
private List<Classes> classes;
public Integer getTid() {

return tid;
}
public void setTid(Integer tid) {

this.tid = tid;
}
public String getTname() {

return tname;
}
public void setTname(String tname) {

this.tname = tname;
}
public List<Classes> getClasses() {

return classes;
}
public void setClasses(List<Classes> classes) {

this.classes = classes;
}
@Override
public String toString() {

return "Teacher [tid=" + tid + ", tname=" + tname + "]";
}
}

3、xml File mapping

<resultMap type="com.po.Classes" id="ClassesWithTeacherResult">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<!-- Multi table association mapping -->
<collection property="teachers" ofType="com.po.Teacher">
<id property="tid" column="tid"/>
<result property="tname" column="tname"/>
</collection>
</resultMap>
<select id="findClassesWithTeacher" parameterType="Integer" resultMap="ClassesWithTeacherResult">
select * from t_classes c,t_teacher t,t_classessTeacher ct
where ct.cid=c.cid
and ct.tid=t.tid
and c.cid=#{cid}
</select>

Many to many association query reference link : https://blog.csdn.net/dwenxue/article/details/82108178

版权声明
本文为[Princess SINB]所创,转载请带上原文链接,感谢
https://javamana.com/2021/04/20210408034310616d.html

  1. Spring Boot 中如何统一 API 接口响应格式?
  2. 深入分析 SpringMVC 参数解析器
  3. SpringBoot 中如何自定义参数解析器?
  4. SpringMVC 初始化流程分析
  5. 如何优雅的实现 Spring Boot 接口参数加密解密?
  6. Spring5 里边的新玩法!这种 URL 请求让我涨见识了!
  7. SpringMVC 中的参数还能这么传递?涨姿势了!
  8. 手把手教你开发 MyBatis 插件
  9. 细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!
  10. Spring Boot 日志各种使用姿势,是时候捋清楚了!
  11. Deep analysis of viewresolver of nine components of spring MVC
  12. In depth analysis of handleradapter of nine components of spring MVC
  13. In depth analysis of handlermapping of nine components of spring MVC
  14. Can spring boot define interface methods to be declared private?
  15. Dispatcherservlet of spring MVC source code analysis
  16. Framework servlet for spring MVC source code analysis
  17. How to unify API response format in spring boot?
  18. In depth analysis of spring MVC parameter parser
  19. How to customize parameter parser in springboot?
  20. Analysis of spring MVC initialization process
  21. How to realize encryption and decryption of spring boot interface parameters gracefully?
  22. A new way to play in spring 5! This kind of URL request makes me see better!
  23. Can parameters in spring MVC be passed like this? It's up!
  24. Hand in hand to teach you how to develop mybatis plug-ins
  25. Fine spring boot + thymeleaf, there are so many fun details!
  26. Spring boot logs all kinds of posture, it's time to clear!
  27. Web 3.0踏浪而来,分布式存储举足轻重|时空云邀请您参加Web3.0中国峰会暨分布式存储行业大会
  28. spring-aop 进不了切面方法的解决办法
  29. Web 3.0 is coming, distributed storage is very important | spatiotemporal cloud invites you to attend Web3.0 China Summit and distributed storage industry conference
  30. The solution of spring AOP can't enter the section method
  31. Linux中如何启用root用户
  32. How to enable root in Linux
  33. 踩坑 MySQL 索引,看看你真的会用吗?
  34. Hive优化之配置参数的优化(一)
  35. Step on the MySQL index to see if you really know how to use it?
  36. Optimization of configuration parameters for hive optimization (1)
  37. Linux入门教程资料分享
  38. Introduction to Linux
  39. 外部连接mysql docker容器异常
  40. Exception of external connection MySQL docker container
  41. Zookeeper分布式锁?
  42. Zookeeper distributed lock?
  43. 嵌入式Linux_Framebuffer_03点阵显示ASCII字符串
  44. 嵌入式Linux_Framebuffer_02字符编码
  45. Embedded Linux_ Framebuffer_ 03 dot matrix display ascii string
  46. Embedded Linux_ Framebuffer_ 02 character encoding
  47. Looking forward to new product launch of Xiaomi in spring CNMO takes you to see 11 new products in advance
  48. An inventory of the commonly used garbage collectors in Java
  49. Why is it so easy to get started with HBase?
  50. Implementation of PRC framework based on netty
  51. 2021 Java back end engineer must know knowledge - (Dubbo, distributed RPC framework)
  52. 关于spring advisor和元数据 同时来管理事务的问题
  53. How to manage transactions with spring advisor and metadata at the same time
  54. 使用Playwright对Java API实现自动视觉测试 - applitools
  55. Using playwright to implement automatic visual testing for Java API - applitools
  56. Dubbo和Spring cloud、Istio对比图
  57. Comparison of Dubbo with spring cloud and istio
  58. HttpServletRequest、通过request获得请求头、请求体等、解决中文乱码等问题
  59. Mybatis学习笔记-一对一,一对多,多对多关联查询
  60. Mybatis学习笔记-基本概念与操作