Mybatis【14】-- Mybatis如何實現一對多查詢?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注:代碼已託管在GitHub上,地址是:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"https://github.com/Damaer/Mybatis-Learning","attrs":{}}],"attrs":{}},{"type":"text","text":" ,項目是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mybatis-10-one2many","attrs":{}}],"attrs":{}},{"type":"text","text":",需要自取,需要配置","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"maven","attrs":{}}],"attrs":{}},{"type":"text","text":"環境以及","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysql","attrs":{}}],"attrs":{}},{"type":"text","text":"環境(","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sql","attrs":{}}],"attrs":{}},{"type":"text","text":"語句在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"resource","attrs":{}}],"attrs":{}},{"type":"text","text":"下的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"test.sql","attrs":{}}],"attrs":{}},{"type":"text","text":"中),覺得有用可以點個小星。","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":"文檔地址在:","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":"很多時候,當查詢條件涉及到具有關聯關係的多個表的時候,需要使用到關聯查詢,關聯查詢一般有四種。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - 一對一關聯查詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - 一對多關聯查詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - 多對一關聯查詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - 多對多關聯查詢","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":"下面我們需要實踐的是一對多關聯查詢,所謂一對多就是一個對象裏面的屬性是一個對象的集合。比如每個國家都有幾個領導。對應的國家表以及領導人的表。","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":"創建數據表","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"#創建數據庫\nCREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;\n#創建數據表\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#初始化數據表\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":"實體類","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":"對應的實體類,屬性有:","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":",以及","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"miniters","attrs":{}}],"attrs":{}},{"type":"text","text":",實現","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"get()","attrs":{}}],"attrs":{}},{"type":"text","text":"以及","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"set()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tostring()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法:","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\[email protected]\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":"的實體類,也需要實現","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"set()","attrs":{}}],"attrs":{}},{"type":"text","text":"以及","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"get()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"toString()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Minister {\n\tprivate Integer mid;\n\tprivate String mname;\n\[email protected]\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":"在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mybatis.xml","attrs":{}}],"attrs":{}},{"type":"text","text":"文件裏面註冊","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mapper","attrs":{}}],"attrs":{}},{"type":"text","text":"文件:","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":"我們操作數據庫的接口:","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":"文件,對應的兩種方式實現一對多查詢:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一種是嵌套查詢(多表單獨查詢),也就是有一個入口的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":"語句,但是這個語句只選出","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"country","attrs":{}}],"attrs":{}},{"type":"text","text":"的信息,在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"resultMap","attrs":{}}],"attrs":{}},{"type":"text","text":"裏面自定義,包括一個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"","attrs":{}}],"attrs":{}},{"type":"text","text":",在裏面指定對應的類型,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"property","attrs":{}}],"attrs":{}},{"type":"text","text":"是屬性的名字,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ofType","attrs":{}}],"attrs":{}},{"type":"text","text":"是這個屬所對應的類型,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" 指定調用的另一個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":"語句,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"column","attrs":{}}],"attrs":{}},{"type":"text","text":"是傳進去的參數,這樣的方式可以實現延遲加載,也就是用不到的時候,不會調用裏面的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SQL","attrs":{}}],"attrs":{}},{"type":"text","text":"。這中多表單獨查詢可以跨多個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mapper","attrs":{}}],"attrs":{}},{"type":"text","text":"文件,只要寫上對應的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"namespace","attrs":{}}],"attrs":{}},{"type":"text","text":"就可以了 ","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果嵌套查詢(多表連接查詢):也有一個入口的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":"語句,與上面不一樣的是,這個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":"語句將兩張表的字段都選擇出來了,我們需要指定一個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"resultMap","attrs":{}}],"attrs":{}},{"type":"text","text":",其他子標籤一樣,但是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"","attrs":{}}],"attrs":{}},{"type":"text","text":"裏面不再調用另外的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sql","attrs":{}}],"attrs":{}},{"type":"text","text":"語句,只是指定了屬性與字段對應就可以了。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\nnPUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\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:","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":"使用到的工具類:","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":"兩個接口的結果一致:","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 [[email protected]]\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":"【作者簡介】","attrs":{}},{"type":"text","text":": ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"秦懷,公衆號【","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"秦懷雜貨店","attrs":{}},{"type":"text","text":"】作者,技術之路不在一時,山高水長,縱使緩慢,馳而不息。這個世界希望一切都很快,更快,但是我希望自己能走好每一步,寫好每一篇文章,期待和你們一起交流。","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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章