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

mapper.xml文件




    
        
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

    
      id, closeorder, account, symbol, cmd, Volume, OpenTime, OpenPrice, SL, TP, Magic,
      Comment, timestamp, Profit, ClosePrice, Digits, Storage
    

    

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 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 selectSignalSource() {
        //取出数据
        List closeOrderLists=closeOrderMapper.selectCloseOrderList();
        //数据分组细化
        Map> map=new HashedMap();
        closeOrderLists.forEach(closeOrderList ->{
            String account=closeOrderList.getAccount();
            List 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 closeOrderList;

}

上面是整个操作文件,下面讲解:

在mapper.xml文件中resultMap(customResultMap)配置详情:

上面的代码为设置按照分组的字段,按照account字段进行分组


    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

上面的collection为按照id中字段account分组后形成的集合的定义


//实体类中的定义
private List closeOrderList;

上面的collection中property属性值对应的CloseOrderList实体类中的定义的名字closeOrderList


上面ofType对应的值为为CloseOrder实体类,为collection中具体的值

上面的sql语句中此时不需要在进行group by操作,因为在resultMap中已经进行了分组操作了

List selectCloseOrderList();

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

你可能感兴趣的:(mybatis从数据库中取数据且分组,返回分组数据)