parent
ac8e453aa1
commit
b00d19ce1a
@ -0,0 +1,75 @@
|
||||
package com.platform.utils.page;
|
||||
|
||||
public class Page {
|
||||
/** 当前是第几页 (当前页) */
|
||||
// private int index = 0;
|
||||
private int currentPageNum = 1;
|
||||
|
||||
/** 每页显示多少条 (当前页前面已有多少条数据:一次查询返回记录条数)*/
|
||||
// private int pageSize = 10;
|
||||
private int limit = 20;
|
||||
|
||||
/** 总共的数据量 */
|
||||
// private int totle;
|
||||
private int totleSize;
|
||||
|
||||
/** 共有多少页 */
|
||||
private int totlePage;
|
||||
|
||||
/**
|
||||
* @return the currentPageNum
|
||||
*/
|
||||
public int getCurrentPageNum() {
|
||||
return currentPageNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param currentPageNum the currentPageNum to set
|
||||
*/
|
||||
public void setCurrentPageNum(int currentPageNum) {
|
||||
this.currentPageNum = currentPageNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the limit
|
||||
*/
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param limit the limit to set
|
||||
*/
|
||||
public void setLimit(int limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the totleSize
|
||||
*/
|
||||
public int getTotleSize() {
|
||||
return totleSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param totleSize the totleSize to set
|
||||
*/
|
||||
public void setTotleSize(int totleSize) {
|
||||
this.totleSize = totleSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the totlePage
|
||||
*/
|
||||
public int getTotlePage() {
|
||||
return totlePage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param totlePage the totlePage to set
|
||||
*/
|
||||
public void setTotlePage(int totlePage) {
|
||||
this.totlePage = totlePage;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,194 @@
|
||||
package com.platform.utils.page;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ibatis.plugin.Interceptor;
|
||||
import org.apache.ibatis.plugin.Invocation;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ibatis.executor.resultset.ResultSetHandler;
|
||||
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.MetaObject;
|
||||
import org.apache.ibatis.reflection.SystemMetaObject;
|
||||
|
||||
import com.platform.utils.Bean2MapUtils;
|
||||
|
||||
@Intercepts({
|
||||
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class}),
|
||||
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
|
||||
})
|
||||
public class PageInterceptor implements Interceptor {
|
||||
|
||||
|
||||
/** 存储所有语句名称 */
|
||||
/* HashMap<String, String> map_statement = new HashMap<String, String>();*/
|
||||
|
||||
/** 用户提供分页计算条数后缀 */
|
||||
private static final String SELECT_ID="page";
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
|
||||
if (invocation.getTarget() instanceof StatementHandler) {
|
||||
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
|
||||
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
|
||||
MappedStatement mappedStatement=(MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
|
||||
String selectId=mappedStatement.getId();
|
||||
// System.out.println(" ---------- selectId ---- : " + selectId);
|
||||
if(selectId.substring(selectId.lastIndexOf(".")+1).toLowerCase().endsWith(SELECT_ID)){
|
||||
BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
|
||||
// 分页参数作为参数对象parameterObject的一个属性
|
||||
String sql = boundSql.getSql();
|
||||
Page co=(Page)(boundSql.getParameterObject());
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = Bean2MapUtils.convertBean(boundSql.getParameterObject());
|
||||
if (null == co) {
|
||||
co = new Page();
|
||||
}
|
||||
|
||||
// 重写 Sql语句
|
||||
String countSql=concatCountSql(sql);
|
||||
// while (countSql.endsWith(" ") || countSql.endsWith("\t")
|
||||
// || countSql.endsWith("\r") || countSql.endsWith("\n")) {
|
||||
// countSql = countSql.substring(0, countSql.length()-1);
|
||||
// }
|
||||
try{
|
||||
if(countSql.contains("?")) {
|
||||
StringBuffer sbr = new StringBuffer();
|
||||
String[] item = countSql.split("\\?");
|
||||
for (int i = 0; i < item.length; i++) {
|
||||
sbr.append(item[i]);
|
||||
String[] s = item[i].split("\\.");
|
||||
String[] re = s[s.length-1].split("\\=");
|
||||
if(map.keySet().contains(re[re.length-1])){
|
||||
sbr.append(map.get(re[re.length-1]));
|
||||
}
|
||||
else {
|
||||
sbr.append(" ");
|
||||
}
|
||||
}
|
||||
countSql = sbr.toString();
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
String pageSql=concatPageMySql(sql,co);
|
||||
//
|
||||
// System.out.println("重写的 count sql :"+countSql);
|
||||
// System.out.println("重写的 select sql :"+pageSql);
|
||||
Connection connection = (Connection) invocation.getArgs()[0];
|
||||
|
||||
PreparedStatement countStmt = null;
|
||||
ResultSet rs = null;
|
||||
int totalCount = 0;
|
||||
try {
|
||||
countStmt = connection.prepareStatement(countSql);
|
||||
rs = countStmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
totalCount = rs.getInt(1);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
rs.close();
|
||||
countStmt.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
|
||||
//绑定count
|
||||
co.setTotleSize(totalCount);
|
||||
co.setTotlePage(totalCount/co.getLimit());
|
||||
if (co.getCurrentPageNum() > co.getTotlePage()) {
|
||||
co.setCurrentPageNum(co.getTotlePage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
/**
|
||||
* <一句话功能简述> 重写sql
|
||||
* <功能详细描述>
|
||||
* @param sql
|
||||
* @return
|
||||
* @see [类、类#方法、类#成员]
|
||||
*/
|
||||
public String concatCountSql(String sql){
|
||||
StringBuffer sb=new StringBuffer("select count(*) from ");
|
||||
// StringBuffer sb=new StringBuffer("select count(*) from (");
|
||||
// sb.append(sql).append(") as a");
|
||||
//
|
||||
sql=sql.toLowerCase();
|
||||
if(sql.lastIndexOf("order")>sql.lastIndexOf(")")){
|
||||
sb.append(sql.substring(sql.indexOf("from")+4, sql.lastIndexOf("order")));
|
||||
}else{
|
||||
sb.append(sql.substring(sql.indexOf("from")+4));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <一句话功能简述> 重写sql
|
||||
* <功能详细描述>
|
||||
* @param sql
|
||||
* @param page
|
||||
* @return
|
||||
* @see [类、类#方法、类#成员]
|
||||
*/
|
||||
public String concatPageMySql(String sql,Page page){
|
||||
StringBuffer sb=new StringBuffer();
|
||||
sb.append(sql);
|
||||
int size = page.getLimit();
|
||||
int index = page.getCurrentPageNum();
|
||||
if (index > 1) {
|
||||
index = index -1;
|
||||
sb.append(" limit ").append(size*index).append(" , ").append(size);
|
||||
}
|
||||
else {
|
||||
sb.append(" limit ").append(0).append(" , ").append(size);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截类型StatementHandler
|
||||
*/
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
if (target instanceof StatementHandler) {
|
||||
return Plugin.wrap(target, this);
|
||||
} else {
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
public void setPageCount(){
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue