SSM使用mybatis分页插件pagehepler实现分页示例
前几天在这里分享了手写 sql 分页查询实现分页,现在来看看使用 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; } // }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java中jvm逃逸问题分析