java中List分页的几种方法介绍
短信预约 -IT技能 免费直播动态提醒
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());
到此这篇关于java中List分页的几种方法介绍的文章就介绍到这了,更多相关java List分页方法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341