ssm框架+PageHelper插件实现分页查询功能
程序员文章站
2022-04-05 12:31:35
通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询。源码:https://gitee.com/smfx1314/pagehelper看一下项目结构:首先创建一个maven...
通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询。
源码:https://gitee.com/smfx1314/pagehelper
看一下项目结构:
首先创建一个maven工程,pom中引入相关jar包
<dependencies> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aspects</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-expression</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.3.2.release</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.0</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.4</version> </dependency> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>2.4</version> </dependency> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version>3.4</version> </dependency> <dependency> <groupid>commons-logging</groupid> <artifactid>commons-logging</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> <version>2.9.1</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.7.21</version> <scope>test</scope> </dependency> <dependency> <groupid>aopalliance</groupid> <artifactid>aopalliance</artifactid> <version>1.0</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.38</version> </dependency> <dependency> <groupid>com.mchange</groupid> <artifactid>c3p0</artifactid> <version>0.9.5.2</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>javax.servlet.jsp-api</artifactid> <version>2.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>taglibs</groupid> <artifactid>standard</artifactid> <version>1.1.2</version> </dependency> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>5.1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>3.6.1</version> <configuration> <encoding>utf-8</encoding> </configuration> </plugin> <plugin> <groupid>org.apache.tomcat.maven</groupid> <artifactid>tomcat7-maven-plugin</artifactid> <version>2.2</version> <configuration> <uriencoding>utf-8</uriencoding> </configuration> </plugin> </plugins> </build> </project>
pom.xml中引入page分页的jar包
<dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>5.1.2</version> </dependency>
jar包这里就引入完了。下面引入配置文件
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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描注解的包 --> <context:component-scan base-package="com.jiangfx.service"/> <!-- 配置数据库 --> <!-- 加载配置文件 --> <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"> <property name="driverclass" value="com.mysql.jdbc.driver"></property> <property name="jdbcurl" value="jdbc:mysql://localhost:3306/pagehelper"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean> <!-- 配置sqlsessionfactory, 并将数据源注入 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <!-- 引入数据源 --> <property name="datasource" ref="datasource"></property> <!--载入mybatis配置文件--> <property name="configlocation" value="classpath:mybatis-config.xml"/> <!--载入配置mapper映射的xml--> <property name="mapperlocations" value="classpath:com/jiangfx/mapper/*.xml"/> </bean> <!-- 配置映射接口 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.jiangfx.mapper"/> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> </bean> <!-- 配置声明式事务 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"></property> </bean> <tx:annotation-driven transaction-manager="transactionmanager"/> <!-- 配置aop --> </beans>
springmvc.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描注解的包 --> <context:component-scan base-package="com.jiangfx.controller"/> <!-- 开启注解 --> <mvc:annotation-driven/> <!--静态资源访问--> <mvc:default-servlet-handler/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/"></property> <property name="suffix" value=".jsp"></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> <!-- 引入 pagehelper插件 --> <!--注意这里要写成pageinterceptor, 5.0之前的版本都是写pagehelper, 5.0之后要换成pageinterceptor--> <plugins> <plugin interceptor="com.github.pagehelper.pageinterceptor"> <!--reasonable:分页合理化参数,默认值为false。 当该参数设置为 true 时,pagenum<=0 时会查询第一页, pagenum>pages(超过总数时),会查询最后一页。 默认false 时,直接根据参数进行查询。--> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration>
以上你也可以直接配置的applicationcontext中。
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1"> <display-name>pagehelper</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingfilter</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>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
controller:
package com.jiangfx.controller; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import com.github.pagehelper.pagehelper; import com.github.pagehelper.pageinfo; import com.jiangfx.entity.user; import com.jiangfx.service.userservice; @controller public class usercontroller { @autowired private userservice pageservice; /** * 分页查询 */ @requestmapping(value="/list",method=requestmethod.get) public string pagelist(modelmap map,@requestparam(defaultvalue="1",required=true,value="pageno") integer pageno){ integer pagesize=4;//每页显示记录数 //分页查询 pagehelper.startpage(pageno, pagesize); list<user> userlist = pageservice.list();//获取所有用户信息 pageinfo<user> pageinfo=new pageinfo<user>(userlist); map.addattribute("pageinfo", pageinfo); return "list"; } }
serviceimpl:接口自己定义,这里就不贴了
package com.jiangfx.service.impl; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.jiangfx.entity.user; import com.jiangfx.mapper.usermapper; import com.jiangfx.service.userservice; @service public class userserviceimpl implements userservice { @autowired private usermapper usermapper; /** * 查询所有用户 * @return */ @override public list<user> list() { return usermapper.getalluser(); } }
entity:
package com.jiangfx.entity; public class user { private integer id; private string username; private string sex; private string city; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } @override public string tostring() { return "user [id=" + id + ", username=" + username + ", sex=" + sex + ", city=" + city + "]"; } }
mapper
package com.jiangfx.mapper; import java.util.list; import com.jiangfx.entity.user; public interface usermapper { //查询所有用户 list<user> getalluser(); }
mapper.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.jiangfx.mapper.usermapper"> <!-- 查询博客 --> <select id="getalluser" resulttype="com.jiangfx.entity.user"> select * from user </select> </mapper>
下面是jsp
index.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <a href="list" rel="external nofollow" >分页查询</a> </body> </html>
返回list
list.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <center> <table width="200" border="1"> <tr> <th scope="col">id</th> <th scope="col">姓名</th> <th scope="col">性别</th> <th scope="col">城市</th> </tr> <c:foreach items="${pageinfo.list}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.sex}</td> <td>${user.city}</td> </tr> </c:foreach> </table> <p>当前 ${pageinfo.pagenum }页,总${pageinfo.pages } 页,总 ${pageinfo.total } 条记录</div></p> <a href="list?pageno=${pageinfo.firstpage}" rel="external nofollow" >第一页</a> <c:if test="${pageinfo.haspreviouspage }"> <a href="list?pageno=${pageinfo.pagenum-1}" rel="external nofollow" >上一页</a> </c:if> <c:if test="${pageinfo.hasnextpage }"> <a href="list?pageno=${pageinfo.pagenum+1}" rel="external nofollow" >下一页</a> </c:if> <a href="list?pageno=${pageinfo.lastpage}" rel="external nofollow" >最后页</a> </center> </body> </html>
访问localhost:8080/pagehelper
点击分页查询结果:
以上就是ssm框架+pagehelper实现分页查询功能的详细内容,更多关于ssm pagehelper分页查询的资料请关注其它相关文章!
上一篇: 鞭炮笋煮多久可以吃?鞭炮笋一般怎么吃?
推荐阅读