The so-called self association refers to, I'm on my own, And act as a multi-party. In fact, it's ordinary1:n and n:1 Deformation

1, One to many

1) Table structure


-- News column CREATE TABLE `news` ( `id` int(5) NOT NULL AUTO_INCREMENT, `name`
varchar(10) NOT NULL, `pid` int(5) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;



2) Entity class


// News column: The current news column is seen as one side, Parent column public class News { private Integer id; private
String name; private Set<News> childen; // ellipsisget set Attribute method @Override public String
toString() { return "News [id=" + id + ", name=" + name + ", childen=" +
childen + "]"; } }
3)Dao Interface




public interface INewsDao { List<News> selectById(int id);// Query assignmentid All descendant menus below the column
News selectById2(int id);// Query assignmentid Column and all descendant menus below }
4)mapper.xml file




<!-- Query assignmentid All descendant menus below the column --> <resultMap type="News" id="newsMapper"> <id
column="id" property="id"/> <result column="name" property="name"/> <collection
property="childen" ofType="News" select="selectById" column="id"/> </resultMap>
<select id="selectById" resultMap="newsMapper"> select id,name from news where
pid = #{xxx} </select> <!-- Query assignmentid column and All descendant menus below --> <select id="selectChilden"
resultMap="newsMapper"> select id,name from news where pid=#{ooo} </select>
<resultMap type="News" id="newsMapper2"> <id column="id" property="id"/>
<result column="name" property="name"/> <collection property="childen"
ofType="News" select="selectChilden" column="id" /> </resultMap> <select
id="selectById2" resultMap="newsMapper2"> select id,name from news where
id=#{xxx} </select>
Label properties can be referred to:

MyBatis Relational query ( One to many, Many to one)
<http://blog.csdn.net/abc5232033/article/details/79054247>


5) Test output:


One, [DEBUG] ==> Preparing: select id,name from news where pid = ? [DEBUG] ==>
Parameters: 1(Integer) [TRACE] <== Columns: id, name [TRACE] <== Row: 3, Entertainment in the mainland
[DEBUG] ====> Preparing: select id,name from news where pid = ? [DEBUG] ====>
Parameters: 3(Integer) [DEBUG] <==== Total: 0 [TRACE] <== Row: 4, RTHK Entertainment [DEBUG]
====> Preparing: select id,name from news where pid = ? [DEBUG] ====>
Parameters: 4(Integer) [DEBUG] <==== Total: 0 [DEBUG] <== Total: 2 News [id=3,
name= Entertainment in the mainland, childen=[]] News [id=4, name= RTHK Entertainment, childen=[]] Two, [DEBUG] ==>
Preparing: select id,name from news where id=? [DEBUG] ==> Parameters:
2(Integer) [TRACE] <== Columns: id, name [TRACE] <== Row: 2, sports news [DEBUG] ====>
Preparing: select id,name from news where pid=? [DEBUG] ====> Parameters:
2(Integer) [TRACE] <==== Columns: id, name [TRACE] <==== Row: 5, Basketball [DEBUG]
======> Preparing: select id,name from news where pid = ? [DEBUG] ======>
Parameters: 5(Integer) [TRACE] <====== Columns: id, name [TRACE] <====== Row:
7, NBA [DEBUG] ========> Preparing: select id,name from news where pid = ?
[DEBUG] ========> Parameters: 7(Integer) [DEBUG] <======== Total: 0 [TRACE]
<====== Row: 8, CBA [DEBUG] ========> Preparing: select id,name from news where
pid = ? [DEBUG] ========> Parameters: 8(Integer) [DEBUG] <======== Total: 0
[DEBUG] <====== Total: 2 [TRACE] <==== Row: 6, Football [DEBUG] ======> Preparing:
select id,name from news where pid = ? [DEBUG] ======> Parameters: 6(Integer)
[TRACE] <====== Columns: id, name [TRACE] <====== Row: 9, Serie A [DEBUG] ========>
Preparing: select id,name from news where pid = ? [DEBUG] ========> Parameters:
9(Integer) [TRACE] <======== Columns: id, name [TRACE] <======== Row: 12, AC Milan
[DEBUG] ==========> Preparing: select id,name from news where pid = ? [DEBUG]
==========> Parameters: 12(Integer) [TRACE] <========== Columns: id, name
[TRACE] <========== Row: 14, Pirlo [DEBUG] ============> Preparing: select
id,name from news where pid = ? [DEBUG] ============> Parameters: 14(Integer)
[DEBUG] <============ Total: 0 [DEBUG] <========== Total: 1 [TRACE] <========
Row: 13, International Milan [DEBUG] ==========> Preparing: select id,name from news where pid
= ? [DEBUG] ==========> Parameters: 13(Integer) [DEBUG] <========== Total: 0
[DEBUG] <======== Total: 2 [TRACE] <====== Row: 10, Spanish Liga [DEBUG] ========>
Preparing: select id,name from news where pid = ? [DEBUG] ========> Parameters:
10(Integer) [DEBUG] <======== Total: 0 [TRACE] <====== Row: 11, China Super League [DEBUG]
========> Preparing: select id,name from news where pid = ? [DEBUG] ========>
Parameters: 11(Integer) [DEBUG] <======== Total: 0 [DEBUG] <====== Total: 3
[DEBUG] <==== Total: 2 [DEBUG] <== Total: 1 News [id=2, name= sports news,
childen=[News [id=5, name= Basketball, childen=[News [id=8, name=CBA, childen=[]], News
[id=7, name=NBA, childen=[]]]], News [id=6, name= Football, childen=[News [id=11,
name= China Super League, childen=[]], News [id=10, name= Spanish Liga, childen=[]], News [id=9, name= Serie A,
childen=[News [id=12, name=AC Milan, childen=[News [id=14, name= Pirlo, childen=[]]]],
News [id=13, name= International Milan, childen=[]]]]]]]]

