项目练习:使用mybatis的foreach标签,实现union all的拼接语句

文章目录

一、需求说明

在sql查询数据后,对数据分组统计。并最后进行总计。

二、需求分析

最终,我想用sql来实现这个统计和查询的功能。

那么,怎么又查询,又统计了?

就用到MySQL的union all关键字。

然后,通过mybatis把参数组装成list,传入,在利用foreach标签,循环拼接union all部分即可。

三、代码实现

Java代码

java 复制代码
    public List<Map<String, Object>>  getPriceData(Long  infoId){
        //根据info id 查询price list
        List<Map<String, Object>> priceList = formPriceService.getVIds(infoId);

        List<Object> vids = new ArrayList<>();
        for (Map<String, Object> res : priceList) {
            vids.add(res.get("variety_id"));
        }

        Map<String, Object> params = new HashMap<>();
        params.put("infoId",infoId);
        params.put("vids",vids);
        List<Map<String, Object>> priceData = formPriceService.getPriceData(params);
        return  priceData;
    }
------------以下为mapper层代码-------------

    @Override
    public List<Map<String, Object>> getPriceData(Map<String, Object> params) {
        return formPriceMapper.getPriceData(params);
    }

    @Override
    public List<Map<String, Object>> getVIds(Long id) {
        return formPriceMapper.getVIds(id);
    }

xml代码

getVIds方法

xml 复制代码
<select id="getVIds" parameterType="Long" resultType="Map">
        SELECT
            distinct price.variety_id
        FROM
            `rent_form_price` price
                LEFT JOIN rent_form_info info
                          ON	price.form_info_id = info.id
                LEFT JOIN rent_material_standard_temp stemp
                          ON price.standard_id = stemp.id
                LEFT JOIN rent_material_variety_temp vtemp
                          ON price.variety_id = vtemp.id
                LEFT JOIN rent_store_info store
                          ON info.store_id = store.id
        WHERE price.form_info_id = #{formInfoId}
    </select>

getPriceData方法

xml 复制代码
    <select id="getPriceData" resultType="Map" parameterType="Map">
        <foreach collection="vids" item="vid" index="index" separator="union all">
            SELECT
            price.variety_id,
            price.id,
            price.form_info_id,
            info.send_date,
            info.rent_begin_date,
            vtemp.variety_name,
            stemp.standard,
            price.number,
            price.rental_number,
            stemp.convert_amount,
            price.pound,
            store.store_name,
            price.note,
            stemp.rental_unit
            FROM
            `rent_form_price` price
            LEFT JOIN rent_form_info info ON price.form_info_id = info.id
            LEFT JOIN rent_material_standard_temp stemp ON price.standard_id = stemp.id
            LEFT JOIN rent_material_variety_temp vtemp ON price.variety_id = vtemp.id
            LEFT JOIN rent_store_info store ON info.store_id = store.id
            WHERE
            price.variety_id = #{vid}
            AND price.form_info_id = #{infoId}

            UNION ALL
            SELECT
            '',
            '',
            '',
            '',
            '',
            CONCAT(vtemp.variety_name,'小计:'),
            '',
            sum(price.number),
            sum(price.rental_number),
            '',
            sum(price.pound),
            '',
            '',
            ''
            FROM
            rent_form_price price
            LEFT JOIN rent_material_variety_temp vtemp ON price.variety_id = vtemp.id
            WHERE
            price.variety_id = #{vid}
            AND price.form_info_id = #{infoId}
        </foreach>
            UNION ALL
            SELECT
            '',
            '',
            '',
            '',
            '',
            '本单总计:',
            '',
            sum(price.number),
            sum(price.rental_number),
            '',
            sum(price.pound),
            '',
            '',
            ''
            FROM
            rent_form_price price
            LEFT JOIN rent_material_variety_temp vtemp ON price.variety_id = vtemp.id
            WHERE
            form_info_id = #{infoId}
    </select>

四、报表效果

参考:MyBatis之foreach的用法

相关推荐
只因在人海中多看了你一眼9 小时前
B.40.3.2-MyBatis核心技术详解与性能优化
性能优化·mybatis
.又是新的一天.15 小时前
健身房预约系统SSM+Mybatis(五、预约展示)
前端·mybatis
北城以北888816 小时前
SSM--MyBatis框架之动态SQL
java·开发语言·数据库·sql·mybatis
计算机学姐19 小时前
基于SpringBoot的公务员考试管理系统【题库组卷+考试练习】
java·vue.js·spring boot·后端·java-ee·intellij-idea·mybatis
刘一说2 天前
深入解析 Spring Boot 数据访问:Spring Data JPA 与 MyBatis 集成实战
spring boot·tomcat·mybatis
赋能大师兄2 天前
MyBatis缓存机制
mybatis·二级缓存·一级缓存
codingPower3 天前
升级mybatis-plus导致项目启动报错: net.sf.jsqlparser.statement.select.SelectBody
java·spring boot·maven·mybatis
123461614 天前
互联网大厂Java面试:从Spring Boot到微服务的探索
java·数据库·spring boot·微服务·面试·mybatis·orm
2301_801252224 天前
Mybatis的添加和修改功能
java·开发语言·mybatis
W.Buffer5 天前
MyBatis 源码深度解析:从 Spring Boot 实战到底层原理
spring boot·后端·mybatis