java:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties; import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.imooc.bean.BaseBean;
import org.imooc.bean.Page; @Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})
public class PageInterceptor implements Interceptor{ public Object intercept(Invocation arg0) throws Throwable {
StatementHandler statementHandler = (StatementHandler)arg0.getTarget();
MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,new DefaultReflectorFactory());
MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement");
String id = mappedStatement.getId();
if(id.endsWith("ByPage")) {
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
String countSql = "select count(*) from(" + sql + ")t";
Connection conn = (Connection)arg0.getArgs()[0];
PreparedStatement statement = conn.prepareStatement(countSql);
ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler");
parameterHandler.setParameters(statement);
ResultSet rs = statement.executeQuery();
BaseBean bean = (BaseBean)boundSql.getParameterObject();
Page page = bean.getPage();
if(rs.next()) {
page.setTotalNumber(rs.getInt(1));
}
String pageSql = sql + " limit " + (page.getCurrentPage() - 1) * page.getPageNumber() + "," + page.getPageNumber();
metaObject.setValue("delegate.boundSql.sql", pageSql);
}
return arg0.proceed();
} public Object plugin(Object arg0) {
return Plugin.wrap(arg0, this);
} public void setProperties(Properties arg0) { }
}

  

/**
* 分页对象
*/
public class Page { // 总条数
private int totalNumber;
// 当前页数
private int currentPage;
// 总页数
private int totalPage;
// 每页显示条数
private int pageNumber; public Page() {
this.currentPage = 1;
this.pageNumber = 5;
} public int getTotalNumber() {
return totalNumber;
} private void count() {
this.totalPage = this.totalNumber / this.pageNumber;
if(this.totalNumber % this.pageNumber > 0) {
this.totalPage++;
}
if(this.totalPage <= 0) {
this.totalPage = 1;
}
if(this.currentPage > this.totalPage) {
this.currentPage = this.totalPage;
}
if(this.currentPage <= 0) {
this.currentPage = 1;
}
} public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
this.count();
} public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() { return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
}

  

public class BaseBean {

	private Page page;

	public BaseBean() {
this.page = new Page();
} public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}

  jsp页面:

<%@ tag language="java" pageEncoding="UTF-8" %>
<%@ attribute type="org.imooc.bean.Page" name="page" required="true" %>
<%@ attribute type="String" name="jsMethodName" required="true" %> <script type="text/javascript">
function transCurrentPage(currentPage) {
var rule = /^[0-9]*[1-9][0-9]*$/;
if(!rule.test(currentPage)) {
currentPage = 1;
}
eval("${jsMethodName}(currentPage)");
}
</script> <div class="page fix">
<a href="javascript:transCurrentPage('1');" class="first">首页</a>
<a href="javascript:transCurrentPage('${page.currentPage - 1}');" class="pre">上一页</a>
当前第<span>${page.currentPage}/${page.totalPage}</span>页
<a href="javascript:transCurrentPage('${page.currentPage + 1}');" class="next">下一页</a>
<a href="javascript:transCurrentPage('${page.totalPage}');" class="last">末页</a>
跳至  <input id="currentPageText" value="1" class="allInput w28" type="text"/> 页  
<a href="javascript:transCurrentPage($('#currentPageText').val());" class="go">GO</a>
</div>

  调用:

<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<!-- 分页 -->
<t:page jsMethodName="search" page="${searchParam.page}"></t:page>

  

JAVA MyBybatis分页的更多相关文章

  1. Java 动态分页类

     动态分页类: Cls_page.java package pagination; public class Cls_page { private int nums;// 总条目数 private i ...

  2. 【java】分页查询实体类

    package com.dmsd.itoo.tool.pageModel; import java.io.Serializable; import java.util.HashMap; import ...

  3. Java实现分页数据获取CachedRowSet

    步骤 1.加载驱动 2.连接数据库 3.创建ResultSet 4.创建CacheRowSet 5.设置并获取分页数据 6.执行查询,展示数据 package ch13; import javax.s ...

  4. java web分页查询初试

    ssh2分页查询初试,放着记录学习一下. entity:student.java: package com.zte.entity; /** * 数据持久化,跟数据库的的相应的表的字段是对应的. * * ...

  5. java Page分页显示

    //entity层实体类 import java.util.List; //分页展示 //相关属性:当前页,页大小(每页显示的条数),总页数,总条数,数据 //select * from t_user ...

  6. java超强分页标签演示

    最近在做一个项目,用到了一个分页,于是动手写了个分页标签,先将代码贴出来,供大家交流,写的不好,请见谅!. 以下是java标签类,继承自SimpleTagSupport package com.lyn ...

  7. Java 实现分页功能

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnic ...

  8. java web 分页实现

       分页实现的效果:    ///////// /////////////////////////////////////////////////////// /////////////////// ...

  9. Java自定义分页标签的实现

    主要字段含义: 页号 pagaNo页面大小 pageSize总记录条数 recordCount计算本次一共分多少页 myPageSize页号显示开始 start 页号显示结束 end PageTag需 ...

随机推荐

  1. P1270 “访问”美术馆——不太一样的树形DP

    P1270 “访问”美术馆 dfs读入,存图有点像线段树: 在枚举时间时,要减去走这条边的代价: #include<cstdio> #include<cstring> #inc ...

  2. Noip2019暑期训练2 反思

    经过两次测试,通过深刻的反思,我主要发现了以下易犯错误: 1.做题目时过于追求速度,导致好几处代码都出现手误打错的现象!而且,千万不要图快.图方便就复制粘贴,非常容易出错!(例如T3-party中直接 ...

  3. 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1。 buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t”分割

    实验内容(mapReduce安装请按照林子雨教程http://dblab.xmu.edu.cn/blog/631-2/) 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为 ...

  4. 打印出js对象里面的内容

    最近调试的时候遇到需要打印出js对象里面的内容,两种方式: 1.直接使用 JSON.stringify(obj) 方法把对象转成字符串,打印出来.但是因为维护的项目比较老,使用的还是ie11的ie5兼 ...

  5. 第06组 Alpha冲刺(3/4)

    队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11899921.html 作业博客 :https://edu.cnblogs.com/campus/f ...

  6. 2019软工实践_Alpha(5/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11898112.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...

  7. @Conditional 和 @ConditionalOnProperty

    @ConditionalOnProperty https://blog.csdn.net/dalangzhonghangxing/article/details/78420057 @Condition ...

  8. 绿色地狱 - 纽博格林赛道详解 | Nürburgring

    Nürburgring - Green Hell [統哥] 車迷人生必去一趟的德國紐柏林賽道之旅 F1赛道通常短而宽,一是为了观赏性,二是为了安全. 而Nürburgring赛道则是F1赛道的极端反面 ...

  9. Android - Button(按钮)的响应点击事件的4种写法

    Button控件setOnclickListener(View.OnClickListener listener)来接收一个点击事件的监听器 自定义一个点击事件监听器类让其实现View.OnClick ...

  10. [转]CSS3 使用 calc() 计算高度 vh px

    1.px 像素,我们在网页布局中一般都是用px. 2.百分比 百分比一般宽泛的讲是相对于父元素,自适应网页布局越来越多,百分比也经常用到了. 3.Viewport    viewport:可视窗口,也 ...