1. 程式人生 > 其它 >springBoot雙重集合如何查詢,mapper.xml如何編寫

springBoot雙重集合如何查詢,mapper.xml如何編寫

技術標籤:javamybatisxmljava

中午吃飯還有一段時間,來和大家趕緊聊聊雙重集合迴圈中的mapper.xml如何編寫,哎,剛剛又聽到不好的訊息,讓去出差、、、寫文章寫文章。
先寫下PmsProductSaleAttr實體:

package com.ygl.gmall.bean;


import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.List;
public class PmsProductSaleAttr implements Serializable { @Id @Column String id; @Column String productId; @Column String saleAttrId; @Column String saleAttrName; @Transient List<PmsProductSaleAttrValue> spuSaleAttrValueList; public String getId
() { return id; } public void setId(String id) { this.id = id; } public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public String getSaleAttrId() { return
saleAttrId; } public void setSaleAttrId(String saleAttrId) { this.saleAttrId = saleAttrId; } public String getSaleAttrName() { return saleAttrName; } public void setSaleAttrName(String saleAttrName) { this.saleAttrName = saleAttrName; } public List<PmsProductSaleAttrValue> getSpuSaleAttrValueList() { return spuSaleAttrValueList; } public void setSpuSaleAttrValueList(List<PmsProductSaleAttrValue> spuSaleAttrValueList) { this.spuSaleAttrValueList = spuSaleAttrValueList; } }

PmsProductSaleAttrValue實體程式碼如下:

package com.ygl.gmall.bean;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.io.Serializable;

public class PmsProductSaleAttrValue implements Serializable {
    @Id
    @Column
    String id;

    @Column
    String productId;

    @Column
    String saleAttrId;

    @Column
    String saleAttrValueName;

    @Transient
    String isChecked;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getSaleAttrId() {
        return saleAttrId;
    }

    public void setSaleAttrId(String saleAttrId) {
        this.saleAttrId = saleAttrId;
    }

    public String getSaleAttrValueName() {
        return saleAttrValueName;
    }

    public void setSaleAttrValueName(String saleAttrValueName) {
        this.saleAttrValueName = saleAttrValueName;
    }

    public String getIsChecked() {
        return isChecked;
    }

    public void setIsChecked(String isChecked) {
        this.isChecked = isChecked;
    }
}

下面Controller層和Service層就不再編寫,直接到mapper層,mapper層介面程式碼如下:

package com.ygl.gmall.manage.mapper;

import com.ygl.gmall.bean.PmsProductSaleAttr;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author ygl
 * @description
 * @date 2020/12/26 18:17
 */
@Mapper
public interface ProductSaleAttrMapper extends tk.mybatis.mapper.common.Mapper<PmsProductSaleAttr> {
    List<PmsProductSaleAttr> selectSpuSaleAttrListCheckBySku(@Param("productId") String productId, @Param("skuId") String skuId);
}

注意:@Param(“xxx")是給傳參命名,當在mapper.xml時可以接收介面傳值過來的引數,mapper.xml程式碼如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ygl.gmall.manage.mapper.ProductSaleAttrMapper">
    <!--    注意:這裡是雙重集合,不能再resultType="實體類地址"-->

    <select id="selectSpuSaleAttrListCheckBySku" resultMap="selectSpuSaleAttrListCheckBySkuMap">
        SELECT
            sa.id as sa_id , sav.id as sav_id ,sa.* , sav.* , if(ssav.sku_id,1,0) as isChecked
        FROM pms_product_sale_attr sa
        INNER JOIN pms_product_sale_attr_value sav on sa.product_id = sav.product_id AND sa.sale_attr_id = sav.sale_attr_id and sa.product_id = #{productId}
        LEFT JOIN pms_sku_sale_attr_value ssav on sa.sale_attr_id = ssav.sale_attr_id
                    and sav.id = ssav.sale_attr_value_id and ssav.sku_id = #{skuId}
    </select>
    <!--autoMapping="true"代表剩下的屬性自主封裝,自己搞定-->
    <resultMap id="selectSpuSaleAttrListCheckBySkuMap" type="com.ygl.gmall.bean.PmsProductSaleAttr" autoMapping="true">
        <result property="id" column="sa_id"></result>

        <collection property="spuSaleAttrValueList" ofType="com.ygl.gmall.bean.PmsProductSaleAttrValue" autoMapping="true">
            <result column="sav_id" property="id"></result>
        </collection>
    </resultMap>
</mapper>

注意在select的標籤中是resultMap,不是resultType,在resultMap標籤中進行定義最外層集合,collection標籤中定義的是內層集合,property是實體中使用名字,column是mapper.xml中所使用的屬性名。標籤中autoMapping="true"是代表剩下屬性自主封裝,讓程式碼自己搞定。
好了,今天的小技術分享到此技術,希望能給大家帶來方便,於人方便,於己方便。
希望大家隨手點贊轉發哦!