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