0
点赞
收藏
分享

微信扫一扫

mybatis中<association> 和 <collection>

一、介绍

在 MyBatis 中,<association><collection> 是用于配置结果映射中关联关系的两个元素。

<association> 用于配置一对一的关联关系,表示两个对象之间的关系是一对一的。例如,一个订单对象关联一个用户对象,使用 <association> 进行配置。

<collection> 用于配置一对多的关联关系,表示一个对象关联多个对象。例如,一个部门对象关联多个员工对象,使用<collection> 进行配置。

二、主要区别:

  1. 关联关系类型:<association> 表示一对一的关联关系,而 <collection> 表示一对多的关联关系。
  2. 配置位置:<association> 和 <collection> 元素通常在 <resultMap> 中使用,用于定义结果映射规则。<association> 用于配置单个属性的关联关系,而 <collection> 用于配置集合属性的关联关系。
  3. 属性映射:<association> 使用 <id> 和 <result> 进行属性映射的配置,用于将关联对象的属性与查询结果进行映射。<collection> 除了使用 <id> 和 <result> 进行属性映射外,还使用 <association> 进行嵌套的关联关系配置,用于定义集合元素对象内部的关联关系。
  4. 查询语句:<association> 通常对应一个单独的查询语句,用于获取关联对象的数据。<collection> 通常也对应一个查询语句,用于获取关联对象的集合数据。

三、association标签

mybatis中<association> 和 <collection>_association

实体类

/**
*书籍
*/
@Data
public class Book {
    private String id;
    private String name;
    private String author;
    private Double price;
    //出版社
    private Publisher pub;//一本书对应一个出版社
}

/**
*出版社
*/
@Data
public class Publisher {
    private String id;
    private String name;
    private String phone;
    private String address;
}

XML关联查询

<!--配置关联实体类-->
<resultMap id="bookResultMap" type="com.entity.Book">
    <!--主键属性-->
    <id property="id" column="id" jdbcType="VARCHAR"></id>
    <!--普通属性-->
    <result property="name" column="name" jdbcType="VARCHAR"></result>
    <result property="author" column="author" jdbcType="VARCHAR"></result>
    <result property="price" column="price" jdbcType="VARCHAR"></result>
    <!--一对一映射-->
    <association property="pub" javaType="com.entity.Publisher">
        <id property="id" column="id" jdbcType="VARCHAR"></id>
        <result property="name" column="name" jdbcType="VARCHAR"></result>
        <result property="phone" column="phone" jdbcType="VARCHAR"></result>
        <result property="address" column="address" jdbcType="VARCHAR"></result>
    </association>
</resultMap>
<!--关联查询-->
<select id="selectAllBook" resultMap="bookResultMap">
    SELECT * FROM book e
    left JOIN publisher d ON e.publisher_id = d.id
</select>

嵌套查询

<resultMap id="bookResultMap" type="com.entity.Book">
    <!--主键属性-->
    <id property="id" column="id" jdbcType="VARCHAR"></id>
    <!--普通属性-->
    <result property="name" column="name" jdbcType="VARCHAR"></result>
    <result property="author" column="author" jdbcType="VARCHAR"></result>
    <result property="price" column="price" jdbcType="VARCHAR"></result>
    <association column="publisher_id" property="pub" 
        javaType="com.entity.Publisher" select="selectPublisher"></association>
</resultMap>
 
<!--书籍查询-->
<select id="selectAllBook" resultMap="bookResultMap">
    select * from book
</select>
 
<!--出版社映射Map-->
<resultMap id="publisherResultMap" type="com.entity.Publisher">
    <id property="id" column="id" jdbcType="VARCHAR"></id>
    <result property="name" column="name" jdbcType="VARCHAR"></result>
    <result property="phone" column="phone" jdbcType="VARCHAR"></result>
    <result property="address" column="address" jdbcType="VARCHAR"></result>
</resultMap>
 
<!--嵌套查询-->
<select id="selectPublisher" resultMap="publisherResultMap">
    SELECT * FROM publisher d
    WHERE d.id = #{publisher_id}
</select>

collection标签

<collection>和<association>标签属性基本相同,就多了一个ofType属性。

mybatis中<association> 和 <collection>_java基础_02

实体类

/**
*出版社
*/
@Data
public class Publisher {
    private String id;
    private String name;
    private String phone;
    private String address;
    // 书籍列表
    List<Book> bookList;//一个出版社对应多本书
}

/**
*书籍
*/
@Data
public class Book {
    private String id;
    private String name;
    private String author;
    private Double price;
}

XML关联查询

<resultMap id="publisherResultMap" type="com.entity.Publisher">
    <id property="id" column="id" jdbcType="VARCHAR"></id>
    <result property="name" column="name" jdbcType="VARCHAR"></result>
    <result property="phone" column="phone" jdbcType="VARCHAR"></result>
    <result property="address" column="address" jdbcType="VARCHAR"></result>
    <collection property="bookList" ofType="com.entity.Book">
        <id property="id" column="id" jdbcType="VARCHAR"></id>
        <result property="name" column="name" jdbcType="VARCHAR"></result>
        <result property="author" column="author" jdbcType="VARCHAR"></result>
        <result property="price" column="price" jdbcType="VARCHAR"></result>
    </collection>
</resultMap>
 
<select id="selectAllPublisher" resultMap="publisherResultMap">
    SELECT * FROM publisher d
    left JOIN book e ON e.publisher_id = d.id
</select>

嵌套查询

<resultMap id="publisherResultMap" type="com.entity.Publisher">
    <id property="id" column="id" jdbcType="VARCHAR"></id>
    <result property="name" column="name" jdbcType="VARCHAR"></result>
    <result property="phone" column="phone" jdbcType="VARCHAR"></result>
    <result property="address" column="address" jdbcType="VARCHAR"></result>
    <collection column="id" property="bookList" 
        javaType="java.util.ArrayList" ofType="com.entity.Book"
       select="selectBookList"/>
</resultMap>
 
<select id="selectAllPublisher" resultMap="publisherResultMap">
    SELECT * FROM publisher d
</select>
 
<resultMap id="bookResultMap" type="com.worldly.config.entity.Employee">
    <id property="id" column="id" jdbcType="VARCHAR"></id>
    <result property="name" column="name" jdbcType="VARCHAR"></result>
    <result property="author" column="author" jdbcType="VARCHAR"></result>
    <result property="price" column="price" jdbcType="VARCHAR"></result>
</resultMap>
 
<select id="selectBookList" resultMap="bookResultMap">
    SELECT * FROM book e
    WHERE e.publisher_id = #{id}
</select>

多条件查询

修改collection标签的column属性,{参数名1=列名1,参数名2=列名2}

/**
*出版社
*/
@Data
public class Publisher {
    private String id;
    private String name;
    private String phone;
    private String address;
    // 新增---状态
    private String status;
    // 书籍列表
    List<Book> bookList;//一个出版社对应多本书
}

/**
*书籍
*/
@Data
public class Book {
    private String id;
    private String name;
    private String author;
    private Double price;
    // 新增---状态
    private String status;
}

<!--修改collection标签的column属性-->
<collection column="{publisherId=id,status=status}" property="bookList" 
        javaType="java.util.ArrayList" ofType="com.entity.Book"
       select="selectBookList"/>
 
<select id="selectEmpBydepId" resultMap="empResultMap">
    SELECT * FROM book e
    WHERE e.publisher_id = #{publisherId} AND e.status=#{status}
</select>

举报

相关推荐

0 条评论