JAVA MyBybatis分页
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分页的更多相关文章
- Java 动态分页类
动态分页类: Cls_page.java package pagination; public class Cls_page { private int nums;// 总条目数 private i ...
- 【java】分页查询实体类
package com.dmsd.itoo.tool.pageModel; import java.io.Serializable; import java.util.HashMap; import ...
- Java实现分页数据获取CachedRowSet
步骤 1.加载驱动 2.连接数据库 3.创建ResultSet 4.创建CacheRowSet 5.设置并获取分页数据 6.执行查询,展示数据 package ch13; import javax.s ...
- java web分页查询初试
ssh2分页查询初试,放着记录学习一下. entity:student.java: package com.zte.entity; /** * 数据持久化,跟数据库的的相应的表的字段是对应的. * * ...
- java Page分页显示
//entity层实体类 import java.util.List; //分页展示 //相关属性:当前页,页大小(每页显示的条数),总页数,总条数,数据 //select * from t_user ...
- java超强分页标签演示
最近在做一个项目,用到了一个分页,于是动手写了个分页标签,先将代码贴出来,供大家交流,写的不好,请见谅!. 以下是java标签类,继承自SimpleTagSupport package com.lyn ...
- Java 实现分页功能
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnic ...
- java web 分页实现
分页实现的效果: ///////// /////////////////////////////////////////////////////// /////////////////// ...
- Java自定义分页标签的实现
主要字段含义: 页号 pagaNo页面大小 pageSize总记录条数 recordCount计算本次一共分多少页 myPageSize页号显示开始 start 页号显示结束 end PageTag需 ...
随机推荐
- shell 杀死80端口的所有进程
netstat -lnp|grep |grep -v grep |awk
- 超级简单的tab点击
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CentOS 6和 CentOS 7的区别【转】
虽然,redhat 8在今年已经推出了,但是centos 8还没有推出.而且公司好多都在用centos 6和7 来了解一下6和7的区别吧 整体说明 1.系统 项目CentOS 6CentOS7 . 安 ...
- MongoDB笔记: 常见问题
系统配置 设置ulimit MongoDB的文件机制 每个Collection会单独创建一个数据文件(collection-xxxxxx.wt) 每个索引会单独创建一个文件(index-xxxxxx. ...
- jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理
问题背景:jenkins服务器发布代码后需要执行删除缓存的操作ssh -p222 eus_pe_devadmin@1.1.1.1 "sudo rm -rf /dev/shm/nginx/hi ...
- [LeetCode] 555. Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- ztree取消之前选中状态并设置新的选中
var zTreeObject = $.fn.zTree.getZTreeObj("treeId"); var node = zTreeObject.getNodeByParam( ...
- javascript循环遍历数组输出key value
javascript循环遍历数组输出key value用$.each方法肯定不行的 所以采用如下方法<pre> markers = []; markers[2]=3; markers[3] ...
- csp联考T1
本题主要难点在于如何处理dist^2的问题 40分算法 n^2暴力就不必多嘴,直接枚举根节点DFS就行了. 70分算法 对于b=0的情况,我们可以考虑用换根法来计算根节点的变化对总权值带来的影响. 换 ...