一、编写dao及配置文件
(1)配置文件两种方式
第一种:
<!--SqlSeesionFactoryBean对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--mybatis分页助手相关配置-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!-- 分页的相关配置参数 用哪个数据库-->
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
第二种:配置文件的方式
<!--SqlSeesionFactoryBean对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
//引入外部配置文件
<property name="configLocation" value="classpath:sqlMapConfig.xml">
//外部配置文件:sqlMapConfig.xml
<?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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
</bean>
(2)//分页查询
@Select("select * from (SELECT *,(@rowNum:=@rowNum+1) as rowNo FROM orders,(Select (@rowNum :=0) ) b) " +
"res where rowNo>=#{param1} and rowNo<=#{param2}")
public List<Orders> findByProduct(Integer currPage,Integer pageSize);
二、编写service及实现类
(1)service
//分页助手分页
public PageInfo<Orders> FindByPageHelper(Integer currPage, Integer pageSize);
(2)实现类
@Override
public PageInfo<Orders> FindByPageHelper(Integer currPage, Integer pageSize) {
//指定分页的参数
PageHelper.startPage(currPage,pageSize);
//查询全部
List<Orders> ordersList = ordersDao.findAll();
//创建pageInfo对象
PageInfo<Orders> pageInfo = new PageInfo<>(ordersList);//第二个参数是底部显示分页数量
return pageInfo;
}
三、编写controller
/ 分页助手分页
// PageHelper 分页助手查询 value 指定页面参数的名称 required 是否必要有参数
@RequestMapping("/findAll")
public ModelAndView findAll(@RequestParam(value = "currPage",defaultValue = "1") Integer currPage,
@RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
//获取数据
PageInfo<Orders> pageInfo = ordersService.FindByPageHelper(currPage, pageSize);
// 创建modelAndView对象
ModelAndView modelAndView = new ModelAndView();
//添加数据
modelAndView.addObject("pageInfo",pageInfo);
//指向页面
modelAndView.setViewName("order-list");
return modelAndView;
}
四、编写web文件
<c:forEach items="${pageInfo.list}" var="orders"></c:forEach>(遍历数据库数据)
分页:
<div class="box-footer">
<div class="pull-left">
<div class="form-group form-inline">
总共${pageInfo.pages} 页,共${pageInfo.total}条数据。 每页
<select class="form-control" onchange="gotoPage(1)" id="pageSize">
<option value="2">2</option>
<option value="5" selected="selected">5</option>
<option value="10">10</option>
</select> 条
</div>
</div>
<div class="box-tools pull-right">
<%--在超链接中访问js函数 必须添加前缀 javascript--%>
<ul class="pagination">
<li><a href="javascript:gotoPage(1)" aria-label="Previous">首页</a></li>
<li><a href="javascript:gotoPage(${pageInfo.prePage})">上一页</a></li>
<c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="i">
<li><a href="javascript:gotoPage(${i})">${i}</a></li>
</c:forEach>
<li><a href="javascript:gotoPage(${pageInfo.nextPage})">下一页</a></li>
<li><a href="javascript:gotoPage(${pageInfo.pages})" aria-label="Next">尾页</a></li>
</ul>
</div>
</div>
<script type="text/javascript">
// 分页
$("#pageSize option[value=${pageInfo.pageSize}]").prop("selected","selected");
function gotoPage(currPage) {
//获取每页显示条数
var pageSize=$("#pageSize").val();
if(currPage<1){
return;
}
if(currPage>${pageInfo.pages}){
return;
}
location.href="${pageContext.request.contextPath}/orders/findAll?currPage="+currPage+"&pageSize="+pageSize;
}
</script>