1.在Maven中加入依赖:
...
com.github.miemiedev
mybatis-paginator
1.2.17
...
3.Dao层方法:
public interface ProductDao {
PageList getList(Map params, PageBounds pageBounds);
}
Service层方法:
@Service
public class ProductService implements ProductDao {
@Autowired
private ProductDao productDao;
@Override
public PageList getList(Map params, PageBounds pageBounds) {
return productDao.getList(params,pageBounds);
}
}
Mapper文件:
public class PageTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new FileSystemXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void test(){
ProductDao productDao = (ProductDao) applicationContext.getBean("productService");
Map params = new HashMap<>();
params.put("vendId",0);//map传参,与分页插件无关
int page = 2;//分页插件页数,第几页
int pageSize = 5;//分页插件一页显示的条数
String sortString = "vend_city.desc,prod_price.asc";//分页插件中的排序Order.formString(sortString)
PageBounds pageBounds = new PageBounds(page,pageSize, Order.formString(sortString));//构造分页插件传参对象
PageList products = productDao.getList(params,pageBounds);
System.out.println(products.getPaginator().getTotalCount());//得到总条数
System.out.println(products.getPaginator().getTotalPages());//得到总页数
System.out.println("-------------------------------------------------------");
for(Product product:products){
System.out.println(product);
}
}
}
调用方式详解:
PageList类是继承于ArrayList
使用PageBounds这个对象来控制结果的输出,常用的使用方式一般都可以通过构造函数来配置。
new PageBounds();//默认构造函数不提供分页,返回ArrayList
new PageBounds(int limit);//取TOPN操作,返回ArrayList
new PageBounds(Order... order);//只排序不分页,返回ArrayList
new PageBounds(int page, int limit);//默认分页,返回PageList
new PageBounds(int page, int limit, Order... order);//分页加排序,返回PageList
new PageBounds(int page, int limit, List orders, boolean containsTotalCount);//使用containsTotalCount来决定查不查询totalCount,即返回ArrayList还是PageList
如果用如下方法设置pageBounds,当前这个查询就可以用两个线程同时查询list和totalCount了
pageBounds.setAsyncTotalCount(true);
如果所有的分页查询都是用异步的方式查询list和totalCount,可以在插件配置加入asyncTotalCount属性
但是你仍然可以用下面代码强制让这个查询不用异步
pageBounds.setAsyncTotalCount(false);
当然需要注意的是,只要你用到了异步查询,由于里面使用了线程池,所以在使用时就要加入清理监听器,以便在停止服务时关闭线程池。需要在web.xml中加入
com.github.miemiedev.mybatis.paginator.CleanupMybatisPaginatorListener
其他配置:
如果用的是SpringMVC的话可以把JSON的配置写成这样:
@ResponseBody
@RequestMapping(value = "/findByCity.json")
public List findByCity(@RequestParam String city,
@RequestParam(required = false,defaultValue = "1") int page,
@RequestParam(required = false,defaultValue = "30") int limit,
@RequestParam(required = false) String sort,
@RequestParam(required = false) String dir) {
return userService.findByCity(city, new PageBounds(page, limit, Order.create(sort,dir)));
}
{
"items":[
{"NAME":"xiaoma","AGE":30,"GENDER":1,"ID":3,"CITY":"BeiJing"},
{"NAME":"xiaoli","AGE":30,"SCORE":85,"GENDER":1,"ID":1,"CITY":"BeiJing"},
{"NAME":"xiaowang","AGE":30,"SCORE":92,"GENDER":0,"ID":2,"CITY":"BeiJing"},
{"NAME":"xiaoshao","AGE":30,"SCORE":99,"GENDER":0,"ID":4,"CITY":"BeiJing"}
],
"slider": [1, 2, 3, 4, 5, 6, 7],
"hasPrePage": false,
"startRow": 1,
"offset": 0,
"lastPage": false,
"prePage": 1,
"hasNextPage": true,
"nextPage": 2,
"endRow": 30,
"totalCount": 40351,
"firstPage": true,
"totalPages": 1346,
"limit": 30,
"page": 1
}
在SpringMVC中使用JSTL的话可以参考一下步骤(懒人用法)
在Spring配置文件中加入拦截器,或则参考拦截器实现定义自己的拦截器
然后Controller方法可以这样写
@RequestMapping(value = "/userView.action")
public ModelAndView userView(@RequestParam String city,
@RequestParam(required = false,defaultValue = "1") int page,
@RequestParam(required = false,defaultValue = "30") int limit,
@RequestParam(required = false) String sort,
@RequestParam(required = false) String dir) {
List users = userService.findByCity(city, new PageBounds(page, limit, Order.create(sort,dir)));
return new ModelAndView("account/user","users", users);
}
JSP中就可以这样用了,拦截器会将PageList分拆添加
Paginator属性,默认命名规则为"原属性名称"+"Paginator"
${user['ID']}
${user['NAME']}
${user['AGE']}
上一页: ${usersPaginator.prePage}
当前页: ${usersPaginator.page}
下一页: ${usersPaginator.nextPage}
总页数: ${usersPaginator.totalPages}
总条数: ${usersPaginator.totalCount}
更多属性参考Paginator类提供的方法
【end】