基于SpringBoot+Mybatis或List集合+Pagehelper实现分页
基于SpringBoot+Mybatis+Pagehelper实现分页功能1、导入分页需要的相关jar包<!-- pagehelper --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</
·
基于SpringBoot+Mybatis+Pagehelper实现分页功能
1、导入分页需要的相关jar包
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2、Mybatis中的查询语句(值得注意的是根据 条件,可为空;根据自己的实际需求决定
查询的是全部数据)
<select id="selectAllByCondition" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select * from table_name where id= #{id,jdbcType=INTEGER}
</select>
3、持久层接口(mapper层接口),此处的 EntityName
为你需要查询的表映射的实体类名。下同。
public interface EntityNameMapper{
/*根据条件查询数据库表(table_name)中的全部数据*/
List<EntityName> selectAllByCondition(Integer id);
}
4、业务层接口及实现类(即:业务处理都放在这里)
/*业务层接口*/
public interface EntityNameService{
/*根据查询表的条件和分页条件进行分页处理*/
PageInfo<EntityName> getPageAllByCondition(Integer id,int pageNum,int pageSize);
}
/*业务层接口实现类*/
public class EntityNameService implement EntityNameServiceImpl{
@Autowired
private EntityNameMapper entityNameMapper;
/*根据查询表的条件和分页条件进行分页处理*/
public PageInfo<EntityName> getPageAllFromDBByCondition(Integer id,int pageNum,int pageSize){
//此处有对查询的拦截器,有查询才开始给Page赋值
Page page = PageHelper.startPage(pageNum,pageSize);
List<EntityName> entityNameList = entityNameMapper.selectAllByCondition(id);
for(int i = 0;i<entityNameList.size();i++){
page.set(i,entityNameList.get(i));
}
return new PageInfo<>(page);
}
/*根据查询的条件和分页条件进行分页处理 --对List集合分页,也可以对数据库查询分页 */
public PageInfo<entityName> getPageAllFromListByCondition(Integer id,int pageNum,int pageSize){
/*此处省略List的来源*/
List<EntityName> entityNameList = entityNameMapper.selectAllByCondition(id);
Page page = new page(pageNum,pageSize);
int total = entityNameList.size();
page.setTotal(total)
int startIndex = (pageNum - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, total);
if (total > startIndex) {
List<EntityName> ens = entityNameList .subList(startIndex, endIndex);
page.addAll(ens);
}
return new PageInfo<>(page,page.getPages());
}
}
5、控制层、或者其他层,调用即可。
…
…
…
更多推荐
已为社区贡献1条内容
所有评论(0)