Mybatis中collection和association的使用區(qū)別詳解
最近一直把collection和association弄混,所以為了增強自己的記憶,就擼一個關(guān)系出來算是總結(jié)罷了
1. 關(guān)聯(lián)-association
2. 集合-collection
比如同時有User.java和Card.java兩個類
User.java如下:
public class User{
private Card card_one;
private List<Card> card_many;
}
在映射card_one屬性時用association標(biāo)簽, 映射card_many時用collection標(biāo)簽.
所以association是用于一對一和多對一,而collection是用于一對多的關(guān)系
下面就用一些例子解釋下吧
association-一對一
人和身份證的關(guān)系
下面是pojo
public class Card implements Serializable{
private Integer id;
private String code;
//省略set和get方法.
}
public class Person implements Serializable{
private Integer id;
private String name;
private String sex;
private Integer age;
//人和身份證是一對一的關(guān)系
private Card card;
//省略set/get方法.
}
下面是mapper和實現(xiàn)的接口
package com.glj.mapper;
import com.glj.poji.Card;
public interface CardMapper {
Card selectCardById(Integer id);
}
<?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>
package com.glj.mapper;
import com.glj.poji.Person;
public interface PersonMapper {
Person selectPersonById(Integer id);
}
<?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出現(xiàn)private Card card_one;
即使用association
collection 一對多和association的多對一關(guān)系
學(xué)生和班級的一對多的例子
pojo類
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;
//班級與學(xué)生是一對多的關(guān)系
private List<Student> students;
//省略set/get方法
}
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;
//學(xué)生與班級是多對一的關(guān)系
private Clazz clazz;
//省略set/get方法
}
<?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>
package com.glj.mapper;
import com.glj.pojo.Clazz;
public interface ClazzMapper {
Clazz selectClazzById(Integer id);
}
ClazzMapper使用到了集合-collection 即為一對多,一個班級面對多個學(xué)生
<?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>
package com.glj.mapper;
import com.glj.pojo.Student;
public interface StudentMapper {
Student selectStudentById(Integer id);
}
StudentMapper則是與班級為多對一關(guān)系,所以使用了關(guān)聯(lián)-association
嗯,希望我以后又不記得二者的關(guān)系時,能感謝現(xiàn)在總結(jié)的自己
附上一張mybatis的類型別名圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- mybatis利用association或collection傳遞多參數(shù)子查詢
- Mybatis之a(chǎn)ssociation和collection用法
- 在Mybatis中association標(biāo)簽多層嵌套的問題
- mybatis中一對一關(guān)系association標(biāo)簽的使用
- MyBatis中association的基本使用方法
- mybatis的association傳遞參數(shù)問題示例
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- MyBatis的collection和association的使用解讀
- mybatis中association標(biāo)簽的使用解讀
- MyBatis使用嵌套查詢collection和association的實現(xiàn)
- Mybatis的association使用子查詢結(jié)果錯誤的問題解決
相關(guān)文章
Java 在volatile內(nèi)部調(diào)用接口的方法
在Java中,volatile?關(guān)鍵字通常用于確保變量的可見性和有序性,而不是用來修飾接口或方法調(diào)用的,這篇文章主要介紹了Java 在volatile內(nèi)部調(diào)用接口的方法,需要的朋友可以參考下2024-07-07
Java設(shè)計模式之策略模式_動力節(jié)點Java學(xué)院整理
策略模式是對算法的封裝,把一系列的算法分別封裝到對應(yīng)的類中,并且這些類實現(xiàn)相同的接口,相互之間可以替換。接下來通過本文給大家分享Java設(shè)計模式之策略模式,感興趣的朋友一起看看吧2017-08-08
jackson 實現(xiàn)null轉(zhuǎn)0 以及0轉(zhuǎn)null的示例代碼
這篇文章主要介紹了jackson 實現(xiàn)null轉(zhuǎn)0 以及0轉(zhuǎn)null的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
詳解Java創(chuàng)建多線程的四種方式以及優(yōu)缺點
這篇文章主要介紹了Java創(chuàng)建多線程的四種方式以及優(yōu)缺點,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
詳述IntelliJ IDEA遠(yuǎn)程調(diào)試Tomcat的方法(圖文)
本篇文章主要介紹了詳述IntelliJ IDEA遠(yuǎn)程調(diào)試Tomcat的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
SpringMVC @ControllerAdvice使用場景
這篇文章主要介紹了SpringMVC @ControllerAdvice使用場景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
IDEA提示內(nèi)存不足 low memory的完美解決方法(親測好用)
這篇文章主要介紹了IDEA提示內(nèi)存不足 low memory的完美解決方法(親測好用),這里以IDEA2022版本為例,在IDE中 幫助(help)–>change memory setting(改變內(nèi)存設(shè)置),具體設(shè)置辦法文中給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01
SpringBoot+Echarts實現(xiàn)請求后臺數(shù)據(jù)顯示餅狀圖
這篇文章主要介紹了SpringBoot+Echarts實現(xiàn)請求后臺數(shù)據(jù)顯示餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12

