詳解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(); } } 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()); } }
到此這篇關於mybatis流式查詢與分頁插件的文章就介紹到這瞭,更多相關mybatis分頁插件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- mybatis mapper互相引用resultMap啟動出錯的解決
- MyBatis中多條件查詢商品的三種方法及區別
- mybatisplus @Select註解中拼寫動態sql異常問題的解決
- Mybatis結果集映射與生命周期詳細介紹
- 關於Mybatis的sql註釋問題