collection與association在Mybatis中有什么區(qū)別

這篇文章將為大家詳細講解有關(guān)collection與association在Mybatis中有什么區(qū)別,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元北海街道做網(wǎng)站,已為上家服務(wù),為北海街道各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

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標簽, 映射card_many時用collection標簽.

所以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的類型別名圖

collection與association在Mybatis中有什么區(qū)別

關(guān)于collection與association在Mybatis中有什么區(qū)別就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

本文標題:collection與association在Mybatis中有什么區(qū)別
標題路徑:http://bm7419.com/article18/jcchgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、服務(wù)器托管、定制網(wǎng)站企業(yè)網(wǎng)站制作ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司