当前位置: 代码网 > it编程>编程语言>Java > mybatis中使用list作为参数方式

mybatis中使用list作为参数方式

2026年01月07日 Java 我要评论
mybatis中使用list作为参数collection属性值类型为listdao@override public integer timeanalyse(string customerid,

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

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

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com