One: the preparatory work
1. Building Data
CREATE TABLE Boss (b_id INT (one) PRIMARY KEY auto_increment not null,b_name VARCHAR (255)); CREATE TABLE Manager (m_id INT) PRIMARY KEY auto_increment not null,m_name VARCHAR (255)); INSERT into Boss (B_name) VALUES (' Tom '); INSERT into Manager (M_name) VALUES (' Mgr1 '), insert into Manager (M_name) VALUES (' Mgr2 '), insert into manager (m_ Name) VALUES (' Mgr3 ');
2. Establishing the corresponding entity class
Package Com.mybatis.entities;public class Manager {private Integer id;private String name;//provides the corresponding Getter/setter method//provides T Ostring Method}================================package Com.mybatis.entities;import Java.util.list;public class Boss { Private Integer id;private String name;private list<manager> mgrs;//provide the appropriate Getter/setter method//Provide toString method}
3. Create a corresponding SQL mapping file
Only the boss is queried here and then all the managers under the boss are identified, so only the boss mapping file is established.
Two: Test
1. Write the SQL statement in Bossmapper.xml, and the corresponding configuration
<?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=" Com.mybatis.entities.BossMapper "><!-- Query methods There are two types of, ::1. nested results, which use nested result mappings to process subsets of duplicate federated results to encapsulate data from a table query (removing duplicate data) 2. Nested query: Returns the expected complex type by executing another SQL mapping statement --> <!-- test nested results --> <!-- Because there is an object in the return value, only the alias is used to map the unavailable, This uses a custom return value type to map --> <select id= "GetBoss1" parametertype= "int" resultmap= "Getboss1map" > SELECT * FROM boss b, manager m where b.b_id=m.b_id and b.b_id=#{b_id} </select> <!-- id: must correspond to the resultmap= "Getstundet1map" above type: Specify the return value type, can be understood as select label resulttype --> <resultmap type= "Boss" id= "GetBoss1Map" >&Nbsp;<id column= "b_id" property= "id"/> <result column= "b_name" property= " Name "/> <!-- oftype Specifies the object type in the students collection --> <collection property=" Mgrs " oftype= "Manager" > <id column= "m_id" property= "id"/> <result column= "M_name" property= "name"/> </collection> </resultmap> <!-- test Nested queries --> <!-- Because there is an object in the return value, so only the alias is used to map the unavailable, This uses a custom return value type to map --> <select id= "GetBoss2" parametertype= "int" resultmap= "Getboss2map" > select b_id, b_name FROM boss WHERE b_id=#{b_id} </select> <!-- Use aliases to map attributes in entity classes --> <select id= "GetManager" parametertype= "int" Resulttype= "Teacher" > SELECT m_id id, m_name name FROM manager Where b_id=#{b_id} </seleCt> <resultmap type= "Boss" id= "Getboss2map" > <id column= "b_id" property= "id"/> <result column= "b_name" property= "name"/> <collection Property= "Mgrs" column= "b_id" oftype= "Manager" select= "GetManager" ></collection> </resultMap> </mapper>
2. Code Testing
Package Com.mybatis.test;import Org.apache.ibatis.session.sqlsession;import Org.junit.test;import Com.mybatis.entities.boss;import Com.mybatis.util.mybatisutil;public class Testboss {@Testpublic void TestGetBoss1 () {sqlsession session = Mybatisutil.getsqlsession (); String statement = "Com.mybatis.entities.BossMapper.getBoss1"; Boss Boss = Session.selectone (statement, 1); SYSTEM.OUT.PRINTLN (boss);} @Testpublic void TestGetBoss2 () {sqlsession session = Mybatisutil.getsqlsession (); String statement = "Com.mybatis.entities.BossMapper.getBoss2"; Boss Boss = Session.selectone (statement, 1); SYSTEM.OUT.PRINTLN (Boss);}}
Complete the test!
This blog post source code click Http://pan.baidu.com/s/1i3LPOP3 Download!
This article is from the "focus on Java,linux Technology" blog, please be sure to keep this source http://wuqinglong.blog.51cto.com/9087037/1726152
7. MyBatis one-to-many queries