首页 > 其他分享 >mybatis流式查询与分页插件

mybatis流式查询与分页插件

时间:2022-11-21 13:56:41浏览次数:61  
标签:插件 wanqi 流式 id org mybatis import com public

1、流式查询

image.png

1、实体类

package com.wanqi.pojo;

import java.util.Date;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/12
 * @Author wandaren
 */

public class VoteRecord {
    private int id;
    private String userId;
    private int voteNum;
    private int groupId;
    private boolean status;
    private Date createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getVoteNum() {
        return voteNum;
    }

    public void setVoteNum(int voteNum) {
        this.voteNum = voteNum;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "VoteRecord{" +
                "id=" + id +
                ", userId='" + userId + '\'' +
                ", voteNum=" + voteNum +
                ", groupId=" + groupId +
                ", status=" + status +
                ", createTime=" + createTime +
                '}';
    }
}

2、mapper

package com.wanqi.mapper;

import com.wanqi.pojo.VoteRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.ResultHandler;

import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/12
 * @Author wandaren
 */
@Mapper
public interface VoteRecordMapper {
//    @Select("select id, user_id as userId, vote_num as voteNum, group_id as groupId, status, create_time as createTime from vote_record")
//    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = Integer.MIN_VALUE)
//    @ResultType(VoteRecord.class)
    void all(ResultHandler<VoteRecord> resultHandler);
}

3、mapper配置

<?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.wanqi.mapper.VoteRecordMapper">

        <select id="all" resultType="com.wanqi.pojo.VoteRecord" resultSetType="FORWARD_ONLY"
                fetchSize="-2147483648">
            select id, user_id as userId, vote_num as voteNum, group_id as groupId, status, create_time as createTime
            from vote_record
        </select>

</mapper>

image.png
:::info
this.query.getResultType() == Type.FORWARD_ONLY
this.query.getResultFetchSize() == Integer.MIN_VALUE�
:::

  • fetchSize必须设置成-2147483648

4、自定义处理结果集

package com.wanqi.handler;

import com.wanqi.pojo.VoteRecord;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/12
 * @Author wandaren
 */
public class VoteRecordHandler implements ResultHandler<VoteRecord> {
    // 这是每批处理的大小
    private final static int BATCH_SIZE = 1000;
    private int size;
    private final List<VoteRecord> list = new ArrayList<>();

    @Override
    public void handleResult(ResultContext<? extends VoteRecord> resultContext) {
        VoteRecord voteRecord = resultContext.getResultObject();
        list.add(voteRecord);
        size++;
        if (size == BATCH_SIZE) {
            handle();
        }
    }


    private void handle() {
        try {
            if (ObjectUtils.isEmpty(list)) {
                return;
            }
            // 在这里可以对你获取到的批量结果数据进行需要的业务处理
//            list.parallelStream().forEach(s -> System.out.println(s.getId()));
        } finally {
            // 处理完每批数据后后将临时清空
            size = 0;
            list.clear();
        }
    }

    // 这个方法给外面调用,用来完成最后一批数据处理
    public void end(){
        handle();// 处理最后一批不到BATCH_SIZE的数据
    }
}

5、service层

package com.wanqi.service;

import com.github.pagehelper.PageHelper;
import com.wanqi.handler.VoteRecordHandler;
import com.wanqi.mapper.VoteRecordMapper;
import com.wanqi.pojo.VoteRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/12
 * @Author wandaren
 */
@Service
public class VoteRecordService {
    @Autowired
    private VoteRecordMapper voteRecordMapper;


    public String allVoteRecord(){
        VoteRecordHandler voteRecordHandler = new VoteRecordHandler();
        voteRecordMapper.all(voteRecordHandler);
        System.out.println("--------------------------------------------------------");
        voteRecordHandler.end();
        return "处理结束!!!";
    }
}

2、分页插件

1、引入依赖

		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.4.3</version>
		</dependency>

2、配置文件

spring:
  redis:
    host: 172.16.156.139
    password: qifeng
    database: 2
    port: 6379
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://172.16.156.139:3306/test?allowPublicKeyRetrieval=true
    username: wq
    password: qifeng
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  type-aliases-package: com.wanqi.pojo
  mapper-locations: classpath:mapper/*.xml
#分页插件的配置
pagehelper:
  #配置四項 hprs
  helper-dialect: mysql  #分页助手方言:mysql
  params: count=countSql  #为了支持 startPage(Object params) 方法
  support-methods-arguments: true  #支持通过 Mapper 接口参数来传递分页参数,默认值 false
  reasonable: true #分页合理化参数,默认值为 false,页码不为负,不超总页码

3、mapper接口

package com.wanqi.mapper;

import com.wanqi.pojo.VoteRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.ResultHandler;

import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/12
 * @Author wandaren
 */
