This article for you to share a complete Java paging class for your reference, the specific contents are as follows
Package com.opms.interceptor;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;
Import Org.apache.ibatis.executor.parameter.ParameterHandler;
Import Org.apache.ibatis.executor.statement.StatementHandler;
Import Org.apache.ibatis.logging.Log;
Import Org.apache.ibatis.logging.LogFactory;
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.factory.DefaultObjectFactory;
Import Org.apache.ibatis.reflection.factory.ObjectFactory;
Import Org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; Import Org.apache.ibatis.reflection.wrapper.ObjectWrappErfactory;
Import Org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
Import Org.apache.ibatis.session.RowBounds;
Import Com.wifi.core.page.Page;
/** * To implement physical paging by overriding SQL statements by intercepting <code>StatementHandler</code> <code>prepare</code> methods.
* As usual, the type to be intercepted in the signature can only be an interface. * * @author Lakeside Breeze */@Intercepts ({@Signature (type = Statementhandler.class, method = "Prepare", args = {CONNECTION.C Lass})}) public class Pageinterceptor implements interceptor {/** * Log/private static final Log logger = Logfact
Ory.getlog (Pageinterceptor.class);
/** * Declaration object/private static final objectfactory default_object_factory = new Defaultobjectfactory (); /** * Declaration object/private static final objectwrapperfactory default_object_wrapper_factory = new Defaultobjectwrapperfact
Ory ();
/** * Database type (default is MySQL)/private static String Defaultdialect = "MySQL";
/** * need to intercept ID (matching) * * private static String Defaultpagesqlid = ". *4page$"; /** * Database type (default is MySQL) */private static String dialect = "";
/** * need to intercept ID (regular match) * * private static String pagesqlid = ""; /** * @param invocation parameter * @return Object * @throws Throwable throws an exception/public object intercept (invocation Invoca
tion) throws Throwable {Statementhandler Statementhandler = (statementhandler) invocation.gettarget (); MetaObject Metastatementhandler = Metaobject.forobject (Statementhandler, Default_object_factory, DEFAULT_OBJECT_
Wrapper_factory); Detach the Agent object chain (because the target class may be blocked by multiple interceptors to form multiple proxies, the most original target class can be separated by the following two cycles) while (Metastatementhandler.hasgetter ("H")) {Object obj
ECT = Metastatementhandler.getvalue ("h");
Metastatementhandler = Metaobject.forobject (object, Default_object_factory, default_object_wrapper_factory); The target class of the last proxy object, while (Metastatementhandler.hasgetter ("target")) {object = Metastatementhandler.getvalu
E ("target");
Metastatementhandler = Metaobject.forobject (object, Default_object_factory, default_object_wrapper_factory); } DIalect=defaultdialect;pagesqlid=defaultpagesqlid;
/* Configuration Configuration = (Configuration) metastatementhandler.getvalue ("Delegate.configuration");
dialect = Configuration.getvariables (). GetProperty ("dialect"); if (null = = Dialect | |
"". Equals (dialect)) {Logger.warn ("property dialect isn't setted,use default ' MySQL '");
dialect = Defaultdialect;
} pagesqlid = Configuration.getvariables (). GetProperty ("Pagesqlid"); if (null = = Pagesqlid | |
'. Equals (Pagesqlid)} {Logger.warn (' property pagesqlid isn't setted,use default '. *page$ ');
Pagesqlid = Defaultpagesqlid;
}*/mappedstatement mappedstatement = (mappedstatement) metastatementhandler.getvalue ("Delegate.mappedStatement"); Rewrite only the SQL statements that require paging. By matching the ID of the mappedstatement, the Mappedstatement SQL if (Mappedstatement.getid (). Matches (Pagesqlid)) {Boundsql) is overridden by default at page end
Boundsql = (boundsql) metastatementhandler.getvalue ("Delegate.boundsql");
Object parameterobject = Boundsql.getparameterobject (); if (pArameterobject = = null) {throw new NullPointerException ("Parameterobject is null!");
else {Object obj = Metastatementhandler. GetValue ("Delegate.boundSql.parameterObject.page"); Obj!=null&&obj instanceof Page && ((Page) obj. Ispagination ()) {page page = ()) When page parameters are passed in and you need to open paging.
(Page) Metastatementhandler. GetValue ("Delegate.boundSql.parameterObject.page");
String sql = Boundsql.getsql ();
Rewrite sql String pagesql = buildpagesql (sql, page);
Metastatementhandler.setvalue ("Delegate.boundSql.sql", pagesql); With physical paging, you do not need mybatis memory paging, so reset the following two parameters Metastatementhandler.setvalue ("Delegate.rowBounds.offset", Rowbounds.no_
Row_offset);
Metastatementhandler.setvalue ("Delegate.rowBounds.limit", rowbounds.no_row_limit);
Connection Connection = (Connection) invocation.getargs () [0];
Resets the total number of pages in the paging parameter such as setpageparameter (SQL, Connection, mappedstatement, Boundsql, page);
Give execution to the next interceptor return Invocation.proceed (); /** * Query The total number of records from the database and calculate total pages, write back to the paging parameter <code>pageparameter</code>, so that the caller can be used through the paging parameter * <code>pageparameter
</code> obtain relevant information.
* * @param SQL parameter * @param connection connection * @param mappedstatement parameter * @param boundsql bound SQL * @param page * private void Setpageparameter (String sql, Connection Connection, Mappedstatement mappedstatement, Boundsql boundsql
, page page {//Record total record number String Countsql = "SELECT count (0) from (" + SQL + ") as totals";
PreparedStatement countstmt = null;
ResultSet rs = null;
try {countstmt = connection.preparestatement (Countsql); Boundsql Countbs = new Boundsql (Mappedstatement.getconfiguration (), Countsql, Boundsql.getparametermappings (), BoundS
Ql.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 ("Ignore This exception", e);
Finally {try {rs.close ();
catch (SQLException e) {logger.error ("Ignore This exception", e);
try {countstmt.close ();
catch (SQLException e) {logger.error ("Ignore This exception", e); }}/** * pair SQL parameters (?) Set Value * * @param ps parameter * @param mappedstatement parameter * @param boundsql binding SQL * @param parameterobject Parameter Object * @thro WS SQLException throws SQL Exception */private void Setparameters (PreparedStatement PS, mappedstatement mappedstatement, Boundsql b Oundsql, Object parameterobject) throws SQLException {Parameterhandler Parameterhandler = new Defaultparameterhandle
R (Mappedstatement, Parameterobject, Boundsql);
Parameterhandler.setparameters (PS); /** * Based on database type, generate specific paging SQL * * @param SQL Meal * @param page * @return string/private String buildpages QlString SQL, page page {if (page!= null) {StringBuilder pagesql = new StringBuilder ();
if ("MySQL". Equals (dialect)) {pagesql = buildpagesqlformysql (sql, page);
else if ("Oracle". Equals (dialect)) {pagesql = buildpagesqlfororacle (sql, page);
else {return SQL;
return pagesql.tostring ();
else {return SQL; /** * MySQL Paging statement * * @param SQL Parameter * @param page * @return String/public StringBuilder Buildpage
Sqlformysql (String SQL, page page) {StringBuilder pagesql = new StringBuilder (100);
String Beginrow = string.valueof (Page.getcurpage ()-1) * page.getpagesize ());
Pagesql.append (SQL);
Pagesql.append ("Limit" + Beginrow + "," + page.getpagesize ());
return pagesql; Implementation of/** * Reference hibernate complete Oracle Paging * * @param SQL Parameter * @param page parameter * @return String/Public Stringbuil
Der Buildpagesqlfororacle (String sql, page page) {StringBuilder pagesql = new StringBuilder (100); String Beginrow =String.valueof ((Page.getcurpage ()-1) * page.getpagesize ());
String Endrow = string.valueof (Page.getcurpage () * page.getpagesize ());
Pagesql.append ("SELECT *" (select temp.*, RowNum row_id from (");
Pagesql.append (SQL);
Pagesql.append (") Temp where rownum <="). Append (Endrow);
Pagesql.append (") where row_id >"). Append (Beginrow);
return pagesql; /** * @param Target parameter * @return object/public object plugin (object target) {//when the target class is a Statementhandler type
Packaging target class, no person directly returns the target itself, reducing the number of times the target is being represented if (target instanceof Statementhandler) {return Plugin.wrap (target, this);
else {return target;
}/** * @param properties parameter */public void SetProperties (properties properties) {}}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.