1. 封装分页 الصفحة 类
package com.framework.common.page.impl ؛ استيراد java.io.serializable ؛ استيراد com.framework.common.page.ipage ؛/** * * * *//public public class pasepage ipage ، serializable {/** * */ private Static Final Long SerialVersionuid = -362344861275790359L ؛ int static int default_page_size = 20 ؛ Private int pagesize = default_page_size ؛ Private Int CurrentResult ؛ private int totalpage ؛ Private Int Currentpage = 1 ؛ private int totalCount = -1 ؛ basepage العامة (int currentpage ، int pagesize ، int totalCount) {this.currentPage = currentPage ؛ this.pagesize = pagesize ؛ this.totalCount = TotalCount ؛ } public int getTotalCount () {return this.totalCount ؛ } public void settoTalCount (int totalCount) {if (totalCount <0) {this.totalCount = 0 ؛ يعود؛ } this.totalCount = TotalCount ؛ } public basepage () {} public int getFirStresult () {return (this.currentPage - 1) * this.pagesize ؛ } public void setPagesize (int pagesize) {if (pagesize <0) {this.pagesize = default_page_size ؛ يعود؛ } this.pagesize = pagesize ؛ } public int getTotalPage () {if (this.totalPage <= 0) {this.totalPage = (this.totalCount / this.pagesize) ؛ if ((this.totalPage == 0) || (this.totalCount ٪ this.pagesize! = 0)) {this.totalPage += 1 ؛ }} إرجاع this.totalPage ؛ } public int getPagesize () {return this.pagesize ؛ } public void setPageno (int currentpage) {this.currentPage = currentPage ؛ } public int getPageno () {return this.currentPage ؛ } boolean public isFirstPage () {return this.currentPage <= 1 ؛ } islastpage boolean العامة () {return this.currentPage> = getTotalPage () ؛ } public int getNextPage () {if (iSlastPage ()) {return this.currentPage ؛ } إرجاع this.currentPage + 1 ؛ } public int getCurrentResult () {this.currentResult = ((getPageno () - 1) * getPagesize ()) ؛ if (this.currentResult <0) {this.currentResult = 0 ؛ } إرجاع this.currentResult ؛ } public int getPrepage () {if (isFirstPage ()) {return this.currentPage ؛ } إرجاع this.currentPage - 1 ؛ }}
package com.framework.common.page.impl ؛ استيراد java.util.list ؛/** * * * */صفحة الفئة العامة تمتد BasePage {/** * */Private Static Final Long SerialVersionuid = -97017792870937315L ؛ public static threadlocal <Page> threadlocal = new threadlocal <age> () ؛ قائمة خاصة <؟> البيانات ؛ public page () {} الصفحة العامة (int currentpage ، int pagesize ، int totalCount) {super (currentpage ، pagesize ، totalCount) ؛ } الصفحة العامة (int currentpage ، int pagesize ، int totalCount ، list <؟> data) {super (currentpage ، pagesize ، totalCount) ؛ this.data = البيانات ؛ } القائمة العامة <؟> getData () {return data ؛ } public void setData (قائمة <؟> data) {this.data = data ؛ }}
2. 封装分页插件
package com.framework.common.page.plugin ؛ استيراد java.sql.connection ؛ استيراد java.sql.preparedstatement ؛ استيراد java.sql.resultset ؛ .properties ؛ import javax.xml.bind.propertyexception ؛ import org.apache.commons.lang3.stringUtils ؛ import org.apache.ibatis.executor.errorContext ؛ import org.apache.ibatis.executor.executorexception ؛ import org.apach. ibatis.executor.statement.basestatementHandler ؛ import org.apache.ibatis.executor.statement.RoutingStateMentHandler ؛ import org.apache.ibatis.mapping.boundsql ؛ eSport org.apache.ibatis.mapping.mpsingStatement ؛ mapping.parametermapping ؛ import org.apache.ibatis.mapping.parametermode ؛ import org.apache.ibatis.plugin.Interceptor ؛ import org.apache.ibatis.plugin.interce .apache.ibatis.plugin.plugin ؛ import org.apache.ibatis.reflection.metaobject ؛ import org.apache.ibatis.reflection.property.propertytokenizer ؛ import org.apache.ibatis.scripting.xmltags.foreachsqlnode ؛ .ibatis.session.configuration ؛ import org.apache.ibatis.type.typehandler ؛ import org.apache.ibatis.type.typeHandlerRegistry ؛ import com.framework.common.page.impl.page .reflectutil ؛/** * * * * */ @intercepts ({org.apache.ibatis.plugin.signature (type = org.apache.ibatis.executor.statement.statementhandler.class ، method = "prepared" ، args = {connection.class})}) Public Class PagePlugin تنفذ Interceptor {private string dialect = "" ؛ سلسلة خاصة pagesqlid = "" ؛ Override اعتراض الكائن العام (استدعاء الاستدعاء) يلقي رمي {if (invocation.getTarget () مثيل RoutingStateMentHandler) {basestatementHandler Depegate = (basestatementhandler) respectUtil .getValueByBifieldName ((RoutingStateMentHandler) invocation.getTarget () ، "DeLegate") ؛ MedpedStatement MedpedStatement = (mappedStatement) ReflectUtil .getValueByByfieldName (مندوب ، "mappedStatement") ؛ صفحة page = page.throadlocal.get () ؛ if (page == null) {page = new page () ؛ page.throadlocal.set (صفحة) ؛ } if (medpedStatement.getId (). Matches (".*(" + this.pagesqlid + ") $") && page.getPagesize ()> 0) {boundsql boundsql = devate.getBoundSql () ؛ parameterObject = boundsql.getParameterObject () ؛ String sql = boundsql.getsql () ؛ string countsqlid = mappedStatement.getId (). replaceall (pagesqlid ، "count") ؛ medpedStatement countmappedStatement = null ؛ if (mappedStatement.getConfiguration (). hasstatement (countsqlid)) {countmappedStatement = mappedStatement.getConfiguration (). getMappedStatement (countsqlid) ؛ } سلسلة countsql = null ؛ if (countmappedStatement! = null) {countsql = countmappedStatement.getBoundSql (parameterObject) .getSql () ؛ } else {countsql = "select count (1) from (" + sql + ") t_count" ؛ } int totalCount = 0 ؛ reparedStatement countStmt = null ؛ resultset resultset = null ؛ حاول {connection connection = (connect) invocation.getargs () [0] ؛ countStmt = connection.preparestatement (countsql) ؛ boundsql countboundsql = new BONDSQL (MEPTEDSTATEMTEMN.GetConfiguration () ، countsql ، boundsql.getParameterMappings () ، parameterObject) ؛ setParameters (CountStmt ، MedpedStatement ، countBoundSql ، parameterObject) ؛ resultset = countStmt.executequery () ؛ if (resultset.next ()) {totalCount = resultSet.getInt (1) ؛ }} catch (استثناء e) {throw e ؛ } أخيرًا {try {if (resultset! = null) {resultset.close () ؛ }} أخيرًا {if (countStmt! = null) {countStmt.Close () ؛ }}} page.settotalCount (TotalCount) ؛ strenseutil.setValuebyFieldName (boundsql ، "sql" ، generatepagesql (sql ، page)) ؛ }} return invocation.proceed () ؛ } / ** * 对 sql 参数 (؟) 设值 ، 参考 org.apache.ibatis.executor.parameter.defaultParameterHandler * param ps * param mappedStatement * param boundsql * param parameteroBject setParameters (preparedStatement PS ، MedpedStatement MedpedStatement ، BoundSQL BONDSQL ، Object ParameterObject) يلقي sqlexception {errorContext.Instance (). النشاط ("إعداد معلمات"). كائن (medstatement.getParameterMap (). getid ()) ؛ قائمة <ParmeterMapping> paramEterMappings = boundsql.getParamEterMappings () ؛ if (parameterMappings! = null) {configuration configuration = medpedStatement.getConfiguration () ؛ typeHandlerRegistry typeHandlerRegistry = configuration.getTyPeHandlerRegistry () ؛ metaObject metaObject = parameterObject == null؟ NULL: configuration.newmetaobject (parameterObject) ؛ لـ (int i = 0 ؛ i <parametermappings.size () ؛ i ++) {parameterMapping parameterMapping = parameterMappings.get (i) ؛ if (parameterMapping.getMode ()! = parametermode.out) {value object ؛ string propertyName = parameTerMapping.getProperty () ؛ PropertyTokenizer Prop = New PropertyTokenizer (PropertyName) ؛ if (parameterObject == null) {value = null ؛ } آخر إذا (typeHandlerRegistry.hastypeHandler (parameterObject.getClass ())) {value = parameterObject ؛ } if if (boundsql.hasadditionalparameter (propertyName)) {value = boundsql.getAdditionalParameter (propertyName) ؛ } if if (propertyName.StartSwith (foreachsqlnode.item_prefix) && bONDSQL.HasAdditionalParameter (prop.getName ())) {value = boundsql.getAdditionalParameter (prop.getName ()) ؛ if (value! = null) {value = configuration.newmetaobject (value) .getValue (propertyName.subString (prop.getName (). length ())) ؛ }} آخر {value = metaObject == null؟ null: metaObject.getValue (propertyName) ؛ } typeHandler typeHandler = paramEterMapping.getTypeHandler () ؛ if (typeHandler == NULL) {رمي executorexception جديد ("لم يتم العثور على typehandler للمعلمة"+ propertyName+ "من العبارة"+ medstatement.getId ()) ؛ } typeHandler.setParameter (ps ، i + 1 ، value ، parameTerMapping.getjdbctype ()) ؛ }}}} / ** * 根据数据库方言 , 生成特定的分页 sql * param sql * param page * regurn * / private string generatepagesql (سلسلة sql ، صفحة الصفحة) {if (page! = null && stringutils. isnotblank (لهجة)) {StringBuffer pagesql = new StringBuffer () ؛ إذا ("mysql" .equals (لهجة)) {pagesql.append (sql) ؛ pagesql.append ("limit"+page.getCurrentResult ()+"،"+page.getPagesize ()) ؛ }else if("oracle".equals(dialect)){ pageSql.append("SELECT * FROM (SELECT TMP_TB.*,ROWNUM ROW_ID FROM ("); pageSql.append(sql); pageSql.append(") AS TMP_TB حيث rownum <= ") ؛ pagesql.append (page.getCurrentResult ()+page.getPagesize ()) ؛ pagesql.append (") where row_id> ") ؛ pagesql.append (page.getCurrentResult ()) ؛ } return pagesql.toString () ؛ } آخر {return sql ؛ }} Override Public Object Plugin (Object Target) {return plugin.wrap (target ، this) ؛ } Override public void setProperties (خصائص الخصائص) {try {if (stringUtils.isempty (this.dialect = properties .getProperty ("Dialect"))) } if (stringUtils.isempty (this.pagesqlid = properties .getProperty ("pagesqlid")))) }} catch (propertyException e) {e.printStackTrace () ؛ }}}
3.Mybatis 配置文件: mybatis-config.xml
<؟ /mybatis-3-config.dtd "> <fignation> <uccedins> <plugin interceptor =" com.framework.common.page.plugin.pageplugin "> <property name =" dialect "value =" mysql " /> <property" name = "pagesqlid" value = "bypage"/> </spliexin> </plugins> </iscification>
4. 分页拦截器
package com.framework.common.page.Interceptor ؛ استيراد javax.servlet.http.httpletrequest .modelandview ؛ استيراد org.springframework.web.servlet.handler.handlerInterceptorAdapter ؛ استيراد com.framework.common.page.impl.page ؛/****14 **/مفهوم الصفوف العامة يمتد المعالجة طلب httpservletrequest ، استجابة httpservletresponse ، معالج الكائنات ، modelandview modelandview) يلقي الاستثناء {super.posthandle (طلب ، استجابة ، معالج ، نموذج) ؛ صفحة page = page.throadlocal.get () ؛ if (page! = null) {request.setAttribute ("page" ، page) ؛ } page.ThreadLocal.Remove () ؛ } Override Public Boolean Prehandle (طلب httpservletrequest ، استجابة httpservletresponse ، معالج الكائن) يلقي الاستثناء {string pagesize = request.getParameter ("pagesize") ؛ String pageno = request.getParameter ("pageno") ؛ صفحة الصفحة = صفحة جديدة () ؛ if (numberUtils.isnumber (pagesize)) {page.setPagesize (numberUtils.toint (pagesize)) ؛ } if (numberUtils.isnumber (pageno)) {page.setpageno (numberUtils.toint (pageno)) ؛ } page.ThreadLocal.set (page) ؛ العودة صحيح. }}
5.SPRING 配置
<!- ============================================== ======================= - تحميل ملف الخصائص - ======================== =========================================== -> <Context: Property -موقع placeholder = "classpath: application.properties" /> <bean id = "sqlsessionfactory"> <property name = "dataSource" ref = "datasource" /> <propert .xml "/> <property name =" mapperlocations "> <list> <value> classpath:/com/framework/mapper/**/*mapper.xml </value> </list> </spurnal> </bean > <!- ============================================= ======================= - 通过扫描的模式 , 扫描目录下所有的 dao , 根据对应的 mapper.xml 为其生成代理类 - === ================================================== =============== -> <bean id = "mapperscannerconfigurer"> <property name = "basepackage" value = "com.framework.dao" /> <property name = "processProperTyplachers" value = "true" /> <property name = "sqlsessionfactorybeanname" value = "sqlsessionfactory" /> </bean>
6.SPRINGMVC 配置拦截器
<!-分页拦截器-> <bean id = "pageInterceptor"> </bean> <!-配置拦截器-> <bean> <property name = "Interceptors"> <list> <ref bean = "pageInterceptor"/> </list> </property> </bean>