@Mapper
public interface VoteRecordMapper {
    List<VoteRecord> one();
}

4、mapper配置

<?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.wanqi.mapper.VoteRecordMapper">

  <select id="one" resultType="com.wanqi.pojo.VoteRecord">
        select id, user_id as userId, vote_num as voteNum, group_id as groupId, status, create_time as createTime
        from vote_record
    </select>

</mapper>

5、servlce层

package com.wanqi.service;

import com.github.pagehelper.PageHelper;
import com.wanqi.handler.VoteRecordHandler;
import com.wanqi.mapper.VoteRecordMapper;
import com.wanqi.pojo.VoteRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/12
 * @Author wandaren
 */
@Service
public class VoteRecordService {

    @Autowired
    private VoteRecordMapper voteRecordMapper;

    public void one(){
        PageHelper.startPage(1, 50000);
        List<VoteRecord> list = voteRecordMapper.one();
        PageInfo<VoteRecord> pageInfo = new PageInfo<>(list);
        System.out.println("getTotal: "+pageInfo.getTotal());
        System.out.println("getPages: "+pageInfo.getPages());
        System.out.println("getPageNum: "+pageInfo.getPageNum());
        System.out.println("getPageSize: "+pageInfo.getPageSize());
        System.out.println("getEndRow: "+pageInfo.getEndRow());
    }
}

标签:插件,wanqi,流式,id,org,mybatis,import,com,public
From: https://www.cnblogs.com/wandaren/p/16906569.html

相关文章

  • 2.3创建mybatis的逆向工程
    1.1创建mybatis的逆向工程1,同步请求和异步请求的区别:同步请求:浏览器窗口发出的请求,响应信息返回到浏览器窗口,所以会进行全局刷新。异步请求:ajax发出的请求,响应......
  • eslint插件屁事多,解决方案
    eslint插件会检测是否写了分号,写了分号就报错,那么我们就设置一个文件".prettierrc"文件在里面写"semi":false从而在格式化代码的时候自动将分号去掉;写"singleQuote"......
  • Mybatis查询返回Map
    1/**2*查询产品的医保名称、省标名称3*@paramproductIds4*@return5*/6@MapKey("productId")7publicMap<Long,Provinc......
  • 一个CONNECTION 插件引起的连接问题
    一个MYSQLCONNECTION插件引起的连接问题,应用反应,数据库帐户经常连接不上,时好时坏。而且手动测试,也是hang在这里.且数据库错误日志并无异常。目......
  • IDEA中给源码添加自己注释——private-notes插件安装使用
    一、前言我们在空闲之余喜欢研究一些经典框架的源码,发现没办法把自己的注释添加上。会给出提示:​​​Fileisread-only​​​很烦,但是为了安全考虑也是没有办法的!这是一......
  • gradle-docker-plugin插件一键发布镜像
    官网地址https://bmuschko.github.io/gradle-docker-plugin/current/user-guide/#introductionSpringboot项目使用plugins{id'java'id'org.springframew......
  • Spring整合Mybatis(注解版)
    Spring整合Mybatis入门(注解版)步骤一:创建maven项目在pom.xml文件中导入以下坐标<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/......
  • MyBatis - 基础学习7 - 多表查询
    一.按照查询嵌套处理1.写接口List<Student>getstudent(); 2.在mapper.xml中写相关的sql语句<!--思路:1.查询所有的学生信息2.根据查询出来的学生的tid,寻找对......
  • mybatisplus和java8一些常用方法总结
    mybatis-plus:1、Stream.of();用于为给定元素创建顺序流。我们可以传递单个元素或多个元素。 2、Optional+Assert优雅判空  3、Optional+ MapUtils组合使用如果要使......
  • 027.1Mybatis注解开发方式
    1.常用注解  2.使用流程2.1  com.imooc.mybatis.dao包下创建接口packagecom.imooc.mybatis.dao;importcom.imooc.mybatis.dto.GoodsDTO;importcom.imooc.......