2, Many to one



1) Entity class changed:

Removedprivate Set<News> childen; It should be kept, It's just deleted to make it easier to understand many to one.

Addedprivate News parent; There must be only one parent column

2 If all member variables exist, Implement many to one and one to many on one class


// News column: The current news column is regarded as multi-faceted, Sub column public class News { private Integer id; private
String name; private News parent; // ellipsisget set Attribute method @Override public String
toString() { return "News [id=" + id + ", name=" + name + ", parent=" + parent
+ "]"; } }
2)Dao Interface




public interface INewsDao { News selectById(int id);// Query assignmentid Columns and all their parent menus }
3)mapper.xml file




<!-- Query assignmentid Columns and all their parent menus --> <resultMap type="News" id="newsMapper"> <id
column="id" property="id"/> <result column="name" property="name"/>
<association property="parent" javaType="News" select="selectById" column="pid"
/> </resultMap> <select id="selectById" resultMap="newsMapper"> select
id,name,pid from news where id = #{xxx} </select> Each sub column, There must be only one parent column. So it's a one-to-one relationship, use
<association />

4) Test output:




Test output: [DEBUG] ==> Preparing: select id,name,pid from news where id = ? [DEBUG]
==> Parameters: 12(Integer) [TRACE] <== Columns: id, name, pid [TRACE] <== Row:
12, AC Milan, 9 [DEBUG] ====> Preparing: select id,name,pid from news where id = ?
[DEBUG] ====> Parameters: 9(Integer) [TRACE] <==== Columns: id, name, pid
[TRACE] <==== Row: 9, Serie A, 6 [DEBUG] ======> Preparing: select id,name,pid from
news where id = ? [DEBUG] ======> Parameters: 6(Integer) [TRACE] <======
Columns: id, name, pid [TRACE] <====== Row: 6, Football, 2 [DEBUG] ========>
Preparing: select id,name,pid from news where id = ? [DEBUG] ========>
Parameters: 2(Integer) [TRACE] <======== Columns: id, name, pid [TRACE]
<======== Row: 2, sports news, 0 [DEBUG] ==========> Preparing: select id,name,pid
from news where id = ? [DEBUG] ==========> Parameters: 0(Integer) [DEBUG]
<========== Total: 0 [DEBUG] <======== Total: 1 [DEBUG] <====== Total: 1
[DEBUG] <==== Total: 1 [DEBUG] <== Total: 1 News [id=12, name=AC Milan, parent=News
[id=9, name= Serie A, parent=News [id=6, name= Football, parent=News [id=2, name= sports news,
parent=null]]]]