mybatis中使用list作为参数

导读:本篇文章讲解 mybatis中使用list作为参数,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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 != ‘’ 判断

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/65563.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!