本文实例为大家分享了完整的 Java 分页拦截类 , 供大家参考 , 具体内容如下
Paket com.opms.interceptor; impor java.sql.connection; impor java.sql.preparedstatement; impor java.sql.resultset; import java.sql.sqlexception; import java.util.properties; impor org.apache.iBata.executor.Uxutor.util.properties; impor org.apache.iBata.executor.Uxutor.util.properties; impor org.apache.ibata.executor.execut.util.utile.properties; impor org.apache.iBata.executor.execut.util.util. org.apache.ibatis.executor.statement.statementhandler; impor org.apache.ibatis.logging.log; import org.apache.ibatis.logging.logfactory; import org.apache.ibatis.mapping.boundsql; impor org.apache.pache.apache.ibatis.mapping org.apache.ibatis.plugin.interceptor; impor org.apache.ibatis.plugin.intercepts; impor org.apache.ibatis.plugin.invokasi; impor org.apache.ibatis.plugin.plugin; impor org.apache.Ibatis.plugin.signature; orgake.apacye.apacye.apacy; org.apache.ibatis.reflection.factory.defaultObjectFactory; impor org.apache.ibatis.reflection.factory.objectfactory; import org.apache.apache.apache. org.apache.ibatis.scripting.defaults.defaultparameterhandler; import org.apache.Ibatis.Session.rowbounds; import com.wifi.core.page.page;/** * @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @, , , , , , , , , , , , , , , */@Intercepts ({@Signature (type = SympantionHandler.class, Method = "Persiapkan", args = {connection.class})}) kelas publik PageInceptor mengimplementasikan interceptor {/** *日志 */private static log Logger = logfactory.getLog (pageInceptor.class); / ** * 声明对象 */ Private Static ObjectFactory Default_object_factory = New DefaultObjectFactory (); / ** * 声明对象 */ Private Static Final ObjectWrapperFactory Default_object_wrapper_factory = New DefaultObjectWrapperFactory (); / ** * 数据库类型 (默认为 mysql) */ string statis pribadi defaultDialect = "mysql"; / ***需要拦截的 id (正则匹配)*/ string statis privat defaultPagesQlid = ".*4Page $"; / ** * 数据库类型 (默认为 mysql) */ private static string dialect = ""; / ** * 需要拦截的 id (正则匹配) */ private static string pagesqlid = ""; / ** * @param Invocation 参数 * @Return objek * @throws Throwable 抛出异常 */ Public Object Intercept (Invocation Invocation) melempar Throwable {Sym caraonHandler pernyataanHandler = (SymponyHandler) Invocation.getTarget (); MetaObject MetastatementHandler = metaObject.forObject (pernyataanhandler, default_object_factory, default_object_wrapper_factory); // 分离代理对象链 (由于目标类可能被多个拦截器拦截 , 从而形成多次代理 , 通过下面的两次循环可以分离出最原始的的目标类) while (metastatementhandler.hasgetter ("h")) {objek objek = metastatementhandler.getValue ("h"); MetastatementHandler = metaObject.forObject (objek, default_object_factory, default_object_wrapper_factory); } // 分离最后一个代理对象的目标类 while (metastatementhandler.hasgetter ("target")) {objek objek = metastatementhandler.getValue ("target"); MetastatementHandler = metaObject.forObject (objek, default_object_factory, default_object_wrapper_factory); } dialek = defaultDialect; pagesqlid = defaultPagesQlid; /* Konfigurasi konfigurasi = (konfigurasi) metastatementhandler.getValue ("delegate.configuration"); dialek = configuration.getVariable (). getProperty ("dialek"); if (null == dialek || "" .Equals (dialek)) {logger.warn ("dialek properti tidak diselesaikan, gunakan default 'mysql'"); dialek = defaultDialect; } pagesqlid = configuration.getVariables (). getProperty ("pagesqlid"); if (null == pagesqlid || "" .Equals (pagesqlid)) {logger.warn ("Properti Pagesqlid tidak diselesaikan, gunakan default '.*Halaman $'"); PAGESQLID = defaultPagesQLID; }*/ Mappedstatement mappedstatement = (mappedstatement) metastatementhandler.getValue ("delegate.mappedstatement"); // 只重写需要分页的sql语句。通过MappedStatement的ID匹配,默认重写以Page结尾的MappedStatement的sql if (mappedStatement.getId().matches(pageSqlId)) { BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); Objek parameterObject = boundsql.getParameterObject (); if (parameterObject == null) {throw new nullpointerException ("ParameterObject adalah null!"); } else {objek obj = metastatementhandler .getValue ("delegate.boundsql.parameterobject.page"); // 传入了 halaman 参数且需要开启分页时 if (obj! = Null && obj instance dari halaman && ((halaman) obj) .ispagination ()) {page page = (halaman) metastatementhandler .getValue ("delegate.boundsql.parameterobject.page"); String sql = boundsql.getsql (); // 重写 sql string pagesql = buildpagesql (sql, halaman); metastatementhandler.setValue ("delegate.boundsql.sql", pagesql); // 采用物理分页后 , 就不需要 mybatis 的内存分页了 , 所以重置下面的两个参数 Metastatementhandler.setValue ("delegate.rowbounds.offset", rowbounds.no_row_offset); metastatementhandler.setValue ("delegate.rowbounds.limit", rowbounds.no_row_limit); Koneksi koneksi = (koneksi) Invocation.getArgs () [0]; // 重设分页参数里的总页数等 SetPageParameter (SQL, Connection, MappedStatement, BoundSQL, Page); }}} // 将执行权交给下一个拦截器 return Invocation.proCed (); }/** * 从数据库里查询总的记录数并计算总页数 , 回写进分页参数 <code> pageparameter </code>, 这样调用者就可用通过 分页参数 * <code> pageparameter </code> 获得相关信息。 * * @param sql 参数 * @param Koneksi 连接 * @param mappedstatement 参数 * @param boundsql 绑定 sql * @param halaman 页 页 @param @param @priving @priving 页param MappedStatement MappedStatement, BoundSQL BoundSQL, halaman halaman) {// 记录总记录数 string countsql = "pilih count (0) dari (" + sql + ") sebagai total"; Disiapkan countstmt = null; Hasil rs = null; coba {countstmt = connection.preparestatement (countsql); BoundSQL countbs = boundSQL baru (mappedstatement.getConfiguration (), countsql, boundsql.getParametermappings (), boundsql.getParameterObject ()); setParameters (countstmt, mappedstatement, countbs, boundsql.getParameterObject ()); rs = countstmt.executeQuery (); int totalCount = 0; if (rs.next ()) {totalCount = rs.getint (1); } page.setTotalCount (TotalCount); page.init (page.getCurpage (), page.getPagesize (), totalCount); } catch (sqlexception e) {logger.error ("abaikan pengecualian ini", e); } akhirnya {coba {rs.close (); } catch (sqlexception e) {logger.error ("abaikan pengecualian ini", e); } coba {countstmt.close (); } catch (sqlexception e) {logger.error ("abaikan pengecualian ini", e); } } } /** * 对SQL参数(?)设值 * * @param ps 参数 * @param mappedStatement 参数 * @param boundSql 绑定sql * @param parameterObject 参数对象 * @throws SQLException 抛出sql异常 */ private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object ParameterObject) melempar sqlexception {parameterHandler parameterHandler = new DefaultParameterHandler (MappedStatement, ParameterObject, BoundSQL); parameterHandler.setParameters (PS); } / ** * 根据数据库类型 , 生成特定的分页 sql * * @param sql 餐宿 * @param halaman 页 * @return string * / string private buildpagesql (string sql, halaman halaman) {if (halaman! = Null) {stringBuilder pagesql = new stringBuilder (); if ("mysql" .equals (dialek)) {pagesql = buildpagesqlFormysql (sql, halaman); } lain jika ("oracle" .Equals (dialek)) {pagesql = buildpagesqlFororacle (sql, halaman); } else {return sql; } return pagesql.toString (); } else {return sql; }} / ** * mysql 的分页语句 * * @param sql 参数 * @param page 页 * @return string * / public stringBuilder buildpagesqlformysql (string sql, halaman halaman) {stringbuilder pagesql = new stringBuilder (100); String beginrow = string.valueof ((page.getCurpage () - 1) * page.getPagesize ()); PAGESQL.Append (SQL); PAGESQL.Append ("Batas" + beginrow + "," + page.getPagesize ()); mengembalikan PAGESQL; } / ** * 参考 hibernate 的实现完成 oracle 的分页 * * @param sql 参数 * @param halaman 参数 * @return string * / public stringBuilder buildpagesqlFororacle (string sql, halaman halaman) {stringBuilder pagesql = new stringBuilder (100); String beginrow = string.valueof ((page.getCurpage () - 1) * page.getPagesize ()); String endrow = string.valueof (page.getCurpage () * page.getPagesize ()); pagesql.append ("pilih * dari (pilih temp. *, rownum row_id from ("); pagesql.append (sql); pagesql.append (") temp mengembalikan PAGESQL; } / ** * @param target 参数 * @return objek * / plugin objek publik (target objek) {// 当目标类是 Pernyataan Tulisan 类型时 , 才包装目标类 ,, 减少目标被代理的次数 减少目标被代理的次数 if (target instanceof pernyataanHandler) {return plugin.wrap (target, ini); } else {return target; }} / ** * @param Properties 参数 * / public void setProperties (properti properti) {}}以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。