springBoot双重集合如何查询,mapper.xml如何编写

       中午吃饭还有一段时间,来和大家赶紧聊聊双重集合循环中的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"是代表剩下属性自主封装,让代码自己搞定。
            好了,今天的小技术分享到此技术,希望能给大家带来方便,于人方便,于己方便。
            希望大家随手点赞转发哦!

猜你喜欢

转载自blog.csdn.net/weixin_45150104/article/details/112348975