1, One to many

for instance : There are many people in a country . One to many

1) Table structure


-- country country CREATE TABLE `country` ( `cid` int(5) NOT NULL AUTO_INCREMENT,
`cname` varchar(20) NOT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- people people CREATE TABLE `people` ( `pid` int(5) NOT NULL AUTO_INCREMENT,
`pname` varchar(20) NOT NULL, `countryId` int(5) NOT NULL, PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
In the database , Which watch has foreign keys in it , It's all about parties
2) Entity class
// country public class Country { private Integer cid; private String cname; private
Set<People> peoples; // ellipsis get set Attribute method @Override public String toString() {
return "Country [cid=" + cid + ", cname=" + cname + ", peoples=" + peoples +
"]"; } } // people public class People { private Integer pid; private String pname;
private Integer countryId; // ellipsis get set Attribute method @Override public String toString() {
return "People [pid=" + pid + ", pname=" + pname + ", countryId=" + countryId +
"]"; } }

3)Dao Interface
public interface ICountryDao { Country selectById(int id); Country
selectById2(int id); }
4)mapper.xml file


<!-- method 1 Multi table connection query method --> <resultMap type="Country" id="countryMapper"> <id
column="cid" property="cid"/> <result column="cname" property="cname"/>
<collection property="peoples" ofType="People"> <id column="pid"
property="pid"/> <result column="pname" property="pname"/> </collection>
</resultMap> <select id="selectById" resultMap="countryMapper"> select
cid,cname,pid,pname from country,people where cid=countryId and cid = #{xxx}
</select> <!-- method 2 Multi table single query method --> <select id="selectPeople" resultType="People">
select pid,pname from people where countryId=#{ooo} </select> <resultMap
type="Country" id="countryMapper2"> <id column="cid" property="cid"/> <result
column="cname" property="cname"/> <collection property="peoples"
ofType="People" select="selectPeople" column="cid" /> </resultMap> <select
id="selectById2" resultMap="countryMapper2"> select cid,cname from country
where cid = #{xxx} </select>
The first method is to connect multiple tables first , Query after connecting to a table . Its query essence is a table . There's only one select

<collection /> It's a collection , There are multiple objects .

property: Specify association properties , Namely Country Collection properties in class

ofType: Generic types of collection properties

Method 2: combine the query results of the main table with those of other tables , Encapsulate as an object . Data in query results of main table , As criteria for other table queries .

These queries can span multiple mapping files , It can span multiple namespace Of . When in use , Add the namespace that will do

Association properties <collection /> Data from another query selectPeople, This query <selectPeople /> Dynamic parameters of
countryId=#{ooo} The value of comes from the query <selectById2 /> Query result fields for cid

5) Test output
Mode 1 : [DEBUG] ==> Preparing: select cid,cname,pid,pname from country,people
where cid=countryId and cid = ? [DEBUG] ==> Parameters: 3(Integer) [TRACE] <==
Columns: cid, cname, pid, pname [TRACE] <== Row: 3, Japan , 1, Xinyuan clothing [TRACE] <==
Row: 3, Japan , 5, Maple Matsushima [DEBUG] <== Total: 2 Country [cid=3, cname= Japan ,
peoples=[People [pid=5, pname= Maple Matsushima , countryId=null], People [pid=1, pname= Xinyuan clothing ,
countryId=null]]]

Mode 2 [DEBUG] ==> Preparing: select cid,cname from country where cid = ? [DEBUG]
==> Parameters: 2(Integer) [TRACE] <== Columns: cid, cname [TRACE] <== Row: 2,
U.S.A [DEBUG] ====> Preparing: select pid,pname from people where countryId=?
[DEBUG] ====> Parameters: 2(Integer) [TRACE] <==== Columns: pid, pname [TRACE]
<==== Row: 3, Annie · Hathaway [DEBUG] <==== Total: 1 [DEBUG] <== Total: 1 Country
[cid=2, cname= U.S.A , peoples=[People [pid=3, pname= Annie · Hathaway , countryId=null]]]
2, Many to one



conversely , Many people to one country . Everyone is only for one country . Of course, we don't think about the bickering of dual nationality .

When querying multiple objects , One by one . therefore : The implementation of one-to-one associated query is the same as that of many to one .

1) Entity class slightly changed :


// country public class Country { private Integer cid; private String cname; // ellipsis get
set Attribute method @Override public String toString() { return "Country [cid=" + cid + ",
cname=" + cname + "]"; } } // people public class People { private Integer pid;
private String pname; private Integer countryId; private Country country;
// ellipsis get set Attribute method @Override public String toString() { return "People [pid=" +
pid + ", pname=" + pname + ", countryId=" + countryId + ", country=" + country
+ "]"; } }
The country is gone Set<People> Human member variable , People have state member variables

2)Dao Interface

public interface IPeopleDao { People selectById(int id); People
selectById2(int id); }
3)mapper.xml configuration file


<!-- method 1 Multi table connection query method --> <resultMap type="People" id="peopleMapper"> <id
column="pid" property="pid"/> <result column="pname" property="pname"/>
<association property="country" javaType="Country"> <id column="cid"
property="cid"/> <result column="cname" property="cname"/> </association> <!--
We can do it in a collective way , A collection is nothing more than a collection of countries --> <!-- <collection property="country"
ofType="Country"> <id column="cid" property="cid"/> <result column="cname"
property="cname"/> </collection> --> </resultMap> <select id="selectById"
resultMap="peopleMapper"> select pid,pname,cid,cname from people,country where
pid = #{xxx} and countryId=cid </select> <!-- method 2 Multi table single query method --> <select
id="selectCountry" resultType="Country"> select cid,cname from country where
cid=#{ooo} </select> <resultMap type="People" id="peopleMapper2"> <id
column="pid" property="pid"/> <result column="pname" property="pname"/>
<association property="country" javaType="Country" select="selectCountry"
column="countryId" /> </resultMap> <select id="selectById2"
resultMap="peopleMapper2"> select pid,pname,countryId from people where pid =
#{xxx} </select> Mode 1 <association /> The label shows 2 Relationships between entity objects , One for one .


property: Specify association properties , Namely People In class country attribute

javaType: Type of associated property

4) Test output


Mode 1 : [DEBUG] ==> Preparing: select pid,pname,cid,cname from people,country
where pid = ? and countryId=cid [DEBUG] ==> Parameters: 5(Integer) [TRACE] <==
Columns: pid, pname, cid, cname [TRACE] <== Row: 5, Maple Matsushima , 3, Japan [DEBUG] <==
Total: 1 People [pid=5, pname= Maple Matsushima , countryId=null, country=Country [cid=3,
cname= Japan ]] Mode 2 : [DEBUG] ==> Preparing: select pid,pname,countryId from people
where pid = ? [DEBUG] ==> Parameters: 5(Integer) [TRACE] <== Columns: pid,
pname, countryId [TRACE] <== Row: 5, Maple Matsushima , 3 [DEBUG] ====> Preparing: select
cid,cname from country where cid=? [DEBUG] ====> Parameters: 3(Integer) [TRACE]
<==== Columns: cid, cname [TRACE] <==== Row: 3, Japan [DEBUG] <==== Total: 1
[DEBUG] <== Total: 1 People [pid=5, pname= Maple Matsushima , countryId=null, country=Country
[cid=3, cname= Japan ]]



remarks :

Country Class can not be deleted Set<People>peoples ; Member variable .

People Class can always have Countrycountry; Member variable .

Removed for ease of understanding one to many , Many to one