一、總結了mybatis中五種不同實現分頁查詢的方法
UserMapper.java接口文件
public interface UserMapper { //分頁查詢 public List<User> selectForPage1(int startIndex,int pageSize); public List<User> selectForPage2(Map<String, Object> map); public Integer selectCount(); public List<User> selectForPage3(PageBean pageBean); //分頁加模糊查詢 public Integer selectCount2(String keywords); public List<User> selectForPage4(Map<String, Object> map); }
工具類PageBean.java
public class PageBean { private Integer currentPage; private Integer startIndex; private Integer pageSize=5; private Integer totalCount; private Integer totalPage; public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; this.startIndex=(this.currentPage-1)*this.pageSize; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; //計算總頁數 this.totalPage=(int)Math.ceil((this.totalCount*1.0/this.pageSize)); } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public Integer getStartIndex() { return startIndex; } public void setStartIndex(Integer startIndex) { this.startIndex = startIndex; } }
UserMapper.xml文件
其中查詢5是模糊加分頁查詢語句
<?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"> <!-- namespace和Mapper接口類名字一樣 --> <mapper namespace="com.gx.mapper.UserMapper"> <!-- 查詢1 --> <select id="selectForPage1" resultType="User"> select * from user limit #{param1},#{param2} </select> <!-- 查詢2--> <select id="selectForPage2" resultType="User"> select * from user limit #{startIndex},#{pageSize} </select> <!-- 查詢3--> <select id="selectAll" resultType="User"> select * from user </select> <!-- 查詢總條數--> <select id="selectCount" resultType="java.lang.Integer"> select count(*) from user </select> <!-- 查詢4--> <select id="selectForPage3" resultType="User"> select * from user limit #{startIndex},#{pageSize} </select> <!-- 查詢5--> <select id="selectForPage4" resultType="User"> select * from user where name like "%"#{keywords}"%" or address like "%"#{keywords}"%" limit #{startIndex},#{pageSize} </select> <!-- 模糊查詢查詢總條數--> <select id="selectCount2" resultType="java.lang.Integer" parameterType="java.lang.String"> select count(*) from user where name like "%"#{value}"%" or address like "%"#{value}"%" </select> </mapper>
測試test
其中方法6是模糊加分頁查詢測試
public class myTest { SqlSession session = MyBatisUtils.openSession(); UserMapper userMapper = session.getMapper(UserMapper.class); @Test public void selectForPage1() { int currentPage=1; int pageSize=5; List<User> selectForPage = userMapper.selectForPage1((currentPage-1)*pageSize, pageSize); for (User user : selectForPage) { System.out.println(user); } MyBatisUtils.closeSession(session); } @Test public void selectForPage2() { int currentPage=1; int pageSize=5; Map<String , Object> map=new HashMap<>(); map.put("startIndex", (currentPage-1)*pageSize); map.put("pageSize", pageSize); List<User> selectForPage = userMapper.selectForPage2(map); for (User user : selectForPage) { System.out.println(user); } MyBatisUtils.closeSession(session); } @Test public void selectForPage3() { int currentPage=1; int pageSize=5; /** * 參數1:開始條 偏移量,下標 * 參數2:參數總條數 */ RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize, pageSize); //使用mybatis里面提供的api去寫的 List<User> list = session.selectList("com.gx.mapper.UserMapper.selectAll", null, rowBounds); for (User user : list) { System.out.println(user); } MyBatisUtils.closeSession(session); } @Test public void selectForPage4() { Integer count = userMapper.selectCount(); System.out.println(count); int currentPage=1; int pageSize=5; Map<String , Object> map=new HashMap<>(); map.put("startIndex", (currentPage-1)*pageSize); map.put("pageSize", pageSize); List<User> list = userMapper.selectForPage2(map); for (User user : list) { System.out.println(user); } System.out.println("當前第"+currentPage+"頁,共"+count+"條"); MyBatisUtils.closeSession(session); } @Test public void selectForPage5() { PageBean bean = new PageBean(); bean.setCurrentPage(1); bean.setPageSize(5); //查詢總條數 Integer count = userMapper.selectCount(); //放到pageBean bean.setTotalCount(count); List<User> list = userMapper.selectForPage3(bean); for (User user : list) { System.out.println(user); } System.out.println("當前第"+bean.getCurrentPage()+"頁,共"+count+"條"); MyBatisUtils.closeSession(session); } @Test public void selectForPage6() { String keywords="雲6"; PageBean bean = new PageBean(); bean.setCurrentPage(1); bean.setPageSize(5); //查詢總條數 Integer count = userMapper.selectCount2(keywords); //放到pageBean bean.setTotalCount(count); Map<String , Object> map = new HashMap<>(); map.put("startIndex", bean.getStartIndex()); map.put("pageSize", bean.getPageSize()); map.put("keywords", keywords); List<User> list = userMapper.selectForPage4(map); for (User user : list) { System.out.println(user); } System.out.println("當前第"+bean.getCurrentPage()+"頁,共"+count+"條"); MyBatisUtils.closeSession(session); } }