一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Mybatis中collection和association的使用區別詳解

Mybatis中collection和association的使用區別詳解

2021-06-17 11:37生活,一半是回憶、一半是 Java教程

這篇文章主要介紹了Mybatis中collection和association的使用區別詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近一直把collection和association弄混,所以為了增強自己的記憶,就擼一個關系出來算是總結罷了

1. 關聯-association
2. 集合-collection

比如同時有user.java和card.java兩個類

user.java如下:

?
1
2
3
4
5
6
7
public class user{
 
private card card_one;
 
private list<card> card_many;
 
}

在映射card_one屬性時用association標簽, 映射card_many時用collection標簽.

所以association是用于一對一和多對一,而collection是用于一對多的關系

下面就用一些例子解釋下吧

association-一對一

人和身份證的關系

下面是pojo

?
1
2
3
4
5
public class card implements serializable{
 private integer id;
 private string code;
//省略set和get方法.
}
?
1
2
3
4
5
6
7
8
9
public class person implements serializable{
 private integer id;
 private string name;
 private string sex;
 private integer age;
 //人和身份證是一對一的關系
 private card card;
//省略set/get方法.
}

下面是mapper和實現的接口

?
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.poji.card;
 
public interface cardmapper {
 card selectcardbyid(integer id);
}
?
1
2
3
4
5
6
7
8
9
<?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.glj.mapper.cardmapper">
 <select id="selectcardbyid" parametertype="int" resulttype="com.glj.poji.card">
 select * from tb_card where id = #{id}
 </select>
</mapper>
?
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.poji.person;
 
public interface personmapper {
 person selectpersonbyid(integer id);
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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.glj.mapper.personmapper">
 <resultmap type="com.glj.poji.person" id="personmapper">
 <id property="id" column="id"/>
 <result property="name" column="name"/>
 <result property="sex" column="sex"/>
 <result property="age" column="age"/>
 <association property="card" column="card_id"
  select="com.glj.mapper.cardmapper.selectcardbyid"
  javatype="com.glj.poji.card">
 </association>
 </resultmap>
 <select id="selectpersonbyid" parametertype="int" resultmap="personmapper">
 select * from tb_person where id = #{id}
 </select>
</mapper>

personmapper.xml 還使用association的分步查詢。

同理多對一,也是一樣

只要那個pojo出現private card card_one;

即使用association

collection 一對多和association的多對一關系

學生和班級的一對多的例子

pojo類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.glj.pojo;
 
import java.io.serializable;
import java.util.list;
 
public class clazz implements serializable{
 private integer id;
 private string code;
 private string name;
    //班級與學生是一對多的關系
 private list<student> students;
//省略set/get方法
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.glj.pojo;
 
import java.io.serializable;
 
public class student implements serializable {
 private integer id;
 private string name;
 private string sex;
 private integer age;
    //學生與班級是多對一的關系
 private clazz clazz;
//省略set/get方法
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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.glj.mapper.clazzmapper">
 <select id="selectclazzbyid" parametertype="int" resultmap="clazzresultmap">
 select * from tb_clazz where id = #{id}
 </select>
 <resultmap type="com.glj.pojo.clazz" id="clazzresultmap">
 <id property="id" column="id"/>
 <result property="code" column="code"/>
 <result property="name" column="name"/>
 <!-- property: 指的是集合屬性的值, oftype:指的是集合中元素的類型 -->
 <collection property="students" oftype="com.glj.pojo.student"
 column="id" javatype="arraylist"
 fetchtype="lazy" select="com.glj.mapper.studentmapper.selectstudentbyclazzid">
  <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="sex" column="sex"/>
  <result property="age" column="age"/>
 </collection>
 </resultmap>
</mapper>
?
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.pojo.clazz;
 
public interface clazzmapper {
 clazz selectclazzbyid(integer id);
}

clazzmapper使用到了集合-collection 即為一對多,一個班級面對多個學生

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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.glj.mapper.studentmapper">
 <select id="selectstudentbyid" parametertype="int" resultmap="studentresultmap">
 select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}
 </select>
 <select id="selectstudentbyclazzid" parametertype="int" resultmap="studentresultmap">
 select * from tb_student where clazz_id = #{id}
 </select>
 <resultmap type="com.glj.pojo.student" id="studentresultmap">
 <id property="id" column="id"/>
 <result property="name" column="name"/>
 <result property="sex" column="sex"/>
 <result property="age" column="age"/>
 <association property="clazz" javatype="com.glj.pojo.clazz">
  <id property="id" column="id"/>
  <result property="code" column="code"/>
  <result property="name" column="name"/>
 </association>
 </resultmap>
</mapper>
?
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.pojo.student;
 
public interface studentmapper {
 student selectstudentbyid(integer id);
}

studentmapper則是與班級為多對一關系,所以使用了關聯-association

嗯,希望我以后又不記得二者的關系時,能感謝現在總結的自己

附上一張mybatis的類型別名圖

Mybatis中collection和association的使用區別詳解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://199604.com/709

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 韩国三级大全 | 日本免费不卡在线一区二区三区 | 亚洲sss综合天堂久久久 | 精品免费视在线观看 | 亚洲福利电影一区二区? | 激情小说色图 | 国产美女下面流出白浆视频 | 精品国产乱码久久久久久免费流畅 | a男人的天堂久久a毛片 | 韩国甜性涩爱在线播放 | 精品一区二区三区免费毛片 | 成人免费福利网站在线看 | 好大好湿好硬好爽好深免费视频 | 午夜福利合集1000在线 | 男人网站视频 | 国产成人亚洲综合91精品555 | 999久久免费高清热精品 | 青草青草伊人精品视频 | 99热这里只有精品免费 | 亚洲男人天堂网站 | 国产99视频精品免费视频7 | 奇米狠狠色 | 日韩欧美国产综合精品 | 好大好猛好爽好深视频免费 | 天美传媒影视在线免费观看 | 欧美图片另类小说综合 | 国产探花在线视频 | 国产99视频精品免费视频免里 | 国产一区二区三区四 | 亚洲精品在线免费看 | 青青草原手机在线视频 | 男女羞羞的视频 | 日本男男gaygays| 欧美 国产 日韩 第一页 | 能播放的欧美同性videos | 国产91第一页 | 亚洲成a人不卡在线观看 | 午夜毛片在线观看 | 亚洲欧美成人综合久久久 | 国产精品视频网 | 教练你好大轻点漫 |