在這些控件里要達到分頁的效果,一般都會傳2個參數,第一個是表示當前頁的索 引(一般從0開始),第二個表示當前頁展示多少條業務記錄,然后將相應的參數傳遞給List<T> getList(PagenateArgs args)方法,最終實現數據庫中的分頁時候可以使用limit關鍵詞(針對mysql)進行分頁,如果是oracle或者sql server他們都有自帶的rownum函數可以使用。
針對上述思路,首先在 demo.mybatis.model下面新建一個名為PagenateArgs的分頁參數實體類與一個名為SortDirectionEnum的枚舉 類,里面包含當前頁面索引pageIndex, 當前頁展示業務記錄數pageSize, pageStart屬性表示從第幾條開始,(pageStart=pageIndex*pageSize)因為limit關鍵詞用法是表示【limit 起始條數(不包含),取幾條】,orderFieldStr排序字段,orderDirectionStr 排序方向,所以具體創建如下:
package david.mybatis.model;/* * 分頁參數實體類 */public class PagenateArgs { private int pageIndex; private int pageSize; private int pageStart; private String orderFieldStr; private String orderDirectionStr; public PagenateArgs() { // TODO Auto-generated constructor stub } public PagenateArgs(int pageIndex, int pageSize, String orderFieldStr, String orderDirectionStr) { this.pageIndex = pageIndex; this.pageSize = pageSize; this.orderFieldStr = orderFieldStr; this.orderDirectionStr = orderDirectionStr; pageStart = pageIndex * pageSize; } public int getPageIndex() { return pageIndex; } public int getPageStart() { return pageStart; } public int getPageSize() { return pageSize; } public String orderFieldStr() { return orderFieldStr; } public String getOrderDirectionStr() { return orderDirectionStr; }}
package david.mybatis.model;/* * 排序枚舉 */public enum SortDirectionEnum { /* * 升序 */ ASC, /* * 降序 */ DESC}
完成上面的步驟以后在IVisitorOperation接口類中繼續添加一個方法public List<Visitor> getListByPagenate(PagenateArgs args),這次的分頁其實也就是在這個的基礎上稍加改動即可,IVisitorOperation接口類 改動后如下所示:
package david.mybatis.demo;import java.util.List;import david.mybatis.model.PagenateArgs;import david.mybatis.model.Visitor;import david.mybatis.model.VisitorWithRn;public interface IVisitorOperation { /* * 基礎查詢 */ public Visitor basicQuery(int id); /* * 添加訪問者 */ public int add(Visitor visitor); /* * 刪除訪問者 */ public int delete(int id); /* * 更新訪問者 */ public int update(Visitor visitor); /* * 查詢訪問者 */ public Visitor query(int id); /* * 查詢List */ public List<Visitor> getList(); /* * 分頁查詢List */ public List<Visitor> getListByPagenate(PagenateArgs args); }
接下來改動VisitorMapper.xml配置文件了,新增一個<select>節點id與參數類型參照前幾章的方式配置好,如下此處新增的id就為getListByPagenate,配置好以后如下
<?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="david.mybatis.demo.IVisitorOperation"> <!-- useGeneratedKeys="true"代表是否使用自增長序列, keyProperty="Id"指定自增長列是哪一列, parameterType="Visitor"指定IVisitorOperation接口類中定義中所傳的相應類型 --> <insert id="add" parameterType="Visitor" useGeneratedKeys="true" keyProperty="Id"> insert into Visitor (Name, Email, Status, CreateTime) values (#{name}, #{email}, #{status}, #{createTime}) </insert> <delete id="delete" parameterType="int"> delete from Visitor where status>0 and id = #{id} </delete> <update id="update" parameterType="Visitor"> update Visitor set Name = #{name}, Email=#{email}, Status=#{status} where id=#{id} and Status>0; </update> <select id="query" parameterType="int" resultType="Visitor"> select Id, Name, Email, Status, CreateTime from visitor where id=#{id} and Status>0 order by Id </select> <select id="basicQuery" parameterType="int" resultType="Visitor"> select * from visitor where id=#{id} and Status>0 order by Id </select> <select id="getList" resultMap="visitorRs"> <include refid="getListsql" /> </select> <sql id="getListSql"> select * from Visitor where status>0 </sql> <!-- 以下為新增部分用來分頁,orderBySql這個提取出來是為了后面有示例復用 --> <resultMap type="Visitor" id="visitorRs"> <id column="Id" property="id" /> <result column="Name" property="name" /> <result column="Email" property="email" /> <result column="Status" property="status" /> <result column="CreateTime" property="createTime" /> </resultMap> <select id="getListByPagenate" parameterType="PagenateArgs" resultType="Visitor"> select * from ( <include refid="getListSql" /> <include refid="orderBySql"/> ) t <!-- #{}表示參數化輸出,${}表示直接輸出不進行任何轉義操作,自己進行轉移 --> <if test="pageStart>-1 and pageSize>-1"> limit #{pageStart}, #{pageSize} </if> </select> <sql id="orderBySql"> order by ${orderFieldStr} ${orderDirectionStr} </sql></mapper>這里面的字段屬性都是針對PagenateArgs參數類中的屬性名,保持一致。
<if test="pageStart>-1 and pageSize>-1"> limit #{pageStart}, #{pageSize}</if>
在DemoRun類中創建測試方法:
/* * 分頁參數 */public static void queryVisitorListWithPagenate(int pageIndex, int pageSize, String orderField, String orderDire) { PagenateArgs args = new PagenateArgs(pageIndex, pageSize, orderField, orderDire); SqlSession session = MybatisUtils.getSqlSession(); IVisitorOperation vOperation = session.getMapper(IVisitorOperation.class); List<Visitor> visitors = vOperation.getListByPagenate(args); for (Visitor visitor : visitors) { System.out.println(visitor); } MybatisUtils.closeSession(session); MybatisUtils.showMessages(CRUD_Enum.List, visitors.size());}
DemoRun.queryVisitorListWithPagenate(0, 100, "id", SortDirectionEnum.DESC.toString());
運行后下測試結果,先按Id倒序排列,查的Visitor表一共有14條記錄,
假設取在第2頁取5條,執行下面也就是6-10條數據,這樣傳參數就行了
DemoRun.queryVisitorListWithPagenate(1, 5, "id", SortDirectionEnum.DESC.toString());
結果如下:
實現了一個分頁邏輯.