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. Node中包的加载机制

      1.Node原生提供的核心模块:http.fs.os.path.url.querystring,Node在加载核心模块时,不需要传入路径,因为Node.js已经将核心模块的文件代码编译到了二进制的 ...

  2. Java_String&StringBuilder&StringBuffer类

    目录 一.String类 二."==" 和 "equals"的区别 三.StringBuffer和StringBuilder 一.String类 String为 ...

  3. 利用jQuery如何获取当前被点击的按钮

    如下代码 <tr> <td><a href="javascript:void(0)">点我1</a></td> < ...

  4. 利用Centos服务器来搭建自己的splash,不再被安装的各种环境繁琐而担忧

    Centos7.5 ----- docker ------- splash               第一步:服务器环境的配置               第二步:dcoker环境的配置       ...

  5. 一起学爬虫——使用Beautiful Soup爬取网页

    要想学好爬虫,必须把基础打扎实,之前发布了两篇文章,分别是使用XPATH和requests爬取网页,今天的文章是学习Beautiful Soup并通过一个例子来实现如何使用Beautiful Soup ...

  6. vue 获取dom的css属性值

    <template> <div id="topInfo" ref="topInfo" style="width: 200px;hei ...

  7. AWS S3 递归上传文件和递归下载文件, 以及S3之间拷贝文件夹

    1. 递归上传文件: aws s3 cp 本地文件夹 s3://bucket-name -- recursive --region us-east-1 2. 递归下载S3上的文件夹: cd  本地下载 ...

  8. (二)文档请求不同源之flash跨域

    对于FLash,IE是认object标签的,但是FF和Chrome是认embed标签的. 1.客户端 <param name="allowScriptAccess" valu ...

  9. SDOI2018:荣誉称号

    题解: https://files.cnblogs.com/files/clrs97/title-solution.pdf Code: #include<cstdio> #include& ...

  10. [LeetCode] Split BST 分割二叉搜索树

    Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...