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

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