在這些控件里要達(dá)到分頁(yè)的效果,一般都會(huì)傳2個(gè)參數(shù),第一個(gè)是表示當(dāng)前頁(yè)的索 引(一般從0開始),第二個(gè)表示當(dāng)前頁(yè)展示多少條業(yè)務(wù)記錄,然后將相應(yīng)的參數(shù)傳遞給List<T> getList(PagenateArgs args)方法,最終實(shí)現(xiàn)數(shù)據(jù)庫(kù)中的分頁(yè)時(shí)候可以使用limit關(guān)鍵詞(針對(duì)mysql)進(jìn)行分頁(yè),如果是oracle或者sql server他們都有自帶的rownum函數(shù)可以使用。
針對(duì)上述思路,首先在 demo.mybatis.model下面新建一個(gè)名為PagenateArgs的分頁(yè)參數(shù)實(shí)體類與一個(gè)名為SortDirectionEnum的枚舉 類,里面包含當(dāng)前頁(yè)面索引pageIndex, 當(dāng)前頁(yè)展示業(yè)務(wù)記錄數(shù)pageSize, pageStart屬性表示從第幾條開始,(pageStart=pageIndex*pageSize)因?yàn)閘imit關(guān)鍵詞用法是表示【limit 起始條數(shù)(不包含),取幾條】,orderFieldStr排序字段,orderDirectionStr 排序方向,所以具體創(chuàng)建如下:
package david.mybatis.model;/* * 分頁(yè)參數(shù)實(shí)體類 */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接口類中繼續(xù)添加一個(gè)方法public List<Visitor> getListByPagenate(PagenateArgs args),這次的分頁(yè)其實(shí)也就是在這個(gè)的基礎(chǔ)上稍加改動(dòng)即可,IVisitorOperation接口類 改動(dòng)后如下所示:
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 { /* * 基礎(chǔ)查詢 */ public Visitor basicQuery(int id); /* * 添加訪問者 */ public int add(Visitor visitor); /* * 刪除訪問者 */ public int delete(int id); /* * 更新訪問者 */ public int update(Visitor visitor); /* * 查詢?cè)L問者 */ public Visitor query(int id); /* * 查詢List */ public List<Visitor> getList(); /* * 分頁(yè)查詢List */ public List<Visitor> getListByPagenate(PagenateArgs args); }
接下來改動(dòng)VisitorMapper.xml配置文件了,新增一個(gè)<select>節(jié)點(diǎn)id與參數(shù)類型參照前幾章的方式配置好,如下此處新增的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"代表是否使用自增長(zhǎng)序列, keyProperty="Id"指定自增長(zhǎng)列是哪一列, parameterType="Visitor"指定IVisitorOperation接口類中定義中所傳的相應(yīng)類型 --> <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> <!-- 以下為新增部分用來分頁(yè),orderBySql這個(gè)提取出來是為了后面有示例復(fù)用 --> <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 <!-- #{}表示參數(shù)化輸出,${}表示直接輸出不進(jìn)行任何轉(zhuǎn)義操作,自己進(jìn)行轉(zhuǎn)移 --> <if test="pageStart>-1 and pageSize>-1"> limit #{pageStart}, #{pageSize} </if> </select> <sql id="orderBySql"> order by ${orderFieldStr} ${orderDirectionStr} </sql></mapper>這里面的字段屬性都是針對(duì)PagenateArgs參數(shù)類中的屬性名,保持一致。
<if test="pageStart>-1 and pageSize>-1"> limit #{pageStart}, #{pageSize}</if>
在DemoRun類中創(chuàng)建測(cè)試方法:
/* * 分頁(yè)參數(shù) */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());
運(yùn)行后下測(cè)試結(jié)果,先按Id倒序排列,查的Visitor表一共有14條記錄,
假設(shè)取在第2頁(yè)取5條,執(zhí)行下面也就是6-10條數(shù)據(jù),這樣傳參數(shù)就行了
DemoRun.queryVisitorListWithPagenate(1, 5, "id", SortDirectionEnum.DESC.toString());
結(jié)果如下:
實(shí)現(xiàn)了一個(gè)分頁(yè)邏輯.