针对上述思路,首先在 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 “-////DTD Mapper 3.0//EN” “
/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());
结果如下:
实现了一个分页逻辑.
如果觉得《查询mysql记录条数 实现mysql动态 – 数据库 – 前端 链接mysql报错》对你有帮助,请点赞、收藏,并留下你的观点哦!