Day29SSM之CRUD开发版本3bootstrap的pagehelper

it2023-03-01  90

pagehelper分页插件介绍

(1)pagehelper是什么? 针对Mybatis提供分页插件,将分页查询简化(2)依赖配置 pagehelper(3)配置插件plugin 配置方法有两种 》1 : mybatis核心配置文件,在application中引入 mybatis核心配置文件 》2 : 在application中配置

pom.xml

<!--引入pageHelper分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>

配置方式1:SqlMapConfig.xml中配置

<!-- 参数合理化 如果当前currPage < 1 则按currPage = 1查询 如果当前currPage > totalPage 则按currPage = totalPage 查询 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="reasonable" value="true"/> </plugin> </plugins>

注意还需要在applicationContext.xml中添加

<!-- session工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- com.wzx.domain.Person person--> <property name="typeAliasesPackage" value="com.wzx.domain"/> <property name="configLocation" value="classpath:SqlMapConfig.xml"/> </bean>

配置方式2:applicationContext.xml中配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- com.wzx.domain.Person person--> <property name="typeAliasesPackage" value="com.wzx.domain"/> <!-- <property name="configLocation" value="classpath:SqlMapConfig.xml"/>--> <!-- PageHelper配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个 --> <!-- pageNum<=0 时会查询第一页 --> <!-- 指定数据库方言 --> <value> reasonable=true helperDialect=mysql </value> </property> </bean> </array> </property> </bean>

两种方式,根据个人习惯任选一个。

TestPageHelper

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestPageHelper { @Autowired private IDepartmentService departmentService; private static final Logger l = LoggerFactory.getLogger(TestPageHelper.class); @Test public void test01(){ //调用分页插件只要两行代码 PageHelper.startPage(1,10);//参1:当前页 参2 每页记录数 //我们只需要做一个最简单的查询 List<Department> list = departmentService.findAllDepartments(); PageInfo<Department> pageInfo = new PageInfo<>(list); l.info("test01 pageInfo="+pageInfo); } }

原理在拦截器 以下是模拟代码:

select count(*) from department select * from department limit 0, 20 ;//第一页 select * from department limit 20, 20 ;//第二页 select * from department;//由拦截器 PageInterceptor生成以上sql

由于拦截器会使用全查语句拼接limit分页,所以每个sql建议不要写; 先调用PageHelper.start(1,10)再调用全查,才能自动分页

DepartmentV3Controller

@RequestMapping(path="/pageUI",method = RequestMethod.GET) public String pageUI(Model model, Integer currPage, Integer pageSize){//你需要第几页数据,每页数据多条 l.info("page currPage="+currPage); l.info("page pageSize="+pageSize); if(currPage==null) { currPage=1; } if(pageSize==null) { pageSize=5; } //给定分页参数 PageHelper.startPage(currPage,pageSize); //一个全查,其他都交给PageInterceptor List<Department> list = iDepartmentService.findAllDepartments(); PageInfo<Department> pi = new PageInfo<>(list); model.addAttribute("pi",pi); return "list_v3"; }

list_v3.jsp

》》导入c标签 使用里面的foreach与if标签 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 》》循环列表的行

<div class="row"> <div class="col-md-12"> <table class="table table-hover"> <tr> <th>部门编号</th> <th></th> <th>部门名称</th> <th>操作</th> </tr> <c:forEach items="${pi.list}" var="dept"> <!--将一行循环指定的次数 --> <tr> <td>${dept.did}</td> <td></td> <td>${dept.dname}</td> <td> <button class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除 </button> <button class="btn btn-info"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 修改 </button> </td> </tr> </c:forEach> </table> </div> </div>

》》赋值显示分页工具条

<div class="row"> <div class="col-md-6">当前共有${pi.total}条记录,共${pi.pages}页</div> <div class="col-md-6"> <nav aria-label="Page navigation"> <ul class="pagination"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">首页</span> </a> </li> <c:if test="${pi.hasPreviousPage}"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> </c:if> <%-- <li class="active"><a href="#">1</a></li>--%> <c:forEach items="${pi.navigatepageNums}" var="num"> <c:if test="${num == pi.pageNum}"> <li class="active"><a href="#">${num}</a></li> </c:if> <c:if test="${num != pi.pageNum}"> <li><a href="#">${num}</a></li> </c:if> </c:forEach> <c:if test="${pi.hasNextPage}"> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">下一页</span> </a> </li> </c:if> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">末页</span> </a> </li> </ul> </nav> </div> </div>
最新回复(0)