1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
| 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.*; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.apache.ibatis.scripting.defaults.DefaultParameterHandler; import org.apache.ibatis.session.Configuration;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import java.util.Properties; import java.util.Set;
@Intercepts({@Signature( type = StatementHandler.class, method = "prepare", args = {Connection.class} )}) public class PagingPlugin implements Interceptor {
/** * 默认页码 */ private Integer defaultPageIndex; /** * 默认每页数据条数 */ private Integer defaultPageSize;
public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = getUnProxyObject(invocation); MetaObject metaObject = SystemMetaObject.forObject(statementHandler); String sql = getSql(metaObject); if (!checkSelect(sql)) { // 不是select语句,进入责任链下一层 return invocation.proceed(); }
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql"); Object parameterObject = boundSql.getParameterObject(); Page page = getPage(parameterObject); if (page == null) { // 没有传入page对象,不执行分页处理,进入责任链下一层 return invocation.proceed(); }
// 设置分页默认值 if (page.getPageIndex() == null) { page.setPageIndex(this.defaultPageIndex); } if (page.getPageSize() == null) { page.setPageSize(this.defaultPageSize); } // 设置分页总数,数据总数 setTotalToPage(page, invocation, metaObject, boundSql); // 校验分页参数 checkPage(page); return changeSql(invocation, metaObject, boundSql, page); }
public Object plugin(Object target) { // 生成代理对象 return Plugin.wrap(target, this); }
public void setProperties(Properties properties) { // 初始化配置的默认页码,无配置则默认1 this.defaultPageIndex = Integer.parseInt(properties.getProperty("default.pageIndex", "1")); // 初始化配置的默认数据条数,无配置则默认20 this.defaultPageSize = Integer.parseInt(properties.getProperty("default.pageSize", "20")); }
/** * 从代理对象中分离出真实对象 * * @param invocation * @return */ private StatementHandler getUnProxyObject(Invocation invocation) { // 取出被拦截的对象 StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); MetaObject metaStmtHandler = SystemMetaObject.forObject(statementHandler); Object object = null; // 分离代理对象 while (metaStmtHandler.hasGetter("h")) { object = metaStmtHandler.getValue("h"); metaStmtHandler = SystemMetaObject.forObject(object); }
return object == null ? statementHandler : (StatementHandler) object; }
/** * 判断是否是select语句 * * @param sql * @return */ private boolean checkSelect(String sql) { // 去除sql的前后空格,并将sql转换成小写 sql = sql.trim().toLowerCase(); return sql.indexOf("select") == 0; }
/** * 获取分页参数 * * @param parameterObject * @return */ private Page getPage(Object parameterObject) { if (parameterObject == null) { return null; }
if (parameterObject instanceof Map) { // 如果传入的参数是map类型的,则遍历map取出Page对象 Map<String, Object> parameMap = (Map<String, Object>) parameterObject; Set<String> keySet = parameMap.keySet(); for (String key : keySet) { Object value = parameMap.get(key); if (value instanceof Page) { // 返回Page对象 return (Page) value; } } } else if (parameterObject instanceof Page) { // 如果传入的是Page类型,则直接返回该对象 return (Page) parameterObject; }
// 初步判断并没有传入Page类型的参数,返回null return null; }
/** * 获取数据总数 * * @param invocation * @param metaObject * @param boundSql * @return */ private int getTotal(Invocation invocation, MetaObject metaObject, BoundSql boundSql) { // 获取当前的mappedStatement对象 MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); // 获取配置对象 Configuration configuration = mappedStatement.getConfiguration(); // 获取当前需要执行的sql String sql = getSql(metaObject); // 改写sql语句,实现返回数据总数 $_paging取名是为了防止数据库表重名 String countSql = "select count(*) as total from (" + sql + ") $_paging"; // 获取拦截方法参数,拦截的是connection对象 Connection connection = (Connection) invocation.getArgs()[0]; PreparedStatement pstmt = null; int total = 0;
try { // 预编译查询数据总数的sql语句 pstmt = connection.prepareStatement(countSql); // 构建boundSql对象 BoundSql countBoundSql = new BoundSql(configuration, countSql, boundSql.getParameterMappings(), boundSql.getParameterObject()); // 构建parameterHandler用于设置sql参数 ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), countBoundSql); // 设置sql参数 parameterHandler.setParameters(pstmt); //执行查询 ResultSet rs = pstmt.executeQuery(); while (rs.next()) { total = rs.getInt("total"); } } catch (SQLException e) { e.printStackTrace(); } finally { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } }
// 返回总数据数 return total; }
/** * 设置总数据数、总页数 * * @param page * @param invocation * @param metaObject * @param boundSql */ private void setTotalToPage(Page page, Invocation invocation, MetaObject metaObject, BoundSql boundSql) { // 总数据数 int total = getTotal(invocation, metaObject, boundSql); // 计算总页数 int totalPage = total / page.getPageSize(); if (total % page.getPageSize() != 0) { totalPage = totalPage + 1; }
page.setTotal(total); page.setTotalPage(totalPage); }
/** * 校验分页参数 * * @param page */ private void checkPage(Page page) { // 如果当前页码大于总页数,抛出异常 if (page.getPageIndex() > page.getTotalPage()) { throw new RuntimeException("当前页码[" + page.getPageIndex() + "]大于总页数[" + page.getTotalPage() + "]"); } // 如果当前页码小于总页数,抛出异常 if (page.getPageIndex() < 1) { throw new RuntimeException("当前页码[" + page.getPageIndex() + "]小于[1]"); } }
/** * 修改当前查询的sql * * @param invocation * @param metaObject * @param boundSql * @param page * @return */ private Object changeSql(Invocation invocation, MetaObject metaObject, BoundSql boundSql, Page page) throws Exception { // 获取当前查询的sql String sql = getSql(metaObject); // 修改sql,$_paging_table_limit取名是为了防止数据库表重名 String newSql = "select * from (" + sql + ") $_paging_table_limit limit ?, ?"; // 设置当前sql为修改后的sql setSql(metaObject, newSql);
// 获取PreparedStatement对象 PreparedStatement pstmt = (PreparedStatement) invocation.proceed(); // 获取sql的总参数个数 int parameCount = pstmt.getParameterMetaData().getParameterCount(); // 设置分页参数 pstmt.setInt(parameCount - 1, (page.getPageIndex() - 1) * page.getPageSize()); pstmt.setInt(parameCount, page.getPageSize());
return pstmt; }
/** * 获取当前查询的sql * * @param metaObject * @return */ private String getSql(MetaObject metaObject) { return (String) metaObject.getValue("delegate.boundSql.sql"); }
/** * 设置当前查询的sql * * @param metaObject */ private void setSql(MetaObject metaObject, String sql) { metaObject.setValue("delegate.boundSql.sql", sql); } }
|