最新版本见github
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
2.Config类,注解@Configuration
ps:该项目的mapper文件为java 文件,如果是xml 改为xml
/**
* pagehelper配置
*/
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
System.out.println("注入pagehelper配置!!!");
final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource());
sqlSessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlSessionFactory.setFailFast(true);
sqlSessionFactory.setMapperLocations(getResource("mapper", "**/*.java"));
return sqlSessionFactory.getObject();
}
public Resource[] getResource(String basePackage, String pattern) throws IOException {
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(new StandardEnvironment().resolveRequiredPlaceholders(basePackage)) + "/" + pattern;
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(packageSearchPath);
return resources;
}
3.配置mybatis-config.xml
下面代码网上直接copy过来的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" />
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="offsetAsPageNum" value="true" />
<property name="rowBoundsWithCount" value="true" />
<property name="pageSizeZero" value="true" />
<property name="reasonable" value="false" />
<property name="supportMethodsArguments" value="false" />
<property name="returnPageInfo" value="none" />
</plugin>
</plugins>
</configuration>
4.调试测试
在impl调用mapper文件中 调用静态方法 PageHelper.startPage(page, pageSize);
对象pageinfo 有很多参数可参考 官方文档
package lb.springboot.server.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lb.springboot.model.ReportmalfuncMst2;
import lb.springboot.model.mapper.ReportmalfuncMstMapper;
import lb.springboot.server.ReportmalfuncMstServer;
@Repository
public class ReportmalfuncMstServerImpl implements ReportmalfuncMstServer {
@Autowired
private ReportmalfuncMstMapper mapper;
public PageInfo<ReportmalfuncMst2> findAll(String term_mac, Integer page, Integer pageSize){
PageHelper.startPage(page, pageSize);
List<ReportmalfuncMst2> list = mapper.selectByMac(term_mac);
return new PageInfo<ReportmalfuncMst2>(list);
}
}
5.thymeleaf 查看视图
<div id="wrapper">
<div class="vertical-align-wrap">
<div class="vertical-align-middle">
<div class="content">
<table class="table table-hover" >
<thead>
<tr>
<th>malfunc_id</th>
<th>report_dt</th>
</tr>
</thead>
<tr th:each="entity : ${entitys}">
<th th:text="${entity.malfuncId}"></th>
<th th:text="${#dates.format(entity.reportDt, 'yyyy-MM-dd')}"></th>
</tr>
</table>
<nav>
<ul class="pager">
<li><a th:href="@{${'/index'}(pageNum=${firstPage},pageSize=${pageSize})}">«</a></li>
<li >
<a th:if="${not isFirstPage}" th:href="@{${'/index'}(pageNum=${pageNum-1},pageSize=${pageSize})}">Previous</a>
<a th:if="${isFirstPage}" href="javascript:void(0);">Previous</a>
</li>
<li th:each="pageNo : ${#numbers.sequence(1, totalPages)}">
<a th:if="${pageNum eq pageNo}" href="javascript:void(0);">
<span th:text="${pageNo}"></span>
</a>
<a th:if="${not (pageNum eq pageNo)}" th:href="@{${'/index'}(pageNum=${pageNo},size=${pageSize})}">
<span th:text="${pageNo}"></span>
</a>
</li>
<li>
<a th:if="${not isLastPage}" th:href="@{${'/index'}(pageNum=${pageNum+1},size=${pageSize})}">Next</a>
<a th:if="${isLastPage}" href="javascript:void(0);">Next</a>
</li>
<li><a th:href="@{${'/index'}(pageNum=${lastPage},pageSize=${pageSize})}">»</a></li>
</ul>
</nav>
</div>
</div>
</div>
</div>