在sql层处理参数添加进来的列

这篇博客介绍了如何在MyBatis中使用对象参数转化为临时表进行复杂的SQL查询,特别是在处理大数据量时。通过将`List<InformationReportDTO>`对象转换为临时表并进行JOIN操作,实现了在SQL层面将企业名称加入返回列,从而避免了在应用层进行大量数据处理。示例展示了如何利用MyBatis的动态SQL来构建这样的查询,包括使用`<foreach>`标签处理集合参数。
摘要由CSDN通过智能技术生成

问题描述

传进来的List<Object>数据在sql层面处理并将Object的某个属性加入返回列中(merchants)

    List<DetailedReportRespVO> selectDetailedReport(@Param("endDate") String endDate,
                                                    @Param("merchants") List<InformationReportDTO> merchants);
public class InformationReportDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "企业编号")
    private String merchantNo;

    @ApiModelProperty(value = "企业名称")
    private String name;
}

 查询的表中没有企业名称列,此数据是之前传入,并且返回是个list且比较大,所以在sql层处理比较好

public class DetailedReportRespVO implements Serializable {
    private static final long serialVersionUID = 1L;

    @Excel(name = "企业编号")
    @ApiModelProperty(value = "企业编号")
    private String merchantNo;

    @Excel(name = "企业名称")
    @ApiModelProperty(value = "企业名称")
    private String name;

    @Excel(name = "员工姓名")
    @ApiModelProperty(value = "员工姓名")
    private String userName;

。。。。。。。。。。。。。。。。。。。。。。。。。。。

解决方案:

mybatis将对象参数转临时表join

 <select id="selectDetailedReport"
            resultType="com.erp.payroll.common.model.payroll.res.DetailedReportRespVO">
        SELECT DISTINCT
        tem.name as name ,
        t.merchant_no AS merchantNo,
        t.NAME AS userName,
        t.mobile AS mobile,
        t.cert_no AS certNo,
        t.bank_name AS bankName,
        t.bank_card AS bankCard
        FROM
        `t_payroll_detail` t
        left JOIN t_payroll tp ON tp.merchant_no = t.merchant_no
        left JOIN
        <foreach collection="merchants" item="merchant" index="index" separator="union" open="(" close=")">
            select
            #{merchant.merchantNo} as merchantNo,
            #{merchant.name} as name
        </foreach>
        as tem
        on t.merchant_no = tem.merchantNo
        WHERE
        tp.merchant_no in
        <foreach collection="merchants" item="merchant" separator="," open="(" close=")">
            #{merchant.merchantNo, jdbcType = VARCHAR}
        </foreach>
        AND concat( tp.pay_year, '-', LPAD( tp.pay_month, 2, 0 ) ) = #{endDate}
        AND t.bank_status = 1
        AND t.`status` = 0;
    </select>
<foreach collection="merchants" item="merchant" index="index" separator="union" open="(" close=")">
            select
            #{merchant.merchantNo} as merchantNo,
            #{merchant.name} as name
        </foreach>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值