PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql,甲骨文,mariaDB, DB2, SQLite, Hsqldb等。
网址:https://pagehelper.github。io/本项目在github的项目地址:https://github.com/pagehelper/Mybatis-PageHelper本项目在gitosc的项目地址:http://git.oschina.net/free/Mybatis_PageHelper
1。在砰的一声。xml中添加如下依赖:
<代码> & lt; dependency> & lt; groupId> com.github.pagehelper & lt; artifactId> pagehelper & lt;/dependency> 代码>
-
<李>
在MyBatis配置xml中配置拦截器插件(dao层,配置在sqlSessionFactory里)
<代码> & lt; !——插件在配置文件中的位置必须符合要求,否则会报错,顺序如下: 属性呢?、设置? typealias吗?、typeHandlers吗? objectFactory ?, objectWrapperFactory吗? 插件吗? 环境吗?,databaseIdProvider ?映射器? ——比; & lt;属性名=安寮北? & lt; array> & lt; bean类=癱om.github.pagehelper.PageInterceptor”比; & lt; !——这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置——比; & lt;属性名="属性"比; & lt; value> helperDialect=mysql 合理=true & lt;/value> & lt;/property> & lt;/bean> & lt;/array> & lt;/property> 代码>
3。配置服务
服务接口
<代码>/* * *查询所有部门,带分页(用的pageHelper) * @return */大小页面PageInfo findAll (int, int); 代码>
服务实现类
<代码>公共PageInfo findAll (int, int大小){//1 .设置分页信息,使用PageHelper的静态方法 PageHelper.startPage(页面大小);//2 .调用deptDao的查询所有方法 List公司=companyDao.findAll ();//3 .创建返回值并返回 返回新PageInfo(公司); }代码>
4。配置控制器的方法
<代码> @ autowired 私人CompanyService CompanyService;/* * *查询所有 * @return */@RequestMapping(“/清单”) 公共字符串列表(@RequestParam (defaultValue=" https://www.yisu.com/zixun/1 ") int页面,@RequestParam (defaultValue=" https://www.yisu.com/zixun/2 ") int大小){//1 .调用服务查询//ListcompanyList=companyService.findAll (); PageInfo PageInfo=companyService。findAll(页面大小);//2 .存入请求域中//request.setAttribute(“列表”,companyList); request.setAttribute(“页面”,pageInfo);//3 .转发到列表页面 返回“公司/公司列表”; }代码>
前端页面
& lt; div类=癰ox-footer"在
& lt; jsp:包括页面=? ./共同/page.jsp"在
& lt; jsp:参数值=https://www.yisu.com/zixun/? {ctx}/公司/list.do " name=" pageUrl "/>
& lt;/jsp: include>
& lt;/div>