java中List分页的几种方法介绍
作者:chatty 时间:2022-03-01 12:04:28
1.根据入参带分页参数进行sql查询分页
Criteria criteria = new Criteria();
//将dataAuto转成 factoryId brandId seriesId 等查询条件
String dataAuth = "";
TypeCaseHelper.dataAuto(criteria, dataAuth);
// 设置分页信息
ExtPager pager = new ExtPager();
Integer startTure = start * pageSize;
criteria.setMysqlPageSize(pageSize);
criteria.setMysqlStart(startTure);
// 排序信息
if (StringUtils.isNotBlank(pager.getDir()) && StringUtils.isNotBlank(pager.getSort())) {
criteria.setOrderByClause(pager.getSort() + " " + pager.getDir());
}
List<VhlAlarmStatusMgtEntity> listDistinct = getVhlAlarmStatusMgtEntities(vin, faultStatus, confirmStatus, startDateTime, endDateTime, sdf, sdfm, carTypeList, criteria);
对应的分页sql
<select id="selectByExample" parameterType="Criteria" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from status_mgt
<where>
<if test="condition.carTypeList != null and condition.carTypeList.size() > 0" >
and car_type in
<foreach collection="condition.carTypeList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="condition.startDateTime != null and condition.startDateTime!=''">
and alarm_time >= str_to_date(#{condition.startDateTime,jdbcType=VARCHAR},'%Y-%m-%d %H:%i:%S')
</if>
<if test="condition.endDateTime != null and condition.endDateTime!=''">
and str_to_date(#{condition.endDateTime,jdbcType=VARCHAR},'%Y-%m-%d %H:%i:%S') >= alarm_time
</if>
and logic_flag = 1
</where>
order by alarm_time desc
<if test="mysqlStart != null and mysqlPageSize != null">
limit #{mysqlStart}, #{mysqlPageSize}
</if>
</select>
2.对所有list根据分页参数分页
Criteria criteria1 = new Criteria();
List<VhlAlarmStatusMgtEntity> listDistinctForCout = getVhlAlarmStatusMgtEntities(vin, faultStatus, confirmStatus, startDateTime, endDateTime, sdf, sdfm, carTypeList, criteria1);
Integer count = listDistinctForCout.size(); // 记录总数
Integer pageCount; // 页数
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex; // 开始索引
int toIndex; // 结束索引
if (!pageCount.equals(start+1)) {
fromIndex = start * pageSize;
toIndex = fromIndex + pageSize;
if(toIndex > count){
fromIndex = (start-1) * pageSize;
toIndex = count;
}
} else {
fromIndex = start * pageSize;
toIndex = count;
}
List<VhlAlarmStatusMgtEntity> pageList = listDistinctForCout.subList(fromIndex, toIndex);
3.PageHelper分页
//开启分页
PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
PageInfo<Map<String, String>> pageInfo = new PageInfo(datalist);
HashMap<String, Object> map= new HashMap<>();
map.put("datalist", datalist);
map.put("total", pageInfo.getTotal());
map.put("size", pageInfo.getPageSize());
map.put("page", pageInfo.getPageNum());
来源:https://blog.csdn.net/qq_41429436/article/details/121660890
标签:java,List
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Hibernate实现批量添加数据的方法
2023-11-29 08:53:56
![](https://img.aspxhome.com/file/2023/5/60815_0s.jpg)
Log4j新手快速入门教程
2023-06-01 11:59:44
![](https://img.aspxhome.com/file/2023/0/91740_0s.png)
spring cloud整合ribbon问题及解决方案
2023-07-25 04:24:49
![](https://img.aspxhome.com/file/2023/3/57823_0s.png)
Python安装Jupyter Notebook配置使用教程详解
2023-08-31 02:52:03
![](https://img.aspxhome.com/file/2023/2/92722_0s.jpg)
使用springCloud+nacos集成seata1.3.0搭建过程
2022-06-19 02:48:47
![](https://img.aspxhome.com/file/2023/7/63397_0s.png)
java中调用super的实例讲解
2021-12-13 01:01:12
Springboot日志开启SLF4J过程解析
2022-04-23 01:29:57
解决使用RestTemplate时报错RestClientException的问题
2023-05-27 19:46:36
SpringBoot如何接收Post请求Body里面的参数
2023-07-30 13:43:35
java使用ftp上传文件示例分享
2021-10-23 08:33:03
javaWeb使用servlet搭建服务器入门
2023-11-21 04:47:45
使用Spring开启注解AOP的支持放置的位置
2022-08-16 19:16:33
全面解析SpringBoot文件上传功能
2023-02-26 15:55:56
![](https://img.aspxhome.com/file/2023/2/67462_0s.png)
关于Spring事务隔离、传播属性与@Transactional注解
2021-11-15 04:37:11
![](https://img.aspxhome.com/file/2023/7/76227_0s.png)
Java Email邮件发送简单实现介绍
2023-10-07 01:05:11
![](https://img.aspxhome.com/file/2023/8/62838_0s.png)
解决RestTemplate第一次请求响应速度较慢的问题
2021-10-20 15:50:50
Java Web实现简易图书管理系统
2023-12-17 21:48:34
![](https://img.aspxhome.com/file/2023/2/85432_0s.jpg)
Java数据结构之线索化二叉树的实现
2022-03-16 15:25:34
![](https://img.aspxhome.com/file/2023/1/75021_0s.png)
java中map和对象互转工具类的实现示例
2023-06-05 11:08:30
![](https://img.aspxhome.com/file/2023/2/62162_0s.png)
Android实现内存中数据保存到sdcard的方法
2022-06-12 21:02:09