Spring+Mybatis 复杂的分组查询
1、需要的结果数据格式为
{
"responseCode": "0000",
"responseMsg": null,
"data": [
{
"genreId": "6015",
"genreName": "财务",
"appRankDtos": [
{
"ranking": "10",
"rankDate": "2019-04-22"
},
{
"ranking": "8",
"rankDate": "2019-04-23"
},
{
"ranking": "9",
"rankDate": "2019-04-24"
}
]
},
{
"genreId": "6007",
"genreName": "应用总榜",
"appRankDtos": [
{
"ranking": "20",
"rankDate": "2019-04-22"
},
{
"ranking": "28",
"rankDate": "2019-04-23"
}
]
}
]
}

创建的bean
RankResult:
public class RankResult extends CommonResult {
/**
* CommentResult中包含responseCode和responseMsg
*/
private List<RankGenreResult> data;
public List<RankGenreResult> getData() {
return data;
}
public void setData(List<RankGenreResult> data) {
this.data = data;
}
}
RankGenreResult:
public class RankGenreResult {
/**
* 排名分类
*/
private String genreId;
/**
* 分类名称
*/
private String genreName;
/**
* 分类列表数据
*/
private List<AppRankDto> appRankDtos;
// 省略getter and setter
}
AppRankDto:
public class AppRankDto {
/**
* 排名
*/
private String ranking;
/**
* 排名时间
*/
private String rankDate;
// 省略getter and setter
}
controller:
@ResponseBody
@RequestMapping("queryAppRank")
public RankResult queryAppRank(@RequestBody Map<String, String> param) {
RankResult result = new RankResult();
List<RankGenreResult> rankGenreResultList;
try {
rankGenreResultList = commentService.queryAppRankGenreResult(param);
result.setData(rankGenreResultList);
} catch (Exception e) {
result.fail(ResponseEnum.SYSTEM_ERROR.getResponseCode(), ResponseEnum.SYSTEM_ERROR.getResponseMsg());
LOGGER.error("查询XXX,e={}", ExceptionUtil.getAllStackTrace(e));
return result;
}
return result; }
service:
@Override
public List<RankGenreResult> queryAppRankGenreResult(Map<String, String> param) {
return commentMapper.queryAppRankGenreResult(param);
}
mapper:
List<RankGenreResult> queryAppRankGenreResult(Map<String, String> param);
mapper.xml
<!--定义映射resultMap-->
<resultMap id="rankGenreResult" type="RankGenreResult">
<result property="genreId" column="genre_id"/>
<result property="genreName" column="genre_name"/>
<collection property="appRankDtos" ofType="AppRankDto">
<result property="ranking" column="ranking"/>
<result property="rankDate" column="rank_date"/>
</collection>
</resultMap>
<select id="queryAppRankGenreResult" resultMap="rankGenreResult">
select genre_id,genre_name,ranking,rank_date
from
t_app_rank
<where>
<if test="popId!=''and popId!=null">
t.POP_ID=#{popId}
</if>
<if test="marketAppId!=''and marketAppId!=null">
t.MARKET_APP_ID=#{marketAppId}
</if>
<if test="beginRankDate!=''and beginRankDate!=null">
<![CDATA[t.RANK_DATE>=#{beginRankDate}]]>
</if>
<if test="endRankDate!=''and endRankDate!=null">
<![CDATA[t.RANK_DATE<=#{endRankDate}]]>
</if>
</where>
group by
genre_id,GENRE_NAME,ranking,rank_date
ORDER BY rank_date
</select>
返回父菜单及其下的子菜单
需要的数据格式:
[
{
"name": "一级菜单",
"pid": 0,
"sub_button": [
{
"key": "",
"name": "二级菜单1",
"pid": 1,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
},
{
"key": "",
"name": "二级菜单2",
"pid": 1,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
},
{
"key": "",
"name": "二级菜单4",
"pid": 1,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
},
{
"key": "",
"name": "二级菜单5",
"pid": 1,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
}
]
},
{
"name": "一级菜单2",
"pid": 0,
"sub_button": [
{
"key": "",
"name": "二级菜单21",
"pid": 4,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
},
{
"key": "",
"name": "二级菜单22",
"pid": 4,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
},
{
"key": "",
"name": "二级菜单23",
"pid": 4,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
},
{
"key": "",
"name": "二级菜单24",
"pid": 4,
"sub_button": [],
"type": "view",
"url": "http://oa.cnsuning.com/portal/soa/index.htm"
}
]
},
{
"name": "一级菜单",
"pid": 0,
"sub_button": [
{
"key": "123456789",
"name": "二级菜单1",
"pid": 23,
"sub_button": [],
"type": "click",
"url": ""
}
]
}
]
表结构:
CREATE TABLE `t_wechat_menu` (
`ID` bigint(15) NOT NULL AUTO_INCREMENT COMMENT '主键',
`TYPE` varchar(10) DEFAULT NULL COMMENT '菜单的响应动作类型',
`NAME` varchar(60) DEFAULT NULL COMMENT '菜单标题',
`KEY` varchar(128) DEFAULT NULL COMMENT '菜单KEY值,用于消息接口推送',
`URL` varchar(1024) DEFAULT NULL COMMENT '网页链接',
`MEDIA_ID` varchar(40) DEFAULT NULL COMMENT '调用新增永久素材接口',
`PID` bigint(15) DEFAULT '0', // 父菜单
`WECHAT_ID` bigint(20) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
pojo
public class WechatMenu {
private transient Integer id;//JSON序列化时忽略此字段
private String url;
private String type;
private String name;
private String key;
private transient Integer pid;//JSON序列化时忽略此字段
private List<WechatMenu> sub_button = new ArrayList<WechatMenu>();
}
dao
@Override
public List<WechatMenu> queryWechatMenuAndSub(@Param("wechatId") Integer wechatId) {
return sqlSessionTemplate.selectList("weChatMenuDao.queryWechatMenuAndSub", wechatId);
}
mapper.xml
<resultMap id="wechatMenuResultMap" type="com.suning.epps.fmmb.dmo.wechat.WechatMenu">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="type" column="type"/>
<result property="key" column="key"/>
<result property="url" column="url"/>
<collection column="id" property="sub_button" ofType="com.suning.epps.fmmb.dmo.wechat.WechatMenu"
select="queryWechatMenuByPid"/>
</resultMap>
<select id="queryWechatMenuAndSub" resultMap="wechatMenuResultMap">
select t.ID,t.TYPE,t.NAME,t.KEY,t.URL from t_wechat_menu t where PID=0 and wechat_id=#{wechatId}
</select>
<select id="queryWechatMenuByPid" resultMap="wechatMenuResultMap" parameterType="String">
select t.ID,t.TYPE,t.NAME,t.KEY,t.URL from t_wechat_menu t where PID=#{id}
</select>
service
// 转成json
String json = JSON.toJSONString(menu, SerializerFeature.SkipTransientField);
Spring+Mybatis 复杂的分组查询的更多相关文章
- spring data jpa条件分组查询及分页
原book对象 package com.shaying.domain; import javax.persistence.Column; import javax.persistence.Entity ...
- Spring Data Solr的分组查询 for 搜索面板的商品分类
private List searchCategoryList(Map searchMap) { SimpleQuery query = new SimpleQuery(new Criteria(&q ...
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...
- Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询
在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...
- Idea SpringMVC+Spring+MyBatis+Maven调整【转】
Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...
- SpringMVC+Spring+MyBatis+Maven调整【转】
Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...
- Spring+Mybatis基于注解整合Redis
基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- 2.springMVC+spring+Mybatis整合
前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...
随机推荐
- webpack4 搭建遇到的奇葩问题集合
一.webpack4 打包es6 会报错 需要安装一下插件 https://blog.csdn.net/Beamon__/article/details/85048448二.webpack4 打包动态 ...
- 用JS实现实时显示系统时间
废话我就不多说了,直接上图和代码了 效果图: 代码视图: 下面为大家附上代码,复制即可用: <!DOCTYPE html> <html lang="en"> ...
- promise async
最简用 promise let res = function () { return new Promise((resolve, reject) => { // 返回一个promise set ...
- Angular项目中核心模块core Module只加载一次的实现
核心模块CoreModule在整个系统中只加载一次,如何实现? 创建core Modele:ng g m core 既然CoreModule是类,就有构造函数,在构造函数中进行依赖注入. export ...
- 527D.Clique Problem
题解: 水题 两种做法: 1.我的 我们假设$xi>xj$ 那么拆开绝对值 $$xi-w[i]>x[j]+w[j]$$ 由于$w[i]>0$,所以$x[i]+w[i]>x[j] ...
- 在SOUI中使用线性布局
SOUI 2.5.1.1开始支持线性布局(LinearLayout). 要在SOUI布局中使用线性布局, 需要在布局容器窗口里指定布局类型为vbox | hbox, (vbox为垂直线性布局, hbo ...
- net core体系-web应用程序-4asp.net core2.0 项目实战(1)-12基于cookie登录授权认证并实现前台会员、后台管理员同时登录
1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...
- for in和for of的区别(转)
原文链接:https://www.jianshu.com/p/c43f418d6bf0 1 遍历数组通常用for循环 ES5的话也可以使用forEach,ES5具有遍历数组功能的还有map.filte ...
- UOJ#460. 新年的拯救计划 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ460.html 题解 本题的构造方法很多.这里只介绍一种. 首先,总边数为 $\frac{n(n-1)}2 ...
- 安卓开发app在后台运行时页面数据被系统清除后操作之重启APP
在安卓开发过程中,当点击HOME键,将app运行在后台时,然后再点击app图标进入时,遇到了如下两种情况: 1.每次打开时,app的入口页面总是被执行. 2.当运行内存被其它应用占用完时,在进入app ...