1, One to many

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

1) Table structure


-- Countrycountry 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;

-- peoplepeople 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; // ellipsisget 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; // ellipsisget 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


<!-- Method1 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> <!-- Method2 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 oneselect

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

property: Specify association properties, NamelyCountry 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 multiplenamespace Of. When used, Add thenamespace that will do

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

5) Test output
One way: [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, Yui Aragaki [TRACE] <==
Row: 3, Japan, 5, Kaede Matsushima [DEBUG] <== Total: 2 Country [cid=3, cname= Japan,
peoples=[People [pid=5, pname= Kaede Matsushima, countryId=null], People [pid=1, pname= Yui Aragaki,
countryId=null]]]

Mode two [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; // ellipsisget
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;
// ellipsisget set Attribute method @Override public String toString() { return "People [pid=" +
pid + ", pname=" + pname + ", countryId=" + countryId + ", country=" + country
+ "]"; } }
The country is goneSet<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


<!-- Method1 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> <!-- Method2 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> One way<association /> The label shows2 Relationships between entity objects, One for one.


property: Specify association properties, NamelyPeople Classcountry attribute

javaType: Type of associated property

4) Test output


One way: [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, Kaede Matsushima, 3, Japan [DEBUG] <==
Total: 1 People [pid=5, pname= Kaede Matsushima, countryId=null, country=Country [cid=3,
cname= Japan]] Mode two: [DEBUG] ==> Preparing: select pid,pname,countryId from people
where pid = ? [DEBUG] ==> Parameters: 5(Integer) [TRACE] <== Columns: pid,
pname, countryId [TRACE] <== Row: 5, Kaede 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= Kaede Matsushima, countryId=null, country=Country
[cid=3, cname= Japan]]



Remarks:

Country Class can not be deletedSet<People>peoples ; Member variables.

People Class can always haveCountrycountry; Member variables.

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