mybatis 单表聚合
表user:id,name,age
按年龄分组,统计每个年龄段的人数,自定义类接受返回结果
*************************
pojo:自定义类接受查询结果
TeacherCount
public class TeacherCount {
private Integer age;
private Integer count;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
。。。。
}
*********************
mapper接口设置
TeacherMapper
public interface TeacherMapper extends Mapper<Teacher> {
List<TeacherCount> getCount();
}
*********************
xml文件
Teacher.xml
<select id="getCount" resultType="com.example.demo.pojo.TeacherCount">
select age,count(*) as count from teacher group by age;
</select>
*********************
controller层
@RequestMapping("/count")
public List<TeacherCount> count(){
return teacherMapper.getCount();
}
*************************
测试输出