mybatis分页插件使用案例
1、导包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2、在mybatis的全局配置文件中配置PageHelper分页插件
- applicationContext.xml配置文件中要指定mybatis全局配置文件的位置
<!-- 引入 pageHelper插件,注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable:分页合理化参数,默认值为false,直接根据参数进行查询。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
3、持久层接口
package com.yl.dao;
import com.yl.bean.Book;
import com.yl.bean.PageBook;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 图书持久层接口
*/
public interface IBookDao {
/**
* 查询所有图书
*/
List<Book> queryAll();
}
4、持久层映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yl.dao.IBookDao">
<!--配置实体类属性和数据库字段对应关系-->
<resultMap id="bookMap" type="com.yl.bean.Book">
<id property="id" column="book_id"></id>
<result property="name" column="book_name"></result>
<result property="author" column="book_author"></result>
<result property="date" column="book_date"></result>
<result property="price" column="book_price"></result>
</resultMap>
<!--查询所有图书,写普通的查询即可,插件会自动分页-->
<select id="queryAll" resultMap="bookMap">
select * from tb_book
</select>
</mapper>
5、业务层接口
package com.yl.service;
import com.github.pagehelper.PageInfo;
import com.yl.bean.Book;
import com.yl.bean.PageBook;
import java.util.List;
/**
* 图书业务层接口
*/
public interface IBookService {
/**
* 查询所有图书
*/
PageInfo<Book> queryAll(int pageCode, int pageSize);
}
6、业务层接口实现类
package com.yl.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.bean.Book;
import com.yl.bean.PageBook;
import com.yl.dao.IBookDao;
import com.yl.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
/**
* 图书业务层接口实现类
*/
@Service("bookService")
public class IBookServiceImpl implements IBookService {
@Autowired
private IBookDao bookDao;
/**
* 查询所有图书
* @param pageCode
* @param pageSize
* @return
*/
@Override
public PageInfo<Book> queryAll(int pageCode, int pageSize) {
//分页查询
PageHelper.startPage(pageCode,pageSize);//写在所有查询之前
List<Book> bookList=bookDao.queryAll();
PageInfo<Book> pageInfo = new PageInfo<Book>(bookList);
return pageInfo;
}
}
7、控制器
package com.yl.controller;
import com.github.pagehelper.PageInfo;
import com.yl.bean.Book;
import com.yl.bean.PageBook;
import com.yl.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
/**
* 图书控制器类
*/
@Controller
@RequestMapping("/book")
@SessionAttributes(value = {"pageBook"},types ={PageBook.class} )
public class BookController {
@Autowired
private IBookService bookService;//图书业务层对象
/**
* 查询所有图书
*/
@RequestMapping("/queryAll")
public ModelAndView queryAll(int pageCode){
int pc=1;//页码,默认为第一页
int pageSize=3;//页面数据条数
//判断表单页码是否为空
if (pageCode>0){
pc=pageCode;
}
//调用业务层查询所有方法
PageInfo<Book> pageInfo=bookService.queryAll(pc,pageSize);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("pageBook",pageInfo);
modelAndView.setViewName("index");
return modelAndView;
}
}
8、jsp
<form method="post" action="${pageContext.servletContext.contextPath}/book/queryByPage">
<table>
<tr>
<th>书名</th>
<th>作者</th>
<th>出版日期</th>
<th>价格</th>
</tr>
<c:forEach items="${sessionScope.pageBook.list}" var="book">
<tr>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.date}</td>
<td>${book.price}</td>
</tr>
</c:forEach>
</table>
第${sessionScope.pageBook.pageNum}页/共${sessionScope.pageBook.pages}页
<a href="<c:url value="/book/queryAll?pageCode=1"/>">首页</a>
<c:if test="${sessionScope.pageBook.pageNum>1}">
<a href="<c:url value="/book/queryAll?pageCode=${sessionScope.pageBook.pageNum-1}"/>">上一页</a>
</c:if>
<c:if test="${sessionScope.pageBook.pageNum<sessionScope.pageBook.pages}">
<a href="<c:url value="/book/queryAll?pageCode=${sessionScope.pageBook.pageNum+1}"/>">下一页</a>
</c:if>
<a href="<c:url value="/book/queryAll?pageCode=${sessionScope.pageBook.pages}"/>">尾页</a>
</form>