@Intercepts ({@Signature (type = Statementhandler.class, method = "Prepare", args = {connection.class})}) public class Pagi Nationinterceptor implements Interceptor {private static final Log logger = Logfactory. GetLog (paginationintercep
Tor.class);
private static final Objectfactory default_object_factory = new Defaultobjectfactory ();
private static final Objectwrapperfactory default_object_wrapper_factory = new Defaultobjectwrapperfactory (); private static String Defaultdialect = "MySQL"; Database type (default is MySQL) private static String Defaultpagesqlid = ". *page$"; ID to intercept (regular match) private static String dialect = ""; Database type (default is MYsql) private static String pagesqlid = ""; ID (regular match) that needs to be intercepted @Override public Object intercept (invocation invocation) throws Throwable {Statem
Enthandler Statementhandler = (statementhandler) invocation. Gettarget ();
MetaObject Metastatementhandler = Metaobject.forobject (Statementhandler, Default_object_factory,
Default_object_wrapper_factory);
Detach the proxy object chain (since the target class may be intercepted by multiple interceptors, thus forming multiple proxies, the most primitive target class can be separated by the following two cycles) while (Metastatementhandler.hasgetter ("H")) {
Object object = Metastatementhandler.getvalue ("h"); Metastatementhandler = Metaobject.forobject (object, Default_object_factory, DEFAULT_OBJECT_WRAPPER_FAC
TORY); }//Detach the target class of the last proxy object while (Metastatementhandler.hasgetter ("target")) {Object object = Metastatementhandler.getvalue ("Target"); Metastatementhandler = Metaobject.forobject (object, Default_object_factory, DEFAULT_OBJECT_WRAPPER_FAC
TORY); The Configuration Configuration = (configuration) metastatementhandler. GetValue ("Delegate.configura
tion ");
dialect = Configuration.getvariables () ==null?null:configuration.getvariables (). GetProperty ("dialect"); if (null = = Dialect | |
". Equals (dialect)) {Logger.warn (" property dialect was not setted,use default ' MySQL ');
dialect = Defaultdialect;
} pagesqlid = Configuration.getvariables () ==null?null:configuration.getvariables (). GetProperty ("Pagesqlid"); if (null = = Pagesqlid | |
". Equals (Pagesqlid)) {Logger.warn (" property Pagesqlid was not setted,use default '. *page$ ' ");
Pagesqlid = Defaultpagesqlid;
} mappedstatement mappedstatement = (mappedstatement) metastatementhandler . GetValue ("Delegate.mappedstatement"); Only the SQL statements that require paging are rewritten.
By Mappedstatement ID matching, the default overrides the Mappedstatement SQL if (Mappedstatement.getid (). Matches (Pagesqlid)) ending with a page {
Boundsql Boundsql = (boundsql) metastatementhandler. GetValue ("Delegate.boundsql");
Object parameterobject = Boundsql.getparameterobject ();
if (Parameterobject = = null) {throw new NullPointerException ("Parameterobject is null!");
} else {Pagination page = (pagination) 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, etc. 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 the total pages, write back into the paging parameter <code>pageparameter</code>, so that callers can be used by paging parameters * <code>page
Parameter</code> get the relevant information.
* * @param SQL * @param connection * @param mappedstatement * @param boundsql * @param page */private void Setpageparameter (String sql, Connection Connection, Mappedstatement mappedstatement, Boun DSQL boundsql, pagination page) {//Records total record Count String Countsql = "SELECT count (0) from (" + SQL + ") as Total";
PreparedStatement countstmt = null;
ResultSet rs = null;
try {countstmt = connection.preparestatement (Countsql); Boundsql Countbs = new Boundsql (Mappedstatement.getconfiguration (), Countsql, Boundsql.getparametermap
Pings (), boundsql.getparameterobject ());
Setparameters (countstmt, Mappedstatement, Countbs, Boundsql.getparameterobject ());
rs = Countstmt.executequery ();
int totalcount = 0;
if (Rs.next ()) {totalcount = Rs.getint (1);
} page.settotalcount ((long) totalcount);
int totalpage = totalcount/page.getpagesize () + ((totalcount% page.getpagesize () = = 0)? 0:1);
Page.settotalpage (Totalpage); } 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); }}}/** * to SQL parameters (?) Set Value * * @param PS * @param mappedstatement * @param boundsql * @param parameterobject * @thr OWS SQLException */private void Setparameters (PreparedStatement PS, Mappedstatement mappedstatement, Boundsql Boundsql, Object parameterobject) throws SQLException {Parameterhandler Parameterhandle
R = new Defaultparameterhandler (mappedstatement, Parameterobject, Boundsql); Parameterhandler.setparameters (PS); /** * Generates a specific paging SQL based on the database type * * @param SQL * @param page * @return */Private Strin G Buildpagesql (String sql, pagination page) {if (page! = null) {String pagesql = new STR
ING (); if ("MySQL". Equals (dialect)) {pagesql = Mysqldialect.getinstance (). getlimitstring (SQL, page.
Getstartindex (), page.getpagesize ()); } else if ("Oracle". Equals (dialect)) {pagesql = Oracledialect.getinstance (). Getl
imitstring (SQL, Page.getstartindex (), page.getpagesize ());
} else {return SQL;
} return pagesql;
} else {return SQL; }} @Override public object plugin (object target) {//when the target class is the Statementhandler type, the target class is wrapped and no one returns directly
Back to the target itself, reducing the number of times the target is proxied if (target instanceof Statementhandler) { Return Plugin.wrap (target, this);
} else {return target; }} @Override public void SetProperties (properties properties) {}}