1.
com.github.pagehelper
pagehelper
4.1.6
2. MybatisPageHelperConf.java
@Configuration
public class MybatisPageHelperConf {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
3. PageUtility.java
public class PageUtility {
public static Map buildPageResult(PageInfo o){
Map m = new HashMap();
m.put("pageNum",o.getPageNum());
m.put("pageSize",o.getPageSize());
m.put("total",o.getTotal());
m.put("data",o.getList());
return m;
}
}
4. 服务层:
public Map queryOrgByPage(Map map){
if(map.get("pageNum") != null && map.get("pageSize") != null){
int pageNum = MapUtility.getMapInt(map, "pageNum");
int pageSize = MapUtility.getMapInt(map, "pageSize");
System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize);
PageHelper.startPage(pageNum, pageSize);
}
List
query = sysOrganizationMapper.queryAll();
return PageUtility.buildPageResult(new PageInfo(query));
}
返回就是翻页好的数据
5. 多数据源的情况,需要处理:
把MybatisPageHelperConf 中的代码复制一下:
@Bean
public SqlSessionFactory sqlSessionFactory(
@Qualifier("ADataSource") DataSource ADataSource
) throws Exception{
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
fb.setDataSource(this.dataSource(ADataSource));
fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));
fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
fb.setPlugins(new Interceptor[]{pageHelper});
return fb.getObject();
}