欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis中collection和association的使用區(qū)別詳解

 更新時(shí)間:2018年11月27日 13:43:38   作者:生活,一半是回憶、一半是繼續(xù).  
這篇文章主要介紹了Mybatis中collection和association的使用區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

最近一直把collection和association弄混,所以為了增強(qiáng)自己的記憶,就擼一個(gè)關(guān)系出來(lái)算是總結(jié)罷了

1. 關(guān)聯(lián)-association
2. 集合-collection

比如同時(shí)有User.java和Card.java兩個(gè)類(lèi)

User.java如下:

public class User{

private Card card_one;

private List<Card> card_many;

}

在映射card_one屬性時(shí)用association標(biāo)簽, 映射card_many時(shí)用collection標(biāo)簽.

所以association是用于一對(duì)一和多對(duì)一,而collection是用于一對(duì)多的關(guān)系

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

association-一對(duì)一

人和身份證的關(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;
 //人和身份證是一對(duì)一的關(guān)系
 private Card card;
//省略set/get方法.
}

下面是mapper和實(shí)現(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的分步查詢(xún)。

同理多對(duì)一,也是一樣

只要那個(gè)pojo出現(xiàn)private Card card_one;

即使用association

collection 一對(duì)多和association的多對(duì)一關(guān)系

學(xué)生和班級(jí)的一對(duì)多的例子

pojo類(lèi)

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;
    //班級(jí)與學(xué)生是一對(duì)多的關(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é)生與班級(jí)是多對(duì)一的關(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:指的是集合中元素的類(lèi)型 -->
 <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 即為一對(duì)多,一個(gè)班級(jí)面對(duì)多個(gè)學(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則是與班級(jí)為多對(duì)一關(guān)系,所以使用了關(guān)聯(lián)-association

嗯,希望我以后又不記得二者的關(guān)系時(shí),能感謝現(xiàn)在總結(jié)的自己

附上一張mybatis的類(lèi)型別名圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java SwingWorkder使用實(shí)例

    Java SwingWorkder使用實(shí)例

    最近在學(xué)習(xí)Swing,我們都知道在UI表現(xiàn)線程里面長(zhǎng)時(shí)間執(zhí)行操作時(shí),畫(huà)面會(huì)假死,為了能夠讓費(fèi)時(shí)操作不影響畫(huà)面表現(xiàn),就需要用多線程了
    2014-04-04
  • jdk8使用stream實(shí)現(xiàn)兩個(gè)list集合合并成一個(gè)(對(duì)象屬性的合并)

    jdk8使用stream實(shí)現(xiàn)兩個(gè)list集合合并成一個(gè)(對(duì)象屬性的合并)

    本文主要介紹了jdk8使用stream實(shí)現(xiàn)兩個(gè)list集合合并成一個(gè)(對(duì)象屬性的合并),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 類(lèi)似Object監(jiān)視器方法的Condition接口(詳解)

    類(lèi)似Object監(jiān)視器方法的Condition接口(詳解)

    下面小編就為大家?guī)?lái)一篇類(lèi)似Object監(jiān)視器方法的Condition接口(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • java HashMap 的工作原理詳解

    java HashMap 的工作原理詳解

    本文主要介紹java HashMap 的資料,這里整理了相關(guān)資料,并詳細(xì)說(shuō)明了HashMap的用法,有需要的小伙伴可以參考下
    2016-09-09
  • 解決java錯(cuò)誤:不支持發(fā)行版本5

    解決java錯(cuò)誤:不支持發(fā)行版本5

    這篇文章主要給大家介紹了關(guān)于如何解決java錯(cuò)誤:不支持發(fā)行版本5的相關(guān)資料,發(fā)行版本5是Java5,已經(jīng)是十多年前的版本了,現(xiàn)在已經(jīng)不再被支持,需要的朋友可以參考下
    2023-07-07
  • Java并發(fā)系列之CountDownLatch源碼分析

    Java并發(fā)系列之CountDownLatch源碼分析

    這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之CountDownLatch源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • springboot 緩存@EnableCaching實(shí)例

    springboot 緩存@EnableCaching實(shí)例

    這篇文章主要介紹了springboot 緩存@EnableCaching實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java?ynchronized重量級(jí)鎖的核心原理詳解

    Java?ynchronized重量級(jí)鎖的核心原理詳解

    這篇文章主要為大家詳細(xì)介紹了Java?ynchronized重量級(jí)鎖的核心原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • Java中實(shí)現(xiàn)雙數(shù)組Trie樹(shù)實(shí)例

    Java中實(shí)現(xiàn)雙數(shù)組Trie樹(shù)實(shí)例

    這篇文章主要介紹了Java中實(shí)現(xiàn)雙數(shù)組Trie樹(shù)實(shí)例,雙數(shù)組Trie就是一種優(yōu)化了空間的Trie樹(shù),本文給出了實(shí)現(xiàn)代碼、測(cè)試代碼和測(cè)試結(jié)果,需要的朋友可以參考下
    2015-01-01
  • 淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式

    淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式

    這篇文章主要介紹了淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式,使用設(shè)計(jì)模式編寫(xiě)代碼有利于團(tuán)隊(duì)協(xié)作時(shí)程序的維護(hù),需要的朋友可以參考下
    2016-01-01

最新評(píng)論