色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

mysql 查詢到序,實現(xiàn)mysql動態(tài)

劉姿婷2年前19瀏覽0評論
mysql 查詢到序,實現(xiàn)mysql動態(tài)?

在這些控件里要達到分頁的效果,一般都會傳2個參數(shù),第一個是表示當前頁的索 引(一般從0開始),第二個表示當前頁展示多少條業(yè)務(wù)記錄,然后將相應(yīng)的參數(shù)傳遞給List<T> getList(PagenateArgs args)方法,最終實現(xiàn)數(shù)據(jù)庫中的分頁時候可以使用limit關(guān)鍵詞(針對mysql)進行分頁,如果是oracle或者sql server他們都有自帶的rownum函數(shù)可以使用。

針對上述思路,首先在 demo.mybatis.model下面新建一個名為PagenateArgs的分頁參數(shù)實體類與一個名為SortDirectionEnum的枚舉 類,里面包含當前頁面索引pageIndex, 當前頁展示業(yè)務(wù)記錄數(shù)pageSize, pageStart屬性表示從第幾條開始,(pageStart=pageIndex*pageSize)因為limit關(guān)鍵詞用法是表示【limit 起始條數(shù)(不包含),取幾條】,orderFieldStr排序字段,orderDirectionStr 排序方向,所以具體創(chuàng)建如下:

package david.mybatis.model;/* * 分頁參數(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ù)添加一個方法public List<Visitor> getListByPagenate(PagenateArgs args),這次的分頁其實也就是在這個的基礎(chǔ)上稍加改動即可,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 { /* * 基礎(chǔ)查詢 */ 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>節(jié)點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"代表是否使用自增長序列, keyProperty="Id"指定自增長列是哪一列, 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> <!-- 以下為新增部分用來分頁,orderBySql這個提取出來是為了后面有示例復(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ù)化輸出,${}表示直接輸出不進行任何轉(zhuǎn)義操作,自己進行轉(zhuǎn)移 --> <if test="pageStart>-1 and pageSize>-1"> limit #{pageStart}, #{pageSize} </if> </select> <sql id="orderBySql"> order by ${orderFieldStr} ${orderDirectionStr} </sql></mapper>

這里面的字段屬性都是針對PagenateArgs參數(shù)類中的屬性名,保持一致。

<if test="pageStart>-1 and pageSize>-1"> limit #{pageStart}, #{pageSize}</if>

在DemoRun類中創(chuàng)建測試方法:

/* * 分頁參數(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());

運行后下測試結(jié)果,先按Id倒序排列,查的Visitor表一共有14條記錄,

假設(shè)取在第2頁取5條,執(zhí)行下面也就是6-10條數(shù)據(jù),這樣傳參數(shù)就行了

DemoRun.queryVisitorListWithPagenate(1, 5, "id", SortDirectionEnum.DESC.toString());

結(jié)果如下:

實現(xiàn)了一個分頁邏輯.