Mybatis如何查询不同数据对应的记录数并返回,在线等,急

图片说明

<select id="alarmTypeNumber" parameterType="string" resultType="">
    select 
        alarm_type,count(alarm_type) 
    from 
        alarm_infor 
    where 
        path like '${value}%' 
    group by 
        alarm_type
</select>

    标签语句知道,但是不知道如何返回和后台如何接受

可以自定义一个对象,通过自定义resultMap来接受这两个字段

  1. 修改SQL如下,主要是为count(alarm_type)字段设置别名
<select id="alarmTypeNumber" parameterType="string" resultType="">
    select alarm_type, count(alarm_type) as alarm_type_count
    from alarm_infor
    where path like '${value}%'
    group by alarm_type
</select>
  1. 第二步定义model类
/**
 * @author 张玉尧(matrix.zhang@ximalaya.com)
 * @version $Id: AlarmTypeCountDO.java, v 0.1 2019年2月21日 上午11:35:35 张玉尧(matrix.zhang@ximalaya.com) Exp $
 */
public class AlarmTypeCountDO {

    private String alarmType;
    private Long alarmTypeCount;

    public String getAlarmType() {
        return alarmType;
    }

    public void setAlarmType(String alarmType) {
        this.alarmType = alarmType;
    }

    public Long getAlarmTypeCount() {
        return alarmTypeCount;
    }

    public void setAlarmTypeCount(Long alarmTypeCount) {
        this.alarmTypeCount = alarmTypeCount;
    }
}
  1. 修改SQL如下,将resultType设置为AlarmTypeCountDO类的全限定名
<select id="alarmTypeNumber" parameterType="string" resultType="包名.AlarmTypeCountDO">
    select alarm_type, count(alarm_type) as alarm_type_count
    from alarm_infor
    where path like '${value}%'
    group by alarm_type
</select>
List<Map> alarmTypeNumber(@Param("value") String value);
<select id="alarmTypeNumber" parameterType="java.lang.String" resultType="java.util.Map">
    select
        alarm_type,count(alarm_type) con
    from
        alarm_infor
    where
        path like '${value}%'
    group by
        alarm_type
</select>

简单的直接返回map即可,Dao 用List 接受