mybatis association collection

fb5ef4357c3a4dad867d4e52fb8c47d0.png

232768d13d5e4373aef6ba3c2eff4366.png

5693e8f524f649cbbb86041a80a5362f.png

c6c4dc700eba4e039823bb6f98aa9ced.png

/**
*出版社
*/
@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;
}

关联查询!!

<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.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>
</resultMap>
 
<select id="selectBookList" resultMap="bookResultMap">
    SELECT * FROM book e
    WHERE e.publisher_id = #{id}
</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值