内容简介:在mapper.xml文件中resultMap(customResultMap)配置详情:上面的代码为设置按照分组的字段,按照account字段进行分组上面的collection为按照id中字段account分组后形成的集合的定义
mapper.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kflh.boxApi.chooseSignalSource.dao.CloseOrderMapper">
<resultMap id="customResultMap" type="com.kflh.boxApi.chooseSignalSource.entity.CloseOrderList">
<id property="account" column="account"/>
<collection property="closeOrderList" ofType="com.kflh.boxApi.chooseSignalSource.entity.CloseOrder">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="closeorder" jdbcType="INTEGER" property="closeOrder"/>
<result column="account" jdbcType="INTEGER" property="account"/>
<result column="symbol" jdbcType="VARCHAR" property="symbol"/>
<result column="cmd" jdbcType="TINYINT" property="cmd"/>
<result column="Volume" jdbcType="DOUBLE" property="volume"/>
<result column="OpenTime" jdbcType="INTEGER" property="openTime"/>
<result column="OpenPrice" jdbcType="DECIMAL" property="openPrice"/>
<result column="SL" jdbcType="DECIMAL" property="sl"/>
<result column="TP" jdbcType="DECIMAL" property="tp"/>
<result column="Magic" jdbcType="INTEGER" property="magic"/>
<result column="Comment" jdbcType="VARCHAR" property="comment"/>
<result column="timestamp" jdbcType="INTEGER" property="timestamp"/>
<result column="Profit" jdbcType="DECIMAL" property="profit"/>
<result column="ClosePrice" jdbcType="DECIMAL" property="closePrice"/>
<result column="Digits" jdbcType="TINYINT" property="digits"/>
<result column="Storage" jdbcType="VARCHAR" property="storage"/>
</collection>
</resultMap>
<sql id="Base_Column_List">
id, closeorder, account, symbol, cmd, Volume, OpenTime, OpenPrice, SL, TP, Magic,
Comment, timestamp, Profit, ClosePrice, Digits, Storage
</sql>
<select id="selectCloseOrderList" resultMap="customResultMap">
select
<include refid="Base_Column_List"/>
from closeorder where account in(select account from mt4list_rel)
</select>
</mapper>
dao文件
package com.kflh.boxApi.chooseSignalSource.dao;
import com.kflh.boxApi.chooseSignalSource.entity.CloseOrder;
import com.kflh.boxApi.chooseSignalSource.entity.CloseOrderList;
import java.util.List;
import java.util.Map;
public interface CloseOrderMapper {
List<CloseOrderList> selectCloseOrderList();
}
CloseOrderServiceImpl文件
package com.kflh.boxApi.chooseSignalSource.service.impl;
import com.kflh.boxApi.chooseSignalSource.entity.CloseOrder;
import com.kflh.boxApi.chooseSignalSource.dao.CloseOrderMapper;
import com.kflh.boxApi.chooseSignalSource.entity.CloseOrderList;
import com.kflh.boxApi.chooseSignalSource.service.CloseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @program: BoxApi
* @description:
* @author: TheEternity Zhang
* @create: 2019-02-27 09:16
*/
@Slf4j
@Service
public class CloseOrderServiceImpl implements CloseOrderService {
@Autowired
CloseOrderMapper closeOrderMapper;
@Override
public Map<String, Object> selectSignalSource() {
//取出数据
List<CloseOrderList> closeOrderLists=closeOrderMapper.selectCloseOrderList();
//数据分组细化
Map<String,List<CloseOrder>> map=new HashedMap();
closeOrderLists.forEach(closeOrderList ->{
String account=closeOrderList.getAccount();
List<CloseOrder> list=closeOrderList.getCloseOrderList();
map.put(account,list);
} );
log.info("list.size={}",closeOrderLists.size());
return null;
}
}
CloseOrder文件
package com.kflh.boxApi.chooseSignalSource.entity;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class CloseOrder {
private Integer id;
private Integer closeOrder;
private Integer account;
private String symbol;
private Byte cmd;
private Double volume;
private Integer openTime;
private BigDecimal openPrice;
private BigDecimal sl;
private BigDecimal tp;
private Integer magic;
private String comment;
private Integer timestamp;
private BigDecimal profit;
private BigDecimal closePrice;
private Byte digits;
private String storage;
}
CloseOrderList文件
package com.kflh.boxApi.chooseSignalSource.entity;
import lombok.Data;
import java.util.List;
/**
* @program: BoxApi
* @description:
* @author: TheEternity Zhang
* @create: 2019-02-27 15:19
*/
@Data
public class CloseOrderList {
private String account;
private List<CloseOrder> closeOrderList;
}
上面是整个操作文件,下面讲解:
在mapper.xml文件中resultMap(customResultMap)配置详情:
<id property="account" column="account"/>
上面的代码为设置按照分组的字段,按照account字段进行分组
<collection property="closeOrderList" ofType="com.kflh.boxApi.chooseSignalSource.entity.CloseOrder">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="closeorder" jdbcType="INTEGER" property="closeOrder"/>
<result column="account" jdbcType="INTEGER" property="account"/>
<result column="symbol" jdbcType="VARCHAR" property="symbol"/>
<result column="cmd" jdbcType="TINYINT" property="cmd"/>
<result column="Volume" jdbcType="DOUBLE" property="volume"/>
<result column="OpenTime" jdbcType="INTEGER" property="openTime"/>
<result column="OpenPrice" jdbcType="DECIMAL" property="openPrice"/>
<result column="SL" jdbcType="DECIMAL" property="sl"/>
<result column="TP" jdbcType="DECIMAL" property="tp"/>
<result column="Magic" jdbcType="INTEGER" property="magic"/>
<result column="Comment" jdbcType="VARCHAR" property="comment"/>
<result column="timestamp" jdbcType="INTEGER" property="timestamp"/>
<result column="Profit" jdbcType="DECIMAL" property="profit"/>
<result column="ClosePrice" jdbcType="DECIMAL" property="closePrice"/>
<result column="Digits" jdbcType="TINYINT" property="digits"/>
<result column="Storage" jdbcType="VARCHAR" property="storage"/>
</collection>
上面的collection为按照id中字段account分组后形成的集合的定义
<collection property="closeOrderList" ofType="com.kflh.boxApi.chooseSignalSource.entity.CloseOrder"> //实体类中的定义 private List<CloseOrder> closeOrderList;
上面的collection中property属性值对应的CloseOrderList实体类中的定义的名字closeOrderList
<collection property="closeOrderList" ofType="com.kflh.boxApi.chooseSignalSource.entity.CloseOrder">
上面ofType对应的值为为CloseOrder实体类,为collection中具体的值
<select id="selectCloseOrderList" resultMap="customResultMap">
select
<include refid="Base_Column_List"/>
from closeorder where account in(select account from mt4list_rel)
</select>
上面的 sql 语句中此时不需要在进行group by操作,因为在resultMap中已经进行了分组操作了
List<CloseOrderList> selectCloseOrderList();
上面为接收的时候以封装的集合进行接收
以上所述就是小编给大家介绍的《mybatis从数据库中取数据且分组,返回分组数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Mysql高级查询-----汇总和分组数据
- 苞米豆-多数据源启动器 2.0.0 全新发布:无限制分组
- 苞米豆-多数据源启动器 2.0.0 全新发布:无限制分组
- Java8中使用stream进行分组统计和普通实现的分组统计的性能对比
- MongoDB 分组统计
- MySQL——分组查询
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Namo Webeditor5.5一看就懂.
吳聲毅 / 金禾資訊 / 20040214 / NT$ 169
一看就懂系列書全以初學者的角度切入,全書以STEP BY STEP方式撰寫,並以豐富的圖片搭配教學,在最後更加上日常生活實例運用講解,一路學來一氣呵成。為了增進學習的效率更採用高級紙品全彩印刷,這麼好的書,您還在等什麼,一看就懂系列書保證是您最佳入門學習好伙伴。 本書特色: 1、一看就懂:Step by Step操作詳盡說明、讓您一看就懂 2、精選範例:精彩實務範例生動活......一起来看看 《Namo Webeditor5.5一看就懂.》 这本书的介绍吧!