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

02/08 20:08
阅读数 8

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();

上面为接收的时候以封装的集合进行接收

原文出处:https://www.cnblogs.com/eternityz/p/12284808.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部