mybatis從數據庫中取數據且分組,返回分組數據


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

上面為接收的時候以封裝的集合進行接收


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM