mybatis流式查询与分页插件()

  本篇文章为你整理了mybatis流式查询与分页插件()的详细内容,包含有 mybatis流式查询与分页插件,希望能帮助你了解 mybatis流式查询与分页插件。

  1、流式查询

  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

  

 

  
 

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

  
// 在这里可以对你获取到的批量结果数据进行需要的业务处理

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

  

 

  以上就是mybatis流式查询与分页插件()的详细内容,想要了解更多 mybatis流式查询与分页插件的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: