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 复杂的分组查询的更多相关文章

  1. spring data jpa条件分组查询及分页

    原book对象 package com.shaying.domain; import javax.persistence.Column; import javax.persistence.Entity ...

  2. Spring Data Solr的分组查询 for 搜索面板的商品分类

    private List searchCategoryList(Map searchMap) { SimpleQuery query = new SimpleQuery(new Criteria(&q ...

  3. 【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 ...

  4. Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询

    在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...

  5. Idea SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  6. SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  7. Spring+Mybatis基于注解整合Redis

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...

  8. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  9. 2.springMVC+spring+Mybatis整合

    前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...

随机推荐

  1. iOS rebuild from bitcode对ipa大小的影响

    https://developer.apple.com/library/content/technotes/tn2432/_index.html 为了测试一下rebuild from bitcode的 ...

  2. git命令之git remote的用法

    git remote git  remote -v git init git add xxx git commit -m 'xxx' git remote add origin ssh://softw ...

  3. 微信H5支付坑一--手续费未结算

    简单随笔小记: 场景:在微信H5支付的过程中,无论怎么支付完成,在微信商户后台查询手续费依然未扣除,当时手续费账户月为5元. 解决方法:起初无论怎么测试都不知道代码到底问题出在哪里了,想一下手续费账户 ...

  4. iOS开发之微信平台分享

    在工程开始之前应该先准备在微信开放平台申请的appid,从微信平台下载sdk文件.下面开始步骤讲述 1.先将SDK导入工程目录 2.在info.plist文件设置相关信息,包括appid标识.白名单 ...

  5. Jmeter性能测试之基础知识(一)

    1. 官网下载Jmeter: 点这里, 下载完成解压即可 2. 启动: 进入解压后的bin目录, Windows点击jmeter.bat, Linux执行jmeter 3. 添加线程组(user) : ...

  6. json server服务器

    json文件可以理解为数据库 一.json-server快速搭建RESTAPI 安装: sudo cnpm install -g json-server 启动(使用): json-server指向js ...

  7. 【Android】PreferenceActivity 详解

    PreferenceActivity是专业的设置界面,只要给它指定一个配置好的xml,它就能自动根据操作更改程序Preference的相应值. 首先要用一个xml文件来配置一个设置界面,也就是我们说的 ...

  8. python输入

    (程序是如何输入输出的) 先了解一个概念,什么是函数? 简单来说,函数就是封装了一些功能,到时候只需要写一个函数名字,就可以使用这些功能 input函数,它是输入函数,它可以将用户输入的内容当做“字符 ...

  9. Linux命令行参数前加--,-和不加杠

    参数前“-”的表明后面的参数是字符形式. 参数前“--”的则表明后面的参数是单词形式. 参数前有横的是System V风格. 参数前没有横的是BSD风格. 

  10. [CF542A]Place Your Ad Here

    [CF542A]Place Your Ad Here 题目大意: 有\(n(n\le2\times10^5)\)个广告和\(m(m\le2\times10^5)\)个电视台,第\(i\)个广告只能在\ ...