ไม่กี่วันที่ผ่านมาฉันแบ่งปันการใช้งานการเพจโดยการสืบค้น Pagination SQL ที่เขียนด้วยลายมือที่นี่ ทีนี้ลองมาดูโดยใช้ปลั๊กอิน MyBatis Paging PageHepler เพื่อใช้การเพจ
เหตุผลในการใช้ปลั๊กอินเพจคือการทำให้การเขียนโค้ด SQL ง่ายขึ้นและบรรลุการเพจฟิสิคัลที่ดีขึ้นซึ่งสามารถลดข้อผิดพลาดได้มากกว่าการเขียนรหัส SQL ที่สมบูรณ์
MyBatis Paging Pagin Demo Project Project ที่อยู่: Free-Mybatis_PageHelper_jb51.rar
ฉันใช้การใช้งานโครงการ Maven ที่นี่:
1. นำเข้าก่อนการพึ่งพาของปลั๊กอินเพจ:
<Ederency> <sderctId> com.github.pageHelper </groupId> <ratifactId> PageHelper </artifactId> <version> 5.0.0 </version>
2. กำหนดค่าปลั๊กอิน Paging Interceptor
ปลั๊กอิน Interceptor Page Page มีสองรายการสำหรับเอกสารอย่างเป็นทางการ
1. กำหนดค่าปลั๊กอิน interceptor ในการกำหนดค่า mybatis xml
<!- ตำแหน่งของปลั๊กอินในไฟล์การกำหนดค่าจะต้องเป็นไปตามข้อกำหนดมิฉะนั้นจะมีการรายงานข้อผิดพลาด คำสั่งซื้อมีดังนี้: คุณสมบัติ?, การตั้งค่า?, typealiases?, typeHandlers?, ObjectFactory?, ObjectWrapperFactory?, ปลั๊กอิน?, สภาพแวดล้อม? interceptor = "com.github.pagehelper.pageInterceptor"> <!-ใช้วิธีการต่อไปนี้เพื่อกำหนดค่าพารามิเตอร์และจะมีพารามิเตอร์ทั้งหมดที่แนะนำในภายหลัง-> <property name = "param1" value = "value1"/> </plugin>
2. กำหนดค่าปลั๊กอิน interceptor ในไฟล์การกำหนดค่าสปริง
การใช้วิธีการกำหนดค่าแอตทริบิวต์ของสปริงคุณสามารถใช้แอตทริบิวต์ปลั๊กอินเพื่อกำหนดค่าได้เช่นนี้:
<bean id = "sqlsessionfactory"> <!-ให้ความสนใจกับการกำหนดค่าอื่น ๆ-> <property name = "ปลั๊กอิน"> <array> <bean> <property name = "คุณสมบัติ"> <!-กำหนดค่าพารามิเตอร์โดยใช้วิธีการต่อไปนี้
ที่นี่ฉันใช้ประเภทที่สองในโครงการของฉันและพารามิเตอร์ในนั้นได้รับการกำหนดค่าตามสถานการณ์จริงหรือสามารถกำหนดค่าได้โดยไม่ต้อง
3. วิธีการโทร
คำสั่ง Mapper Layer SQL ถูกเขียนขึ้นตามวิธีการสืบค้นทั่วไปโดยไม่ต้องเขียน Paging:
<!-การสืบค้น Pagination-> <select id = "finds" resultType = "ประเทศ"> เลือก ID, CountryName, CountryCode จากประเทศ </elect>
อินเทอร์เฟซเลเยอร์ Mapper:
/*** แบบสอบถาม* @param params* @return*/รายการสาธารณะ <ประเทศ> finds ();
อินเทอร์เฟซชั้นบริการบริการ:
/ ** * การสืบค้น pagination * @param พารามิเตอร์การแบ่งหน้าพารามิเตอร์ pageno (หมายเลขหน้า), หน้า (จำนวนการสืบค้นต่อหน้า) * @return */ หน้าสาธารณะ <ประเทศ> ค้นหา (params params);
บริการเลเยอร์ธุรกิจชั้นเรียน: PageHelper.StartPage (1, 10); พารามิเตอร์แรกระบุว่าหน้าใดและพารามิเตอร์ที่สองระบุจำนวนระเบียนที่แสดงในแต่ละหน้า
หลังจากดำเนินการ pagehelper.startpage (1, 10); คำสั่งวิธีการเลือกแรกต่อไปนี้จะถูก paginated: รายการ <ประเทศ> บล็อก = countrymapper.finds ();
จากนั้นใช้ PageInfo เพื่อสรุปผลลัพธ์การสืบค้น PageInfo <ประเทศ> PageInfo = ใหม่ PageInfo <ประเทศ> (บล็อก);
และส่งคืน pageinfo ไปยังเลเยอร์ควบคุม
/*** Query*/Public PageInfo <ountains> finds (params params) // query int pageno = params.getPageno (); int pagesize = params.getPagesize (); PageHelper.StartPage (Pageno, Pagesize); รายการ <ประเทศ> บล็อก = CountryMapper.Finds (); // ห่อผลลัพธ์ด้วย PageInfo <ประเทศ> PageInfo = ใหม่ PageInfo <ประเทศ> (บล็อก); ส่งคืน pageinfo; -
หลังจากส่งคืน PageInfo ที่นี่ให้ผ่านพารามิเตอร์การปนเปื้อนของพารามิเตอร์และแยกวิเคราะห์หน้าเว็บที่เลเยอร์คอนโทรลเลอร์:
รายการ <ountry> clist = pageinfo.getList (); จากนั้นใส่ clist ลงในขอบเขตและใช้ <c: foreach> </c: foreach> เพื่อวนเพื่อรับข้อมูลเพจ/*** หน้าแรกและการสืบค้น pagination* @return*/@requestmapping ("/index") โมเดลสาธารณะ // แรกหน้าแรกคำถาม 10 params.setPageno (1); params.setPagesize (10); PageInfo <ประเทศ> PageInfo = CountrySservice.Finds (params); รายการ <ountry> clist = pageinfo.getList (); // ปริมาณของการสืบค้น long couts = countryservice.counts (); ModelAndView.AdDoBject ("CLIST", CLIST); ModelAndView.adDoBject ("Couts", Couts); ModelAndView.SetViewName ("ดัชนี"); Return ModelandView; -ข้างต้นเป็นรหัสการใช้งานทั้งหมดสำหรับหน้าคีย์ ตอนนี้ดูรหัสสำหรับการกำหนดค่าและการใช้งานทั้งหมด:
pom.xml
<Project XMLNS: XSI = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://maven.apache.org/pom/4.0.0" XSI: schemalocation = "httpmm:MANCMMMMMMMMMMMMMMMMMMMMMMMMMMMMM http://maven.apache.org/maven-v4_0_0.xsd "> <modersion> 4.0.0 </modelversion> <roupid> com.krry </groupid> <ratifactid> maven_pageplepler_ora </artifactid> <packaging> <name> maven_pagehepler_ora </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <TargetJavapRoject> $ {achedir}/src/main/java </targetJavaproject> <sourceMapperpackage> com.isea533.Mybatis.mapper </targetmapperpackage> <gartermodelpackage> com.isea533 <TargetResourcesProject> $ {achedir}/src/main/resources </targetResourcesproject> <sournedxmlpackage> Mapper </targetxmlpackage> <! <mapper.version> 3.3.6 </mapper.version> <pagehelper.version> 5.0.0 </pagehelper.version> <mysql.version> 5.1.29 </mysql.version> <pring.version> 4.1.2.release </spring.version <mybatis.spring.version> 1.2.4 </mybatis.spring.version> </premerties> <การพึ่งพา> <การพึ่งพาอาศัย> <roupid> junit </groupid> <ratifactid> junit </artifactid> <ArtIfactId> log4j </artifactId> <persion> 1.2.17 </เวอร์ชัน> </การพึ่งพาอาศัย> <!-เว็บ-> <การพึ่งพาอาศัย> <roupId> javax.servlet </groupId> <ArtIfactId> Servlet-Api </artifactid> <roupId> javax.servlet.jsp </groupid> <ratifactid> jsp-api </artifactid> <version> 2.1 </เวอร์ชัน> <Scope> ให้ </scope> </การพึ่งพา> <pertiNcy> <derction> <RoupID> javax.ws.rs </groupId> <ratifactid> javax.ws.rs-api </artifactid> <sersion> 2.0 </version> </การพึ่งพาอาศัย> <pendency> <sdernced> javax.websocket </groupid> <RoupID> Javax.Annotation </groupID> <ArtIfactId> Javax.Annotation-Api </artifactId> <persion> 1.2 </เวอร์ชั่น> </perdency> <predency> <droupId> javax.transaction </groupid> <การพึ่งพา> <roupId> org.springframework </groupId> <ratifactid> Spring-Context </artifactId> </การพึ่งพาอาศัย> <predency> <roupId> org.springframework </groupid> <ratifactid> สปริง <ArtIfactId> Spring-Oxm </artifactId> </serdency> <การพึ่งพา> <roupId> org.springframework </groupid> <ratifactid> Spring-Jdbc </artifactid> <RoupID> org.springframework </artifactId> </dercendency> <การพึ่งพาอาศัย> <roupId> org.springframework </groupid> <ratifactid> Spring-Web </artifactid> <การพึ่งพา> <roupId> org.springframework </groupId> <ratifactid> การทดสอบฤดูใบไม้ผลิ </artifactid> </การพึ่งพา> <!-การพึ่งพา Spring-oxm-> <การพึ่งพา> <roupid> org.codehaus.castor </groupid> <!-การพึ่งพา Spring-JSON-> <predency> <sdeperency> <roupId> com.fasterxml.jackson.core </groupId> <ratifactid> Jackson-Databind </artifactid> <version> 2.4.2 </version> <ArtIfactId> Jackson-Dataformat-xml </artifactid> <sersion> 2.4.2 </เวอร์ชัน> </การพึ่งพา> <!-การพึ่งพา Spring-Aop-> <การพึ่งพา> <roupId> org.aspectj </groupid> <ratifactid> <RoupID> Commons-FileUpload </groupID> <RatifactId> Commons-FileUpload </artifactId> <Sersion> 1.3.1 </เวอร์ชัน> </perentency> <!-MySQL-> <การพึ่งพา <Sersion> $ {MySQL.Version} </Sident> </การพึ่งพา> <!-DRUID-> <การพึ่งพา> <GroupId> com.alibaba </groupId> <ratifactid> druid </artifactid> <version> 1.0.11 </เวอร์ชัน> <ArtIfactId> myBatis </artifactId> <persion> $ {mybatis.version} </sent> </การพึ่งพา> <การพึ่งพา> <roupId> org.mybatis </groupId> <ArtIfactId> MyBatis-spring </artifactid> Generator-> <predency> <roupId> org.mybatis.generator </groupid> <ratifactid> mybatis-generator-core </artifactid> <erson> 1.3.2 </เวอร์ชัน> <pope> Compile </scope> <RoupID> com.github.pageHelper </groupId> <ratifactId> pageHelper </artifactid> <cersion> $ {pagehelper.version} </senture> </การพึ่งพา> <! <cersion> $ {mapper.version} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <loupId> org.apache.Commons </groupId> <ratifactid> Commons-Lang3 </artifactid> <version> 3.3.2 </เวอร์ชัน> </การพึ่งพาการพึ่งพา <ArtIfactId> Spring-Framework-BOM </artifactId> <persion> $ {Spring.version} </เวอร์ชัน> <ply> pom </type> <pope> นำเข้า </cope> </การพึ่งพา> <url> http://maven.oschina.net/content/groups/public/ </url> <leasees> <enabled> true </enabled> </releases> Nexus ปล่อย </name> <url> http://oss.sonatype.org/content/repositories/releases </url> <releases> <enabled> true </enabled> </releases> <id> sonatype-nexus-snapshots </id> <name> sonatype nexus snapshots </name> <url> http://oss.sonatype.org/content/repositories/snapshots </url> </snapshots> </repository> </repository> </repository> </repository> </build> <plugins> <souldId> org.apache.maven.plugins </groupId> <ArtIfactId> MAVEN-Compiler-plugin </source SourceTid> </การกำหนดค่า> </plugin> </plugins> </uffer> </ched>ApplicationContext.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: บริบท = "http://www.springframework.org/schema/tx" xmlns: util = "http:/ xmlns: p = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://ww.springframework การสแกนแพ็คเกจการกำหนดค่า-> <บริบท: Component-Scan base-package = "com.krry"> < /บริบท: Component-Scan> <!-นำเข้าไฟล์ทรัพยากรภายนอก-> <บริบท: สถานที่ตั้งของผู้ถือครองตำแหน่ง = "classpath: jdbc.properties" /> <! name = "driverclassName" value = "$ {db.driver}" /> <property name = "url" value = "$ {db.url}" /> <ชื่อคุณสมบัติ = "ชื่อผู้ใช้" value = "$ {db.username}" id = "txmgr"> <property name = "dataSource" ref = "dataSource"> </property> </ebean> <!-เปิดใช้งานไดรเวอร์คำอธิบายประกอบการทำธุรกรรม-> <tx: การทำธุรกรรมที่ขับเคลื่อนด้วยคำอธิบาย name = "dataSource" ref = "dataSource"/> <property name = "configlocation" value = "classpath: mybatis-config.xml"> </คุณสมบัติ> <property name = "typealiasespackage" value = "com.krry.entity"/> <property name = "plugins" หากคุณไม่เข้าใจคุณจะต้องลบการกำหนดค่าต่อไปนี้-> <property name = "คุณสมบัติ"> <ค่า> </value> </property> </ebean> </erray> </property> </ebean> <!-การกำหนดค่าสามารถสแกนสแกนได้ value = "com.krry.mapper"> </property> <property name = "SQLSessionFactoryBeanName" value = "SQLSessionFactory"> </property> </ Beans>mybatis-config.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! การกำหนดค่า doctype สาธารณะ "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd" name = "cacheenabled" value = "true" /> <!-ปิดการโหลดทันทีของวัตถุที่เกี่ยวข้องสำหรับประสิทธิภาพ-> <การตั้งค่าชื่อ = "lazyloadingenabled" value = "true" /> <!-ตั้งค่ารูปแบบการโหลดของวัตถุที่เกี่ยวข้อง นี่คือฟิลด์โหลดตามความต้องการ (ฟิลด์โหลดถูกระบุโดย SQL) ฟิลด์ทั้งหมดของตารางที่เกี่ยวข้องจะไม่ถูกโหลดเพื่อปรับปรุงประสิทธิภาพ-> <การตั้งค่าชื่อ = "aggressivelazyloading" value = "false" /> <!-สำหรับการสืบค้น SQL ที่ไม่รู้จักชุดผลลัพธ์ที่แตกต่างกันได้รับอนุญาตให้ส่งคืนเพื่อให้ได้เอฟเฟกต์ทั่วไป value = "true" /> <!-อนุญาตให้ใช้ค่าคีย์หลักที่กำหนดเอง (เช่นการเข้ารหัส UUID 32 บิตที่สร้างขึ้นโดยโปรแกรมเป็นค่าคีย์) และกลยุทธ์การสร้าง PK ของตารางข้อมูลจะถูกเขียนทับ-> <การตั้งค่าชื่อ = "useGeneratedKeys" value = "true" /> <! <!-แคช SQL สำหรับการดำเนินการอัปเดตแบบแบตช์เพื่อปรับปรุงประสิทธิภาพ-> <การตั้งค่าชื่อ = "defaultExecutortype" value = "batch" /> <!-หมดเวลาหากฐานข้อมูลไม่ตอบสนองมากกว่า 25,000 วินาที-> <การตั้งค่า = "ค่าเริ่มต้น" </settings> <sypealias> <!-นามแฝงสำหรับวัตถุผู้ใช้ที่กำหนดเอง-> <!-<typealias type = "com.krry.mybatis.sysmanage.entity.user" alias = "ผู้ใช้"/>-> <!
Springmvc.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: บริบท = "http://www.springframework.org/schema/util XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/www.springframework http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://ww.springframework http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! FormattingConversionServiceFactoryBean-> <MVC: คำอธิบายประกอบ-ไดรฟ์> <MVC: ข้อความคอนเวอร์เตอร์ register-defaults = "true"> <bean> <property name = "Supportedediatypes"> <list> <dalue> text/plain; charset = utf-8 </alues> <bean/> <bean/> <bean> <bean> <property name = "prefixjson" value = "false"/> <property name = "ObjectMapper"> <Bean> <! /> </ebean> </คุณสมบัติ> <!-ไม่แสดงเมื่อฟิลด์เป็นค่า null-> <property name = "serializationinclusion"> <value type = "com.fasterxml.jackson.annotation.jsoninclude.include"> non_null </ค่า> <value> แอปพลิเคชัน/json; charset = utf-8 </value> <value> แอปพลิเคชัน/x-www-form-urlencoded; charset = utf-8 </value> </list> </property> </ebean> base-package = "com.krry.controller"> </บริบท: ส่วนประกอบ-สแกน> <!-การเข้าถึงไฟล์ทรัพยากรคงที่จะต้องตั้งค่าเนื่องจากในการกำหนดค่า SpringMVC คำขอทั้งหมด (.do, Adduser, JS/Image/CSS) ในโครงการนี้ <!-<MVC: Default-Servlet-Handler //>-> <MVC: การแม็พทรัพยากร = "/resource/**" location = "/resource/"/> <!-ดูการเรนเดอร์ jsp/freemaker/velocity-> <bean> <! name = "suffix" value = ". jsp"> </property> </epeans>
jdbc.properties และ log4j.properties ไม่จำเป็นต้องแสดงพวกเขาเกือบจะเหมือนกัน
web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_2_2_2_5 <play-name> maven_pagehepler </display-name> <welcome-file-list> <welcome-file> ดัชนี/ดัชนี </welcome-file> </welcome-file-list> <dextonce-param> <param-name> contextconfiglocation </param-name> <param-value> <Sistener-Class> org.springframework.web.context.contextloaderListener </Listener-Class> </Listener> <Sistener> <Sistener-Lass> org.springFramework.web.util.IntrospectorCleanupListener <Tilter-class> org.springframework.web.filter.characterencodingFilter </filter-class> <init-Param> <param-Name> การเข้ารหัส </param-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name> maven_pagehepler </servlet-name> <servlet-lass> org.springframework.web.servlet.dispatcherservlet <param-value> classpath: springmvc.xml </param-value> </itrin-param> </servlet> <servlet-mapping> <servlet-name> maven_pagehepler </servlet-name> <url-pattern>/index/index/index <servlet-name> maven_pagehepler </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </eb-app>
ระดับเอนทิตี: Country.java
แพ็คเกจ com.krry.entity; ประเทศระดับสาธารณะ { / *** คีย์หลัก* / รหัสจำนวนเต็มส่วนตัว; / *** ชื่อ*/ สตริงส่วนตัวชื่อ countryName; / *** รหัส*/ Private String CountryCode; ประเทศสาธารณะ (ID จำนวนเต็ม, String CountryName, String CountryCode) {this.id = id; this.countryName = countryName; this.countryCode = CountryCode; } / *** รับคีย์หลัก** @return id - คีย์หลัก* / จำนวนเต็มสาธารณะ getId () {return id; } / *** ตั้งคีย์หลัก** @param ID หลักคีย์* / โมฆะสาธารณะ setId (ID จำนวนเต็ม) {this.id = id; } / *** รับชื่อ** @return countryName - ชื่อ* / สตริงสาธารณะ getCountryName () {return countryName; } / *** ตั้งชื่อ** @param ชื่อ countryName* / โมฆะสาธารณะ setCountryName (String countryName) {this.countryName = countryName; } / *** รับรหัส** @return CountryCode - รหัส* / สตริงสาธารณะ getCountryCode () {ส่งคืน CountryCode; } / *** ตั้งรหัส** @param รหัส CountryCode* / โมฆะสาธารณะ setCountryCode (String CountryCode) {this.countryCode = CountryCode; -params.java
แพ็คเกจ com.krry.entity;/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * pageno จำนวนเต็มส่วนตัว = 0; จำนวนเต็มสาธารณะ getPageno () {return pageno; } โมฆะสาธารณะ setpageno (จำนวนเต็ม pageno) {this.pageno = pageno; } จำนวนเต็มสาธารณะ getPagesize () {กลับ pageSize; } โมฆะสาธารณะ setPagesize (จำนวนเต็มหน้า) {this.page.ize = pageSize; - Layer Stervince: Countrymapper.java
แพ็คเกจ com.krry.mapper; นำเข้า java.util.list; นำเข้า com.krry.entity.country;/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / ** * คำนวณ * com.krry.dao.admin * ชื่อวิธี: countblogs * @author krry * @param params * @return int * @exception * @since 1.0.0 */ นับยาวสาธารณะ (); -
countrymapper.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" namespace = "com.krry.mapper.countrymapper"> <!-การสืบค้นการแบ่งหน้า-> <select id = "finds" resultType = "ประเทศ"> id, id, countryName, countrycode จากประเทศ </select> <!
อินเทอร์เฟซเลเยอร์ธุรกิจ:
แพ็คเกจ com.krry.service; นำเข้า java.util.hashmap; นำเข้า java.util.list; นำเข้า javax.servlet.http.httpservletrequest นำเข้า org.apache.ibatis.annotations.param; com.krry.entity.params;/** * เลเยอร์บริการ: จัดการตรรกะทางธุรกิจ (นำไปใช้ใน impl) * @author asusaad * */อินเตอร์เฟสสาธารณะ icountryservice {/** * แบบสอบถาม pagination สำหรับบล็อกทั้งหมด * @param params parameters pageno (จำนวนหน้า) ค้นหา (params params); / ** * คำนวณจำนวนบล็อก * @param params * @return */ การนับความยาวสาธารณะ (); - ชั้นเรียนการใช้งานเลเยอร์ธุรกิจ
แพ็คเกจ com.krry.service.impl; นำเข้า java.util.hashmap; นำเข้า java.util.list; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.service; com.github.pageHelper.pageHelper; นำเข้า com.github.pageHelper.pageInfo; นำเข้า com.krry.entity.country; นำเข้า com.krry.entity.params; นำเข้า com.krry.mapper.countrymapper; Asusaad * */ @ServicePublic Class Countryservice ใช้ icountryservice {@autowired Private Countrymapper CountryMapper; /*** Query*/Public PageInfo <ountains> finds (params params) {// query int pageno = params.getPageno (); int pagesize = params.getPagesize (); PageHelper.StartPage (Pageno, Pagesize); รายการ <ประเทศ> บล็อก = CountryMapper.Finds (); // ห่อผลลัพธ์ด้วย PageInfo PageInfo <ประเทศ> PageInfo = ใหม่ PageInfo <ประเทศ> (บล็อก); ส่งคืน pageinfo; } / ** * การคำนวณ * @param params * @return * / การนับความยาวสาธารณะ () {long couts = countrymapper.counts (); กลับ couts; - เลเยอร์ควบคุม: krrycontroller.java
แพ็คเกจ com.krry.controller; นำเข้า java.util.arraylist; นำเข้า java.util.hashmap; นำเข้า java.util.list; นำเข้า org.junit.test; นำเข้า org.springframework.beans.factory.annotation. org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า org.springframework.web.servlet.modelandview; com.krry.entity.params; นำเข้า com.krry.service.icocom. ** * krrycontroller * เลเยอร์คอนโทรลเลอร์, ส่งต่อเป็นคำขอ * @author asusaad * */ @คอนโทรลเลอร์ // หมายความว่ามันเป็นรูปแบบหลายกรณี Icountryservice Countryservice; /*** โฮมเพจและการสืบค้นการแบ่งหน้า* @return*/@requestmapping ("/index") ดัชนีสาธารณะและวิววิว (params params) {modelandView modelandView = new ModelandView (); // แรกหน้าแรกคำถาม 10 params.setPageno (1); params.setPagesize (10); PageInfo <ประเทศ> PageInfo = CountrySservice.Finds (params); รายการ <ountry> clist = pageinfo.getList (); // ปริมาณของการสืบค้น long couts = countryservice.counts (); ModelAndView.AdDoBject ("CLIST", CLIST); ModelAndView.adDoBject ("Couts", Couts); ModelAndView.SetViewName ("ดัชนี"); Return ModelandView; }/** * การสืบค้น pagination สำหรับคำขอ ajax * @param params * @return */@ResponseBody @RequestMapping ("/loadData") Public HashMap <String, Object> loadData (พารามิเตอร์พารามิเตอร์) PageInfo <ประเทศ> PageInfo = CountrySservice.Finds (params); รายการ <ountry> clist = pageinfo.getList (); map.put ("clist", clist); แผนที่กลับ; -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น