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

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

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

查看所有标签

猜你喜欢:

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

可用性工程

可用性工程

尼尔森 / 刘正捷 / 机械工业出版社 / 2004-1 / 28.00元

《可用性工程》系统地介绍可用性工程,被国际可用性工程界一致推崇为该领域的最佳入门书籍。《可用性工程》着重讲述了能取得良好成本效益的可用性方法,并详细介绍了在软件开发生命周期的不同阶段如何运用这些方法,以及其他与可用性相关的特殊问题。一起来看看 《可用性工程》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具