在用mybatis进行数据库查询时,对查询结果进行自动分组,在mapper.xml中的配置有些注意的地方,下面是实际项目中一个例子。在数据库中查询中如下:

在结果集中需要对alarmDate进行分组,比如2017-04-05这两条数据,分组后最终实现如下效果:

{
"alarmDate": "2017-04-05",
"entityCounts": [
{
"count": 2,
"alarmLevel": "警告"
},
{
"count": 567,
"alarmLevel": "严重"
}
]
}

那么这种情况在mapper.xml该怎么配呢?需要配一个resultMap, 在里面配置分组的关键字段。

  <resultMap id="alarmCountDate" type="monitor.entity.EntityCountDateList">
<id property="alarmDate" column="alarmDate" />
<collection property="entityCounts" ofType="monitor.entity.EntityCountDate">
<result property="alarmLevel" column="alarmLevel" />
<result property="count" column="count" />
</collection>
</resultMap> <!--resultMap的值为上面resultMap的id。 -->
<select id="getAlarmCountByDate" parameterType="map" resultMap="alarmCountDate">
<![CDATA[
select count(t.alarmLevel) as count,t.alarmLevel,date(t.alarmDate) as alarmDate from (select alarmDate,alarmLevel
from alarm_info where alarmDate>=#{alarmStartDate} and alarmDate<=#{alarmEndDate}
group by alarmLevel,alarmDate ) t group by t.alarmLevel,date(t.alarmDate) order by alarmDate; ]]>
</select>

涉及到两个对象类,如下

类monitor.entity.EntityCountDateList

package monitor.entity;

import java.util.List;

public class EntityCountDateList {

    private String alarmDate;
private List<EntityCountDate> entityCounts;
public String getAlarmDate() {
return alarmDate;
}
public void setAlarmDate(String alarmDate) {
this.alarmDate = alarmDate;
}
public List<EntityCountDate> getEntityCounts() {
return entityCounts;
}
public void setEntityCounts(List<EntityCountDate> entityCounts) {
this.entityCounts = entityCounts;
} }

类:monitor.entity.EntityCountDate

package monitor.entity;

public class EntityCountDate {

    private Integer count;
private String alarmLevel;
public EntityCountDate(){}
public EntityCountDate(String alarmLevel,Integer count){
this.count = count;
this.alarmLevel = alarmLevel;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getAlarmLevel() {
return alarmLevel;
}
public void setAlarmLevel(String alarmLevel) {
this.alarmLevel = alarmLevel;
} }

在resultMap中配置的entityCounts为类EntityCountDateList中List<EntityCountDate>的名字,这一步很关键。

<collection property="entityCounts" ofType="monitor.entity.EntityCountDate">

mybatis中查询结果进行分组的更多相关文章

  1. 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案

    1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...

  2. mybatis中查询结果为空时不同返回类型对应返回值

    今天在别人的代码基础上实现新需求,看到对于mybatis查询结果的判断不是很正确,如果查询结果为空就会异常,不知道大家有没有这样的疑惑:mybatis中resultType有多种返回类型,对于每种不同 ...

  3. mybatis中查询使用#{}和${}的区别

    ${}中的变量什么值,就会简单的替代变量,不会做处理 比如delete * from tb_label where name=${labelname} 如果labelname的值是 something ...

  4. SSM-MyBatis-11:Mybatis中查询全部用resultmap

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 实体类很普通,四个字段,编号,名字,作者名,价格 在接口中的方法声明如下 //查全部 public List& ...

  5. 记自己在mybatis中设置jdbcType的一个坑

    项目是用ssm搭建的.主要是为app数据接口.其中有一个需求就app想要查询一段时间内某个用户的测量信息,所以app给我后端传递了3个参数,分别是appuserId(String),startDate ...

  6. Mybatis中的延迟加载的使用方法

    Mybatis中的延迟加载的使用方法 在Mybatis中查询订单,然后带出商品信息和快递信息的配置方法 orderMapper.xml配置如下 <?xml version="1.0&q ...

  7. java之mybatis之查询及分页

    1.mybatis中查询方式有3种 //查询单个值 @Test public void testFindOne()throws IOException{ SqlSession session = My ...

  8. mybatis中的查询缓存

    一: 查询缓存 Mybatis提供查询缓存,用于减轻数据压力,提高数据库压力. Mybatis提供一级缓存和二级缓存. 在操作数据库时需要构造SqlSession对象,在对象中有一个数据结构(Hash ...

  9. Mybatis中的in查询和foreach标签

    Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...

随机推荐

  1. 小结:VB.NET机房收费系统个人版

    经过几天的缝缝补补,自己的个人版最终OK了,!或许是由于有第一次的机房收费系统的经验,这次做,感觉很亲切. 在业务逻辑方面,沿袭曾经的逻辑.做了一点升级.可是修改不大. 在数据库方面,感觉自己从一个小 ...

  2. svn解决与优化帮助

    1.问题的出现 解决方案: 最后一行不能子目录. 启动的时候也是要根目录的.svnserve -d -r /home/svn/repos [不能是svnserve -d -r /home/svn/re ...

  3. Nginx:handler模块按处理阶段挂载原理

    参考资料<深入理解Nginx>(陶辉) 在Nginx中,handler模块真正的处理函数通过两种方式挂载到处理过程中,一种方式就是按处理阶段挂载;另外一种挂载方式就是按需挂载. 本次我们将 ...

  4. Asp.net管道模型(管线模型)之一发不可收拾

    前言 为什么我会起这样的一个标题,其实我原本只想了解asp.net的管道模型而已,但在查看资料的时候遇到不明白的地方又横向地查阅了其他相关的资料,而收获比当初预想的大了很多. 有本篇作基础,下面两篇就 ...

  5. 无法将“Update-Database”项识别为 cmdlet、函数、脚本文件或可运行程序的名称的问题

    原因: 没有引用EntityFramework命令 解决: 在程序包管理器控制台执行如下命令:Import-Module 项目路径\packages\EntityFramework.6.1.3(EF版 ...

  6. create table #temptable 临时表 和 declare @bianliang table ()表变量

    create table #temptable 临时表 和 declare @bianliang table ()表变量 在开发过程中,经常会遇到使用表变量和本地临时表的情况.下面是对二者的一个介绍: ...

  7. 工作总结 无法确定条件表达式的类型,因为“<null>”和“System.DateTime”之间没有隐式转换 解决办法 object——Nullable<T> (可空类型)

    可空值类型 备注     一种类型认为是可以为 null,如果它可以分配一个值,也可以分配null,这意味着类型具有无论如何没有值. 默认情况下,所有都引用类型,如String,是否可以为 null, ...

  8. 在oracle11g中配置多个DataGuard物理备机

    >> from zhuhaiqing.info 主机配置 alter system set DB_UNIQUE_NAME='starboss' scope=spfile; alter sy ...

  9. spark standlone 伪分布式安装

    一,在官网下载对应的版本http://spark.apache.org/downloads.html 二在linux中解压下来的spark包   三:配置环境变量     (1)在/etc/profi ...

  10. oracle高性能的SQL语句的写法

    1.当多表查询的时候,把数据量小的表放在最后面,ORACLE会把最后面的表当作基础表,因为表间连接时,最右边的表会被放到嵌套循环的最外层.最外层的循环次数越少,效率越高. 2.Oracle采用自下而上 ...