mybatis从数据库中取数据且分组,返回分组数据

栏目: IT技术 · 发布时间: 6年前

内容简介:在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从数据库中取数据且分组,返回分组数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Git

Pro Git

Scott Chacon / Apress / 2009-8-27 / USD 34.99

Git is the version control system developed by Linus Torvalds for Linux kernel development. It took the open source world by storm since its inception in 2005, and is used by small development shops a......一起来看看 《Pro Git》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试