1、配置文件
项目在ssm框架下运行(ssm搭建步骤 ),数据库是mysql。
pom 配置依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
我在我原来的ssm的demo项目结果中添加了mybatis 文件夹。
配置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="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="Mysql"/>
</plugin>
</plugins>
</configuration>
把mybatis-config.xml文件引入到spring_mybatis.xml的数据源
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
2.java文件
UserController控制层
UserServiceImpl层具体实现
public PageInfo<User> page(int size, int index) {
if(size<=0){
size=10;
}
//设置分页参数
PageHelper.startPage(index, size);
//正常的查询
List<User> listData=userMapper.selectByPage(new User());
//自动分页
PageInfo<User> pageInfo=new PageInfo< User>(listData);
return pageInfo;
}
3测试请求
请求接口我们可以看到 装数据的List,总条目数 total,总页码 pages,当前页码 pageNum,每页大小 pageSize