mybatis中使用list作为参数

mybatis中使用list作为参数

collection属性值类型为List

dao

@Override
    public Integer timeAnalyse(String customerId, LocalDateTime firstDay, LocalDateTime lastDay, String metric ,Integer type) {
        List<String> list = this.getMetrics(metric);
        return baseMapper.countByTime(customerId, firstDay, lastDay, list, type);
    }

    /**
     * 获取指标列表
     * @param metric 指标大类名称
     * @return 指标列表
     */
    private List<String> getMetrics(String metric) {
        List<String> list = new ArrayList<>();
        if (metric.equals(HealthRecordConstant.METRIC_TYPE_WEIGHT)) {
            list.add(HealthRecordConstant.METRIC_TYPE_WEIGHT);
            list.add(HealthRecordConstant.METRIC_TYPE_BMI);
        } else if (metric.equals(HealthRecordConstant.METRIC_TYPE_BLOODPRESSURE)) {
            list.add(HealthRecordConstant.METRIC_TYPE_DIASTOLICPRESSURE);
            list.add(HealthRecordConstant.METRIC_TYPE_HEARTPATEOFBP);
            list.add(HealthRecordConstant.METRIC_TYPE_SYSTOLICPRESSURE);
        } else {
            list.add(HealthRecordConstant.METRIC_TYPE_BGAFTERBREAKFAST);
            list.add(HealthRecordConstant.METRIC_TYPE_BGAFTERDINNER);
            list.add(HealthRecordConstant.METRIC_TYPE_BGAFTERLUNCH);
            list.add(HealthRecordConstant.METRIC_TYPE_BGBEFOREBEDTIME);
            list.add(HealthRecordConstant.METRIC_TYPE_BGBEFOREDAWN);
            list.add(HealthRecordConstant.METRIC_TYPE_BGBEFOREDINNER);
            list.add(HealthRecordConstant.METRIC_TYPE_BGBEFORELUNCH);
            list.add(HealthRecordConstant.METRIC_TYPE_FASTINGBLOODGLUCOSE);
        }
        return list;
    }

mapper

Integer countByTime(@Param("customerId") String customerId,
                     @Param("firstDay") LocalDateTime firstDay,
                     @Param("lastDay") LocalDateTime lastDay,
                     @Param("metrics") List<String> metrics,
                     @Param("type") Integer type);

xml

 <select id="countByTime" resultType="Integer">
        SELECT
            COUNT(*)
        from
            customer_body_metrics
        WHERE
        <if test="type == 0">
            HOUR(create_time) BETWEEN 0 and 8
        </if>
        <if test="type == 1">
            HOUR(create_time) BETWEEN 8 and 16
        </if>
        <if test="type == 2">
            HOUR(create_time) BETWEEN 16 and 24
        </if>

        and
            customer_id = #{customerId}
        and
            create_time > #{firstDay}
        and
            #{lastDay} > create_time
        <if test="metrics.size()>0">
        and
            metric IN
            <foreach collection="metrics" item="value" index="index"  open="(" close=")" separator=",">
                #{value}
            </foreach>
        </if>

    </select>

注意:传入参数为List不能用lists != ‘’ 判断

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值