您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關SSM使用mybatis分頁插件pagehepler如何實現分頁功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
使用分頁插件的原因,簡化了sql代碼的寫法,實現較好的物理分頁,比寫一段完整的分頁sql代碼,也能減少了誤差性。
Mybatis分頁插件 demo 項目地址:free-Mybatis_PageHelper_jb51.rar
我這里使用 maven 工程實現:
1.首先導入分頁插件的依賴:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>
2.配置分頁攔截器插件
官方文檔有兩種配置分頁攔截器插件
1. 在 MyBatis 配置 xml 中配置攔截器插件
<!-- plugins在配置文件中的位置必須符合要求,否則會報錯,順序如下: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 使用下面的方式配置參數,后面會有所有的參數介紹 --> <property name="param1" value="value1"/> </plugin> </plugins>
2. 在 Spring 配置文件中配置攔截器插件
使用 spring 的屬性配置方式,可以使用 plugins 屬性像下面這樣配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注意其他配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置參數,一行配置一個 --> <value> params=value1 </value> </property> </bean> </array> </property> </bean>
這里我項目中使用的是第二種,里面的參數根據實際情況配置,也可以不配置
3.調用方法
mapper 層 sql 語句按照一般查詢寫法,不用寫分頁:
<!-- 分頁查詢 --> <select id="finds" resultType="country"> SELECT id, countryname, countrycode FROM country </select>
mapper 層接口:
/** * 查詢 * @param params * @return */ public List<Country> finds();
service 業務層接口:
/** * 分頁查詢 * @param params 分頁參數 pageNo(頁碼),pageSize(每頁查詢數目) * @return */ public PageInfo<Country> finds(Params params);
service 業務層實現類:PageHelper.startPage(1, 10);第一個參數表示第幾頁,第二個參數表示每頁顯示的記錄數
執行完 PageHelper.startPage(1, 10);語句后,緊跟著的第一個select方法會被分頁:List<Country> blogs = countryMapper.finds();
然后再用 PageInfo 對查詢結果進行包裝,PageInfo<Country> pageInfo = new PageInfo<Country>(blogs);
并將 pageInfo 返回到控制層
/** * 查詢 */ public PageInfo<Country> finds(Params params) //查詢 int pageNo = params.getPageNo(); int pageSize = params.getPageSize(); PageHelper.startPage(pageNo, pageSize); List<Country> blogs = countryMapper.finds(); //用PageInfo對結果進行包裝 PageInfo<Country> pageInfo = new PageInfo<Country>(blogs); return pageInfo; }
這里返回 pageInfo 后,在 controller 層傳入 params 分頁參數 和 解析 pageInfo:
List<Country> clist = pageInfo.getList(); 再將 clist 放進作用域,在前臺頁面利用<c:forEach></c:forEach>可循環獲得分頁數據 /** * 首頁,并且分頁查詢 * @return */ @RequestMapping("/index") public ModelAndView index(Params params){ ModelAndView modelAndView = new ModelAndView(); //一開始第一頁,查詢10條 params.setPageNo(1); params.setPageSize(10); PageInfo<Country> pageInfo = countryService.finds(params); List<Country> clist = pageInfo.getList(); //查詢數量 long couts = countryService.counts(); modelAndView.addObject("clist", clist); modelAndView.addObject("couts", couts); modelAndView.setViewName("index"); 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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.krry</groupId> <artifactId>maven_pagehepler_ora</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>maven_pagehepler_ora</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- MyBatis Generator --> <!-- Java接口和實體類 --> <targetJavaProject>${basedir}/src/main/java</targetJavaProject> <targetMapperPackage>com.isea533.mybatis.mapper</targetMapperPackage> <targetModelPackage>com.isea533.mybatis.model</targetModelPackage> <!-- XML生成路徑 --> <targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject> <targetXMLPackage>mapper</targetXMLPackage> <!-- 編譯jdk版本 --> <jdk.version>1.6</jdk.version> <!-- 依賴版本 --> <mybatis.version>3.3.1</mybatis.version> <mapper.version>3.3.6</mapper.version> <pagehelper.version>5.0.0</pagehelper.version> <mysql.version>5.1.29</mysql.version> <spring.version>4.1.2.RELEASE</spring.version> <mybatis.spring.version>1.2.4</mybatis.spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--web--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>javax.transaction-api</artifactId> <version>1.2</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <!--spring-oxm依賴--> <dependency> <groupId>org.codehaus.castor</groupId> <artifactId>castor-xml</artifactId> <version>1.3.3</version> </dependency> <!--spring-json依賴--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.4.2</version> </dependency> <!--spring-aop依賴--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.2</version> </dependency> <!--上傳文件--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- Mybatis Generator --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional>true</optional> </dependency> <!--分頁插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!--通用Mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>nexus</id> <name>local private nexus</name> <url>http://maven.oschina.net/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>sonatype-nexus-releases</id> <name>Sonatype Nexus Releases</name> <url>http://oss.sonatype.org/content/repositories/releases</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>http://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!-- 配置包掃描 --> <context:component-scan base-package="com.krry"></context:component-scan> <!-- 導入外部資源文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置druid數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <!-- 注冊事務管理器 --> <bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟事務注解驅動 --> <tx:annotation-driven transaction-manager="txMgr" /> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="typeAliasesPackage" value="com.krry.entity"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <!-- 這里的幾個配置主要演示如何使用,如果不理解,一定要去掉下面的配置 --> <property name="properties"> <value> </value> </property> </bean> </array> </property> </bean> <!-- 配置可以整體掃描Mapper的一個掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--如果有多個報路徑,用逗號分開即可 --> <property name="basePackage" value="com.krry.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 全局映射器啟用緩存 --> <setting name="cacheEnabled" value="true" /> <!-- 查詢時,關閉關聯對象即時加載以提高性能 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 設置關聯對象加載的形態,此處為按需加載字段(加載字段由SQL指 定),不會加載關聯表的所有字段,以提高性能 --> <setting name="aggressiveLazyLoading" value="false" /> <!-- 對于未知的SQL查詢,允許返回不同的結果集以達到通用的效果 --> <setting name="multipleResultSetsEnabled" value="true" /> <!-- 允許使用列標簽代替列名 --> <setting name="useColumnLabel" value="true" /> <!-- 允許使用自定義的主鍵值(比如由程序生成的UUID 32位編碼作為鍵值),數據表的PK生成策略將被覆蓋 --> <setting name="useGeneratedKeys" value="true" /> <!-- 給予被嵌套的resultMap以字段-屬性的映射支持 --> <setting name="autoMappingBehavior" value="FULL" /> <!-- 對于批量更新操作緩存SQL以提高性能 --> <setting name="defaultExecutorType" value="BATCH" /> <!-- 數據庫超過25000秒仍未響應則超時 --> <setting name="defaultStatementTimeout" value="25" /> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <typeAliases> <!--自定義user對象的別名 --> <!-- <typeAlias type="com.krry.mybatis.sysmanage.entity.User" alias="user"/> --> <!-- 批量定義別名 --> <package name="com.krry.entity" /> </typeAliases> </configuration>
springmvc.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 開啟注解模式驅動 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 開啟mvc的注解模式 user 還會注冊一個ConversionService 子類FormattingConversionServiceFactoryBean--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.krry.core.UTF8StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="prefixJson" value="false" /> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <!-- 處理responseBody 里面日期類型 --> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> <!-- 為null字段時不顯示 --> <property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> </bean> </property> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>application/x-www-form-urlencoded;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 掃包 --> <context:component-scan base-package="com.krry.controller"></context:component-scan> <!--對靜態資源文件的訪問 必須要設置,因為在springmvc的配置中配置了/匹配所有請求, 此工程所有的請求(.do ,addUser,js/image/css)都會被springmvc解析, 必須對所有的靜態資源文件進行過濾放行 --> <!-- 靜態資源過濾 下面二選一--> <!--<mvc:default-servlet-handler/> --> <mvc:resources mapping="/resource/**" location="/resource/" /> <!-- 視圖渲染 jsp/freemaker/velocity--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 制定頁面存放的路徑 --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- 文件的后綴 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
jdbc.properties 和 log4j.properties 就不用展示了,都差不多的
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>maven_pagehepler</display-name> <welcome-file-list> <welcome-file>index/index</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>maven_pagehepler</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>maven_pagehepler</servlet-name> <url-pattern>/index/index</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>maven_pagehepler</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
實體類:Country.java
package com.krry.entity; public class Country { /** * 主鍵 */ private Integer id; /** * 名稱 */ private String countryname; /** * 代碼 */ private String countrycode; public Country(Integer id, String countryname, String countrycode) { this.id = id; this.countryname = countryname; this.countrycode = countrycode; } /** * 獲取主鍵 * * @return Id - 主鍵 */ public Integer getId() { return id; } /** * 設置主鍵 * * @param id 主鍵 */ public void setId(Integer id) { this.id = id; } /** * 獲取名稱 * * @return countryname - 名稱 */ public String getCountryname() { return countryname; } /** * 設置名稱 * * @param countryname 名稱 */ public void setCountryname(String countryname) { this.countryname = countryname; } /** * 獲取代碼 * * @return countrycode - 代碼 */ public String getCountrycode() { return countrycode; } /** * 設置代碼 * * @param countrycode 代碼 */ public void setCountrycode(String countrycode) { this.countrycode = countrycode; } }
Params.java
package com.krry.entity; /** * * Params * @author krry * @version 1.0.0 * */ public class Params { private Integer pageSize = 10; private Integer pageNo = 0; public Integer getPageNo() { return pageNo; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } }
持久層:CountryMapper.java
package com.krry.mapper; import java.util.List; import com.krry.entity.Country; /** * * Mapper:操作數據庫 * @author krry * @version 1.0.0 * */ public interface CountryMapper { /** * 查詢 * @param params * @return */ public List<Country> finds(); /** * 計算 * com.krry.dao.admin * 方法名:countBlogs * @author krry * @param params * @return int * @exception * @since 1.0.0 */ public long counts(); }
CountryMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.krry.mapper.CountryMapper" > <!-- 分頁查詢 --> <select id="finds" resultType="country"> SELECT id, countryname, countrycode FROM country </select> <!-- 查詢博客數量 --> <select id="counts" resultType="long"> SELECT count(*) FROM country </select> </mapper>
業務層接口:
package com.krry.service; import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.ibatis.annotations.Param; import com.github.pagehelper.PageInfo; import com.krry.entity.Country; import com.krry.entity.Params; /** * service層:處理業務邏輯(impl里面實現) * @author asusaad * */ public interface ICountryService { /** * 分頁查詢所有博客 * @param params 分頁參數 pageNo(頁碼),pageSize(每頁查詢數目) * @return */ public PageInfo<Country> finds(Params params); /** * 計算博客數量 * @param params * @return */ public long counts(); }
業務層實現類
package com.krry.service.impl; import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.servlet.ModelAndView; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.krry.entity.Country; import com.krry.entity.Params; import com.krry.mapper.CountryMapper; import com.krry.service.ICountryService; /** * 實現service層接口 * @author asusaad * */ @Service public class CountryService implements ICountryService{ @Autowired private CountryMapper countryMapper; /** * 查詢 */ public PageInfo<Country> finds(Params params) { //查詢 int pageNo = params.getPageNo(); int pageSize = params.getPageSize(); PageHelper.startPage(pageNo, pageSize); List<Country> blogs = countryMapper.finds(); //用PageInfo對結果進行包裝 PageInfo<Country> pageInfo = new PageInfo<Country>(blogs); return pageInfo; } /** * 計算 * @param params * @return */ public long counts(){ long couts = countryMapper.counts(); return couts; } }
控制層:KrryController.java
package com.krry.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.github.pagehelper.PageInfo; import com.krry.entity.Country; import com.krry.entity.Params; import com.krry.service.ICountryService; /** * KrryController * controller層,作為請求轉發 * @author asusaad * */ @Controller //表示是多例模式,每個用戶返回的web層是不一樣的 @RequestMapping("/index") public class KrryController { @Autowired private ICountryService countryService; /** * 首頁,并且分頁查詢 * @return */ @RequestMapping("/index") public ModelAndView index(Params params){ ModelAndView modelAndView = new ModelAndView(); //一開始第一頁,查詢10條 params.setPageNo(1); params.setPageSize(10); PageInfo<Country> pageInfo = countryService.finds(params); List<Country> clist = pageInfo.getList(); //查詢數量 long couts = countryService.counts(); modelAndView.addObject("clist", clist); modelAndView.addObject("couts", couts); modelAndView.setViewName("index"); return modelAndView; } /** * ajax請求 的 分頁查詢 * @param params * @return */ @ResponseBody @RequestMapping("/loadData") public HashMap<String, Object> loadData(Params params){ HashMap<String, Object> map = new HashMap<String, Object>(); PageInfo<Country> pageInfo = countryService.finds(params); List<Country> clist = pageInfo.getList(); map.put("clist", clist); return map; } // }
關于“SSM使用mybatis分頁插件pagehepler如何實現分頁功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。