Mybatis 6: one to many association query

itread01 2021-01-22 12:32:02
mybatis association query


### Welcome to my GitHub[https://github.com/zq2599/blog_demos](https://github.com/zq2599/blog_demos) Content : All original articles classified summary and supporting original code , involve Java、Docker、Kubernetes、DevOPS etc. ;### An overview of this article - This article is about 《MyBatis Primary combat 》 Part 6 of the series , Continue to practice getting data from multiple tables ;- Looking back on the last article , We have a real one-to-one relationship with multiple tables , As shown in the figure below , When querying log records , Find out the corresponding user information :![ Insert picture description here ](https://img2020.cnblogs.com/other/485422/202101/485422-20210122074747422-1942973624.jpg)- What this article wants to practice is the one to many relationship : When querying user records , Find out all the log records of the user , The logical relationship is shown in the figure below :![ Insert picture description here ](https://img2020.cnblogs.com/other/485422/202101/485422-20210122074747969-942405540.jpg)- When implementing one to many query with specific coding , We use join table and nest form to realize , Each method follows the steps in the figure below :![ Insert picture description here ](https://img2020.cnblogs.com/other/485422/202101/485422-20210122074748321-597931846.jpg)### Source code download 1. If you don't want to code , Can be in GitHub Download all the source code , The address and link information is shown in the table below (https://github.com/zq2599/blog_demos):| Name | Link to | Note || :-------- | :----| :----|| Project home page | https://github.com/zq2599/blog_demos | The project is in GitHub Home page on || git Warehouse address (https)| https://github.com/zq2599/blog_demos.git | The warehouse address of the project source code ,https Agreement || git Warehouse address (ssh)| [email protected]:zq2599/blog_demos.git | The warehouse address of the project source code ,ssh Agreement |2. This git There are multiple folders in the project , The application of this chapter is in mybatis Under folder , As shown in the red box below :![ Insert picture description here ](https://img2020.cnblogs.com/other/485422/202101/485422-20210122074748619-1022109746.png)3. mybatis It's a parent project , There are several sub projects in it , The source code of this article is relatedoperation In the subproject , As shown in the red box below :![ Insert picture description here ](https://img2020.cnblogs.com/other/485422/202101/485422-20210122074749233-1971121638.jpg)### Prepare information 1. This actual battle , In the name of mybatis Create two tables in the database of ( As like as two peas in the previous articles ):user and log surface ;2. user Table records user information , Very simple , There are only three fields : Primary key 、 Name 、 Age 3. log Tables record user behavior , Four fields : Primary key 、 Users id、 Behavior description 、 Behavior time 4. user and log The relationship is shown in the figure below :![ Insert picture description here ](https://img2020.cnblogs.com/other/485422/202101/485422-20210122074749693-1745658186.jpg)5. The statements for creating tables and adding data are as follows :```sqluse mybatis;DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `id` int(32) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `age` int(32) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `log`;CREATE TABLE `log` ( `id` int(32) NOT NULL AUTO_INCREMENT, `user_id` int(32), `action` varchar(255) NOT NULL, `create_time` datetime not null, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;INSERT INTO mybatis.user (id, name, age) VALUES (3, 'tom', 11);INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (3, 3, 'read book', '2020-08-07 08:18:16');INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (4, 3, 'go to the cinema', '2020-09-02 20:00:00');INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (5, 3, 'have a meal', '2020-10-05 12:03:36');INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (6, 3, 'have a sleep', '2020-10-06 13:00:12');INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (7, 3, 'write', '2020-10-08 09:21:11');```### About two ways of multi table Association query - There are two kinds of implementation of multi table associated query: linked table and nested query , The difference between them is Mybatis It's reflected in resultMap On the definition of :1. When linking tables ,resultMap Internal use collection Child node , Map the result of join table query to the collection of associated objects ;2. Nest time ,resultMap Internal use association Child node ,association Of select Property to trigger a new query ;- The above two methods can get the query results successfully , Next, try one by one ;### Joint table query 1. This article continues to use the subproject established in the previous article relatedoperation;2. Entity class UserWithLogs.java as follows , Visible member variables logs Is used to store all the logs of the user :```javapackage com.bolingcavalry.relatedoperation.entity;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import lombok.NoArgsConstructor;import java.util.List;@Data@NoArgsConstructor@ApiModel(description = " User entity class ( Including behavior log collection )")public class UserWithLogs { @ApiModelProperty(value = " Users ID") private Integer id; @ApiModelProperty(value = " User name ", required = true) private String name; @ApiModelProperty(value = " User address ", required = false) private Integer age; @ApiModelProperty(value = " Behavior log ", required = false) priv
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210122122913974t.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云