Mybatis之分页插件pagehelper的简单使用
最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目。
一、创建maven项目
主要创建了User类、UserMapper.xml、database.properties、mybatis-config.xml。
UserMapper.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 例如namespace="Cuiyw.MyBatis.DBMapping.UserMapper"就是Cuiyw.MyBatis.DBMapping(包名)+UserMapper(UserMapper.xml文件去除后缀) --> <mapper namespace="Cuiyw.MyBatis.DBMapping.UserMapper"> <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 resultType="Cuiyw.MyBatis.Model.User"就表示将查询结果封装成一个User类的对象返回 User类就是user表所对应的实体类 --> <!-- 根据id查询得到一个user对象 --> <select id="getUser" parameterType="int" resultType="Cuiyw.MyBatis.Model.User"> select * from user where id=#{id} </select> <select id="getUserList" resultType="Cuiyw.MyBatis.Model.User"> select * from user </select> <insert id="addUser" parameterType="Cuiyw.MyBatis.Model.User"> <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id"> SELECT LAST_INSERT_ID() </selectKey> insert into user(name,age) values (#{name},#{age}) </insert> <delete id="deleteUserById" parameterType="int"> delete from user where id=#{id} </delete> <update id="updateUser" parameterType="Cuiyw.MyBatis.Model.User"> update user set name=#{name},age=#{age} where id=#{id} </update> </mapper>
User类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package Cuiyw.MyBatis.Model; public class User { @Override public String toString() { // TODO Auto-generated method stub return "User [id=" + Id + ", name=" + Name + ", age=" + Age + "]"; } public int getId() { return Id; } public void setId(int id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public int getAge() { return Age; } public void setAge(int age) { Age = age; } private int Id; private String Name; private int Age; }
database.properties
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis
mybatis-config.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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> <properties resource="database.properties"> <property name="username" value="root"/> <property name="password" value="123456"/> </properties> <typeAliases> <typeAlias type="Cuiyw.MyBatis.Model.User" alias="User"/> </typeAliases> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 方言 --> <property name="dialect" value="mysql"/> <!-- 该参数默认为false --> <!-- 设置为true时,使用RowBounds分页会进行count查询 --> <property name="rowBoundsWithCount" value="true"/> </plugin> </plugins> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- 程序中所用到sql映射文件都在这里列出,这些映射sql都被Mybatis管理 --> <mappers> <mapper resource="Cuiyw/MyBatis/DBMapping/UserMapper.xml" /> </mappers> </configuration>
二、pagehelper插件的配置
pom.xml配置
除了上面mybatis-config.xml中配置的plugin插件com.github.pagehelper.PageHelper.这里还主要使用了org.mybatis、mysql、com.github.pagehelper、com.github.jsqlparser,分页插件的是后面两个。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Cuiyw</groupId> <artifactId>MyBatisDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MyBatisDemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
三、数据库配置
这里我事先在user表准备了一个几条数据。
四、插件的使用
PageHelper.startPage(1, 5, true)用来设置分页,PageInfo用来显示分页信息。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package Cuiyw.MyBatisDemo; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import Cuiyw.MyBatis.Model.User; public class App { public static void main( String[] args ) throws IOException { //mybatis的配置文件 String resource = "mybatis-config.xml"; //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) Reader reader = Resources.getResourceAsReader(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); // String statement="Cuiyw.MyBatis.DBMapping.UserMapper.addUser"; // User user=new User(); // user.setName("Cuiyw"); // user.setAge(27); // int result=session.insert(statement, user); // session.commit(); // session.close(); // System.out.println(user.getId()); System.out.println("------所有记录----------"); String statement="Cuiyw.MyBatis.DBMapping.UserMapper.getUserList"; List<User>users=session.selectList(statement); for (User u: users) { System.out.println(u.toString()); } System.out.println("------分页----------"); PageHelper.startPage(1, 5, true); statement="Cuiyw.MyBatis.DBMapping.UserMapper.getUserList"; users=session.selectList(statement); for (User u: users) { System.out.println(u.toString()); } PageInfo<User> pageInfo = new PageInfo<User>(users); System.out.println("数据总数:" + pageInfo.getTotal()); System.out.println("数据总页数:" + pageInfo.getPages()); System.out.println("最后一页:" + pageInfo.getLastPage()); for (User u: pageInfo.getList()) { System.out.println(u.toString()); } } }
---------------我是有底线的--------------------
作者:社会主义接班人
出处:http://www.cnblogs.com/5ishare/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
作者:社会主义接班人
出处:http://www.cnblogs.com/5ishare/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。