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>