在ssm框架上實現分頁查詢,具體以下:java
1. 首先建立一個分頁的實體類sql
public class Flexigrid { //符合條件總記錄數 private int total; //當前查詢的第幾頁數據 private int page; //總頁數 private int totalPage; //每頁查詢結果結束序號 @JSONField(serialize=false) private int rowEnd; //每頁查詢結果開始序號 @JSONField(serialize=false) private int rowStart; //每頁結果數 private int rp; //查詢條件 @JSONField(serialize=false) private Object obj; //返回結果 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) private List<?> rows; //......其餘get、set方法 public int getTotalPage() { if(rp == 0) return 1; // 提取信息行數 int total = this.total; this.totalPage = total / rp; int mod = total % this.rp; if (mod != 0) this.totalPage += 1; return totalPage; } }
2. Controller ,student對象接收參數,並將對象做爲查詢參數存入Flexigrid對象中。mybatis
@Controller @RequestMapping("/student") public class StudentController { @RequestMapping(value = "/page") @ResponseBody public Flexigrid page(@RequestBody Student student) { Flexigrid flex = new Flexigrid(); flex.setObj(student); // 設置查詢參數 flex.setPage(student.getPageNo()); //查詢的第幾頁數據 flex.setRp(student.getPageSize()); //每頁結果數 flex.setRowStart((flex.getPage() - 1) * flex.getRp()); //查詢結果開始序號 flex.setRowEnd(flex.getPage() * flex.getRp()); //查詢結果結束序號 List list = studentService.page(flex); //查詢的結果集 int total = studentService.total(flex); //符合查詢條件的總記錄數 flex.setTotal(total); flex.setRows(list); flex.setObj(null); //將查詢條件置爲空 return flex; } }
3. service 提供page 和total方法app
@Service @Transactional(readOnly = true) public class StudentService { /** * 查詢分頁數據 * @param flex 包含查詢條件以及分頁數據 * @return */ public List page(Flexigrid flex) { return studentDao.page(flex); } /** * 獲取查詢信息總數量 * @param flex 包含查詢條件以及分頁數據 * @return */ public Integer total(Flexigrid flex) { return studentDao.total(flex); } }
4. dao層定義接口框架
@MyBatisDao public interface StudentDao{ /** * 查詢分頁數據 * @param flex 包含查詢條件以及分頁數據 * @return */ List<Student> page(Flexigrid flex); /** * 獲取查詢信息總數量 * @param flex 包含查詢條件以及分頁數據 * @return */ Integer total(Flexigrid flex); }
5. 建立映射文件。flex
編寫sql-id爲page、total的sql 語句。this
<?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指向dao接口 --> <mapper namespace="com.dao.StudentDao" > <!-- 信息個數提取 --> <select id="total" resultType="int" parameterType="com.entity.Flexigrid"> SELECT COUNT(t.ID) FROM T_STUDENT t <where> <if test="obj != null"> <if test="obj.id != null"> AND t.ID=#{obj.id} </if> <if test="obj.studentName != null"> AND t.STUDENT_NAME like '%${obj.studentName}%' </if> </if> </where> </select> <!-- 信息分頁 --> <select id="page" resultType="com.entity.Student" parameterType="com.entity.Flexigrid"> SELECT * FROM (SELECT A.*, rownum r FROM ( SELECT t.id FROM T_STUDENT t <where> <if test="obj != null"> <if test="obj.id != null"> AND t.ID=#{obj.id} </if> <if test="obj.studentName != null"> AND t.STUDENT_NAME like '%${obj.studentName}%' </if> </if> </where> ORDER BY t.STUDENT_NAME <![CDATA[ ) A WHERE rownum <=#{rowEnd} )B WHERE r > #{rowStart} ]]> </select> </mapper>