Mybatis [14] - how does mybatis implement one to many query?

InfoQ 2021-01-23 22:11:38
mybatis mybatis implement query


{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" notes : Code is hosted on GitHub On , The address is :","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"https://github.com/Damaer/Mybatis-Learning","attrs":{}}],"attrs":{}},{"type":"text","text":" , The project is ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mybatis-10-one2many","attrs":{}}],"attrs":{}},{"type":"text","text":", You need to take it yourself , Need configuration ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"maven","attrs":{}}],"attrs":{}},{"type":"text","text":" The environment and ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysql","attrs":{}}],"attrs":{}},{"type":"text","text":" Environmental Science (","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sql","attrs":{}}],"attrs":{}},{"type":"text","text":" Statements in ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"resource","attrs":{}}],"attrs":{}},{"type":"text","text":" Under the ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"test.sql","attrs":{}}],"attrs":{}},{"type":"text","text":" in ), If you think it's useful, you can order a little star .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"docsify","attrs":{}}],"attrs":{}},{"type":"text","text":" The document address is :","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"https://damaer.github.io/Mybatis-Learning/#/","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" A lot of times , When the query condition involves multiple tables with association relationship , You need to use association queries , There are four kinds of association queries .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - One to one association query ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - One to many associated queries ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - Many to one association query ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - Many to many associated query ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Next, we need to practice one to many association query , The so-called one to many is that the attributes in an object are a collection of objects . For example, each country has several leaders . The corresponding country table and the leader's table .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/25/25d21c5e791d7acb9c38d545980d9c16.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/48/483a4f05ed6bd42bc3e48f9808c1bf7a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Create data table ","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# Create database \nCREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;\n# Create data table \nCREATE TABLE `test`.`country` ( `cid` INT(10) NOT NULL AUTO_INCREMENT ,`cname` VARCHAR(20) NOT NULL ,PRIMARY KEY(`cid`)) ENGINE = MyISAM;\nCREATE TABLE `test`.`minister` ( `mid` INT(10) NOT NULL AUTO_INCREMENT ,`mname` VARCHAR(20) NOT NULL ,`countryId` INT(20) NOT NULL ,PRIMARY KEY(`mid`)) ENGINE = MyISAM;\n\n# Initialize data table \nINSERT INTO `country` (`cid`, `cname`) VALUES ('1', 'USA');\nINSERT INTO `country` (`cid`, `cname`) VALUES ('2', 'England');\n\nINSERT INTO `minister` (`mid`, `mname`, `countryId`) VALUES ('1', 'aaa', '1');\nINSERT INTO `minister` (`mid`, `mname`, `countryId`) VALUES ('2', 'bbb', '1');\nINSERT INTO `minister` (`mid`, `mname`, `countryId`) VALUES ('3', 'ccc', '2');\nINSERT INTO `minister` (`mid`, `mname`, `countryId`) VALUES ('4', 'ddd', '2');\nINSERT INTO `minister` (`mid`, `mname`, `countryId`) VALUES ('5', 'eee', '2');","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Entity class ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"country","attrs":{}}],"attrs":{}},{"type":"text","text":" The corresponding entity class , Attributes are :","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cid","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cname","attrs":{}}],"attrs":{}},{"type":"text","text":", as well as ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"miniters","attrs":{}}],"attrs":{}},{"type":"text","text":", Realization ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"get()","attrs":{}}],"attrs":{}},{"type":"text","text":" as well as ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"set()","attrs":{}}],"attrs":{}},{"type":"text","text":" Method ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tostring()","attrs":{}}],"attrs":{}},{"type":"text","text":" Method :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"import java.util.Set;\npublic class Country {\n\tprivate Integer cid;\n\tprivate String cname;\n\tprivate Set ministers;\n\n\tpublic Integer getCid() {\n\t\treturn cid;\n\t}\n\tpublic void setCid(Integer cid) {\n\t\tthis.cid = cid;\n\t}\n\tpublic String getName() {\n\t\treturn cname;\n\t}\n\tpublic void setName(String cname) {\n\t\tthis.cname = cname;\n\t}\n\tpublic Set getMinisters() {\n\t\treturn ministers;\n\t}\n\tpublic void setMinisters(Set ministers) {\n\t\tthis.ministers = ministers;\n\t}\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Country [cid=\" + cid + \", cname=\" + cname + \", ministers=\"\n\t\t\t\t+ ministers + \"]\";\n\t}\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Minister","attrs":{}}],"attrs":{}},{"type":"text","text":" The entity class , It also needs to be realized ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"set()","attrs":{}}],"attrs":{}},{"type":"text","text":" as well as ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"get()","attrs":{}}],"attrs":{}},{"type":"text","text":" Method ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"toString()","attrs":{}}],"attrs":{}},{"type":"text","text":" Method :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Minister {\n\tprivate Integer mid;\n\tprivate String mname;\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Minister [mid=\" + mid + \", mname=\" + mname + \"]\";\n\t}\n\tpublic Integer getMid() {\n\t\treturn mid;\n\t}\n\tpublic void setMid(Integer mid) {\n\t\tthis.mid = mid;\n\t}\n\tpublic String getMname() {\n\t\treturn mname;\n\t}\n\tpublic void setMname(String mname) {\n\t\tthis.mname = mname;\n\t}\n\t\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" stay ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mybatis.xml","attrs":{}}],"attrs":{}},{"type":"text","text":" It's registered in the file ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mapper","attrs":{}}],"attrs":{}},{"type":"text","text":" file :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":" \n \n \n ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" We operate the database interface :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface ICountryDao {\n\tCountry selectCountryById(int cid);\n\tCountry selectCountryById2(int cid);\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mapper.xml","attrs":{}}],"attrs":{}},{"type":"text","text":" file , There are two corresponding ways to realize one to many query :","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" One is nested queries ( Query multiple tables separately ), That is, there is an entrance ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" sentence , But this sentence only selects ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"country","attrs":{}}],"attrs":{}},{"type":"text","text":" Information about , stay ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"resultMap","attrs":{}}],"attrs":{}},{"type":"text","text":" It's customized , Including a ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"","attrs":{}}],"attrs":{}},{"type":"text","text":", Specify the corresponding type in it ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"property","attrs":{}}],"attrs":{}},{"type":"text","text":" It's the name of the attribute ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ofType","attrs":{}}],"attrs":{}},{"type":"text","text":" It's the type of this genus ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" Specify another... To call ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" sentence ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"column","attrs":{}}],"attrs":{}},{"type":"text","text":" It's the input parameter , This way you can achieve delayed loading , That's when it's not available , It won't call the ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SQL","attrs":{}}],"attrs":{}},{"type":"text","text":". In this case, multiple tables can be queried individually across multiple ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mapper","attrs":{}}],"attrs":{}},{"type":"text","text":" file , Just write the corresponding ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"namespace","attrs":{}}],"attrs":{}},{"type":"text","text":" That's all right. ","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Results nested query ( Multi table join query ): There is also an entrance ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" sentence , What is different from the above is , This ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" Statement selects the fields of both tables , We need to specify one ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"resultMap","attrs":{}}],"attrs":{}},{"type":"text","text":", Just like other sub tags , however ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"","attrs":{}}],"attrs":{}},{"type":"text","text":" It doesn't call the other ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sql","attrs":{}}],"attrs":{}},{"type":"text","text":" sentence , Just specify the corresponding attribute and field .","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n\n\n\n\t\n\t\n\t\n\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\n\t\n\n \n \n \n \n \n \n \n \n \n \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" test test:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class MyTest {\n private ICountryDao dao;\n private SqlSession sqlSession;\n @Before\n public void Before(){\n sqlSession=MyBatisUtils.getSqlSession();\n dao=sqlSession.getMapper(ICountryDao.class);\n }\n @Test\n public void TestselectCountryById(){\n Country country=dao.selectCountryById(1);\n System.out.println(country);\n }\n\n @Test\n public void TestselectCountryById2(){\n Country country=dao.selectCountryById2(1);\n System.out.println(country);\n }\n\n\n @After\n public void after(){\n if(sqlSession!=null){\n sqlSession.close();\n }\n }\n\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Tool classes used :","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MybatisUtils.java","attrs":{}}],"attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class MyBatisUtils {\n static private SqlSessionFactory sqlSessionFactory;\n static public SqlSession getSqlSession() {\n InputStream is;\n try {\n is = Resources.getResourceAsStream(\"mybatis.xml\");\n if (sqlSessionFactory == null) {\n sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);\n }\n return sqlSessionFactory.openSession();\n } catch (IOException e) {\n // TODO Auto-generated catch block\n e.printStackTrace();\n }\n return null;\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The results of the two interfaces are consistent :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"[service] 2018-07-12 14:01:15,835 - org.apache.ibatis.transaction.jdbc.JdbcTransaction -508 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4abdb505]\n[service] 2018-07-12 14:01:15,837 - dao.ICountryDao.selectCountryById2 -510 [main] DEBUG dao.ICountryDao.selectCountryById2 - ==> Preparing: select * from country c,minister m where c.cid = m.countryId and c.cid= ? \n[service] 2018-07-12 14:01:15,869 - dao.ICountryDao.selectCountryById2 -542 [main] DEBUG dao.ICountryDao.selectCountryById2 - ==> Parameters: 1(Integer)\n[service] 2018-07-12 14:01:15,884 - dao.ICountryDao.selectCountryById2 -557 [main] DEBUG dao.ICountryDao.selectCountryById2 - <== Total: 2\nCountry [cid=1, cname=USA, ministers=[Minister [mid=2, mname=bbb], Minister [mid=1, mname=aaa]]]","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"【 Author's brief introduction 】","attrs":{}},{"type":"text","text":": ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Qin Huai , official account 【","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Qin Huai grocery store ","attrs":{}},{"type":"text","text":"】 author , The road to technology is not in the moment , one's nobility lasts forever , Even slowly , Go on and on . The world wants everything to be quick , faster , But I hope I can take every step well , Write every article well , Looking forward to communicating with you .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210123221032341k.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课程百度云