1. 封装分页หน้า类
แพ็คเกจ com.framework.common.page.impl; นำเข้า java.io.serializable; นำเข้า com.framework.common.page.ipage;/** * * *//บทคัดย่อระดับสาธารณะ */ ส่วนตัวคงที่สุดท้าย Long SerialVersionUid = -3623448612757790359L; สาธารณะคงที่ int default_page_size = 20; หน้า int ส่วนตัว = default_page_size; INT INT CurrentResult; Private Int TotalPage; Private Int CurrentPage = 1; Private Int TotalCount = -1; basepage สาธารณะ (int currentPage, หน้า int, int totalCount) {this.currentPage = currentPage; this.pageize = pageSize; this.totalcount = TotalCount; } public int getTotalcount () {return this.totalcount; } โมฆะสาธารณะ Settotalcount (int totalCount) {ถ้า (TotalCount <0) {this.totalcount = 0; กลับ; } this.totalcount = TotalCount; } public basepage () {} public int getFirstResult () {return (this.currentPage - 1) * this.page.ize; } โมฆะสาธารณะ setPagesize (int pageize) {if (pagesize <0) {this.pagesize = default_page_size; กลับ; } this.pagesize = pagesize; } public int getToTalPage () {if (this.totalPage <= 0) {this.totalPage = (this.totalcount / this.page.ize); if ((this.totalpage == 0) || (this.totalcount % this.pagesize! = 0)) {this.totalpage += 1; }} ส่งคืน this.totalpage; } public int getPagesize () {return this.pagesize; } โมฆะสาธารณะ setPageno (int currentPage) {this.currentPage = currentPage; } public int getPageno () {return this.currentPage; } บูลีนสาธารณะ isFirstPage () {ส่งคืนสิ่งนี้ currentPage <= 1; } บูลีนสาธารณะ iSlastPage () {return this.currentPage> = getTotalPage (); } public int getNextPage () {if (iSlastPage ()) {return this.currentPage; } return this.currentPage + 1; } สาธารณะ int getCurrentResult () {this.currentResult = ((getPageno () - 1) * getPagesize ()); if (this.currentResult <0) {this.currentResult = 0; } return this.currentresult; } public int getPrepage () {ถ้า (isFirstPage ()) {return this.currentPage; } return this.currentPage - 1; -
แพ็คเกจ com.framework.Common.page.impl; นำเข้า java.util.list;/** * * * */หน้าคลาสสาธารณะขยาย basepage {/** * */ส่วนตัวคงที่ Long SerialVersionUid = -970177928709377315L; public Static threadlocal <Page> threadLocal = ใหม่ ThreadLocal <page> (); รายการส่วนตัว <?> ข้อมูล; หน้าสาธารณะ () {} หน้าสาธารณะ (int currentPage, หน้า int, int totalCount) {super (currentPage, pageSize, TotalCount); } หน้าสาธารณะ (int currentPage, หน้า int, int totalCount, รายการ <?> data) {super (currentPage, pageSize, TotalCount); this.data = ข้อมูล; } รายการสาธารณะ <?> getData () {ส่งคืนข้อมูล; } โมฆะสาธารณะ setData (รายการ <?> data) {this.data = data; -
2. 封装分页插件
แพ็คเกจ com.framework.common.page.plugin; นำเข้า java.sql.connection; นำเข้า java.sql.preparedStatement; นำเข้า java.sql.resultset นำเข้า java.sql.sqlexception; .properties; นำเข้า Javax.xml.bind.propertyException; นำเข้า org.apache.commons.lang3.Stringutils; นำเข้า org.apache.ibatis.executor.errorcontext; นำเข้า org.apache.ibatis.executor.executorexception; ibatis.executor.statement.basestatementhandler; นำเข้า org.apache.ibatis.executor.statement.routingstatementhandler; นำเข้า org.apache.ibatis.mapping.boundsql; Mapping.parametermapping; นำเข้า org.apache.ibatis.mapping.parametermode; นำเข้า org.apache.ibatis.plugin.interceptor; นำเข้า org.apache.ibatis.plugin.intercepts; นำเข้า org.apache.ibatis.plugin.invocation; .apache.ibatis.plugin.plugin; นำเข้า org.apache.ibatis.reflection.metaobject; นำเข้า org.apache.ibatis.reflection.property.propertytokenizer; นำเข้า org.apache.ibatis.scripting.xmltags.foreachsqlnode; .ibatis.session.configuration; นำเข้า org.apache.ibatis.type.typehandler; นำเข้า org.apache.ibatis.type.typehandlerregistry; นำเข้า com.framework.common.page.impl.page; .reflectutil;/** * * * */ @intercepts ({ @org.apache.ibatis.plugin.signature (type = org.apache.ibatis.executor.statement.statementhandler.class, method = "เตรียม", args = args = args = args = args = args = args = args = args = args {connection.class})}) คลาสสาธารณะ Pagplugin ใช้ interceptor {private String Dialect = ""; สตริงส่วนตัว pagesqlid = ""; @Override การสกัดกั้นวัตถุสาธารณะ (การเรียกร้องการเรียกใช้) โยนได้ {ถ้า (invocation.getTarget () อินสแตนซ์ของการกำหนดเส้นทาง statementhandler) {basestatementhandler delegate = (basestatementhandler) retrictutil.getValueByfieldName MappedStatement MappedStatement = (MappedStatement) ReflectUtil .getValueByfieldName (มอบหมาย, "MappedStatement"); หน้า = page.threadlocal.get (); if (page == null) {page = new page (); page.threadlocal.set (หน้า); } if (mappedStatement.getId (). Matches (".*(" + this.pagesQlid + ") $") && page.getPagesize ()> 0) {boundSQL BoundSQL = delegate.getBoundSql (); พารามิเตอร์ Object = BoundSql.GetParameterObject (); String sql = boundsql.getsql (); สตริง countsqlid = mappedStatement.getId (). replaceall (pagesqlid, "count"); MappedStatement CountMappedStatement = null; if (mappedStatement.getConfiguration (). hasstatement (countsqlid)) {countmappedStatement = mappedStatement.getConfiguration (). getMappedStatement (countsQLID); } สตริง countsql = null; if (countMappedStatement! = null) {countsql = countMappedStatement.getBoundSql (parameterObject) .getSql (); } else {countsql = "เลือกนับ (1) จาก (" + sql + ") t_count"; } int totalCount = 0; PreparedStatement CountStmt = NULL; Resultset Setterset = NULL; ลอง {Connection Connection = (Connection) Invocation.getArgs () [0]; countStmt = connection.prepareStatement (countsql); BoundSQL CountBoundSQL = New BoundSQL (MappedStatement.getConfiguration (), CountsQl, BoundSQL.GetParameterMappings (), parameterObject); SetParameters (CountStmt, MappedStatement, CountBoundSQL, ParameterObject); resultset = countStmt.executeQuery (); if (resultset.next ()) {totalCount = resultSet.getInt (1); }} catch (Exception e) {โยน e; } ในที่สุด {ลอง {ถ้า (ผลลัพธ์! = null) {resultset.close (); }} ในที่สุด {if (countStmt! = null) {countStmt.close (); }}} page.settotalcount (TotalCount); ReflectUtil.SetValueByfieldName (BoundSQL, "SQL", GeneratePagesQl (SQL, หน้า)); }} return invocation.proceed (); } / ** * 对 sql 参数 (?) 设值, 参考 org.apache.ibatis.executor.parameter.defaultparameterhandler * @param ps * @param mappedstatement * @param boundsql * @param parameterobject * @throws Sqlexception * SetParameters (PreparedStatement PS, MappedStatement MappedStatement, BoundSQL BoundSQL, พารามิเตอร์วัตถุ) พ่น SQLException {ErrorContext.Instance () กิจกรรม ("การตั้งค่าพารามิเตอร์") วัตถุ (mappedStatement.getParametermap () รายการ <ParameterMapping> ParameterMappings = boundSQL.GetParameterMappings (); if (parameterMappings! = null) {การกำหนดค่าการกำหนดค่า = mappedStatement.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) {ค่าวัตถุ; String propertyName = parameterMapping.getProperty (); PropertyTokenizer prop = new PropertyTokenizer (PropertyName); if (parameterObject == null) {value = null; } อื่นถ้า (typeHandlerRegistry.hastypehandler (parameterObject.getClass ())) {value = parameterObject; } อื่นถ้า (boundsql.hasadditionalParameter (PropertyName)) {value = boundSql.GetAdDitionAlParameter (PropertyName); } else if (propertyName.startswith (foreachsqlnode.item_prefix) && boundsql.hasadditionalparameter (prop.getName ())) {value = boundsql.getAdditionalParameter (prop.getName ()); if (value! = null) {value = configuration.newMetaObject (value) .getValue (propertyName.SubString (prop.getName (). length ())); }} else {value = metaObject == null? null: metaobject.getValue (PropertyName); } typeHandler typeHandler = parametermapping.getTypeHandler (); if (typeHandler == null) {โยน executorexception ใหม่ ("ไม่พบ typeHandler สำหรับพารามิเตอร์"+ propertyName+ "ของคำสั่ง"+ mappedStatement.getId ()); } typeHandler.SetParameter (PS, I + 1, ค่า, parameterMapping.getjdbctype ()); }}}} / ** * 根据数据库方言, 生成特定的分页 sql * @param sql * @param หน้า * @return * / สตริงส่วนตัว generatePagesQl (สตริง sql หน้าหน้า) {if (หน้า! = null && stringutils isNotBlank (ภาษาถิ่น)) {StringBuffer PagesQl = ใหม่ StringBuffer (); if ("mysql" .equals (ภาษาถิ่น)) {pagesql.append (SQL); PagesQl.Append ("Limit"+Page.getCurrentResult ()+","+page.getPagesize ()); } อื่นถ้า ("Oracle" .equals (ภาษาถิ่น)) {pagesql.append ("เลือก * จาก (เลือก tmp_tb. *, rownum row_id จาก ("); pagesql.append (sql); pagesql.append (") โดยที่ rownum <= "); pagesql.append (page.getCurrentResult ()+page.getPagesize ()); pagesql.append (") โดยที่ row_id> "); pagesql.append (page.getCurrentResult ()); } return pagesql.toString (); } else {return sql; }} @Override ปลั๊กอินวัตถุสาธารณะ (เป้าหมายวัตถุ) {return plugin.wrap (เป้าหมาย, สิ่งนี้); } @Override โมฆะสาธารณะ setProperties (คุณสมบัติคุณสมบัติ) {ลอง {ถ้า (stringUtils.isEmpty (this.dialect = properties .getProperty ("ภาษา"))) } if (stringUtils.isEmpty (this.pagesQlid = คุณสมบัติ. getProperty ("PagesQlid"))) {โยน propertyException ใหม่ ("PageSqLid คุณสมบัติไม่พบ!"); }} catch (propertyException e) {e.printStackTrace (); -
3.Mybatis 配置文件: mybatis-config.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! การกำหนดค่า doctype สาธารณะ "-// mybatis.org//dtd sql map config 3.0 // en" "http://mybatis.org/dtd /mybatis-3-config.dtd "> <การกำหนดค่า> <ปลั๊กอิน> <ปลั๊กอิน interceptor =" com.framework.common.page.plugin.pageplugin "> <property name =" Dialect "value =" mysql " /> < name = "pagesqlid" value = "bypage"/> </plugin> </plugins> </การกำหนดค่า>
4. 分页拦截器
แพ็คเกจ com.framework.common.page.interceptor; นำเข้า javax.servlet.http.httpservletrequest นำเข้า javax.servlet.http.httpservletresponse; นำเข้า org.apache.commons.lang3.math.numberutils; .ModelandView; นำเข้า org.springframework.web.servlet.handler.handlerinterceptoradapter; นำเข้า com.framework.common.page.impl.page;/****14 **/คลาสสาธารณะ คำขอ httpservletrequest, การตอบสนอง httpservletResponse, ตัวจัดการวัตถุ, modelandview modelandview) โยนข้อยกเว้น {super.posthandle (คำขอ, การตอบสนอง, handler, modelandview); หน้า = page.threadlocal.get (); if (page! = null) {request.setAttribute ("หน้า" หน้า); } page.threadlocal.remove (); } @Override Public Boolean Prehandle (คำขอ httpservletRequest, การตอบสนอง httpservletResponse, ตัวจัดการวัตถุ) โยนข้อยกเว้น {สตริง pageSize = request.getParameter ("pagesize"); สตริง 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 (หน้า); กลับมาจริง; -
5.Spring 配置
<!- ================================================== ====================== - โหลดไฟล์คุณสมบัติ - ========================== ============================================== -> <บริบท: ทรัพย์สิน -สถานที่วางตำแหน่ง = "classpath: application.properties" /> <bean id = "sqlsessionfactory"> <property name = "dataSource" ref = "dataSource" /> <property name = "configlocation" value = "classpath: mybatis-config .xml "/> <property name =" mapperlocations "> <list> <value> classpath:/com/framework/mapper/**/*mapper.xml </alue> > <!- ================================================= ======================= - 通过扫描的模式, 扫描目录下所有的 dao, 根据对应的 mapper.xml 为其生成代理类 - === ====================================================== =============== -> <bean id = "mapperscannerConfigurer"> <ชื่อคุณสมบัติ = "basepackage" value = "com.framework.dao" /> <property name = value = "true" /> <property name = "SQLSessionFactoryBeanName" value = "SQLSessionFactory" /> </ebean>
6.SpringMvc 配置拦截器
<!-分页拦截器-> <bean id = "pageInterceptor"> </ebean> <!-配置拦截器-> <bean> <property name = "interceptors"> <list> <ref bean = "PageInterceptor"/> </list> </porement> </ebean>