1、修改的pom.xml的文件,增加PageHelper
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
2、修改DruidDataSourceConfig.java文件,加入分页功能(我是写在代码中的,你可以采用配置文件的形式)
/**
* 配置分页
*/
@Bean
public Interceptor pageHelper() {
Interceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "false");
interceptor.setProperties(properties);
return interceptor;
}
整体代码内容如下:
package com.example.demo.common.config;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.github.pagehelper.PageInterceptor;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
/**
*
* @ClassName: DruidDataSourceConfig
* @Description: TODO(多数据源切换)
* @author willdas
* @date 2018年9月11日 下午2:50:12
*
*/
@Configuration
@MapperScan(basePackages = "com.example.demo.*.dao", sqlSessionFactoryRef = "sqlSessionFactory")
public class DruidDataSourceConfig {
/**
* 配置别名
*/
@Value("${mybatis.type-aliases-package}")
private String typeAliasesPackage;
/**
* 配置mapper的扫描,找到所有的mapper.xml映射文件
*/
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
/**
* 加载全局的配置文件
*/
@Value("${mybatis.config-location}")
private String configLocation;
/**
* 数据源1
*/
@Bean(name = "oneDataSource")
@ConfigurationProperties(prefix = "spring.datasource.one")
public DataSource dataSourceOne() {
return DruidDataSourceBuilder.create().build();
}
/**
* 数据源2
*/
@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.two")
public DataSource dataSourceTwo() {
return DruidDataSourceBuilder.create().build();
}
/**
* 数据源管理
*/
@Bean
public DataSource dynamicDataSource() throws SQLException {
DynamicDataSource dynmicDataSource = new DynamicDataSource();
Map
3、修改StudentMapper.xml文件,编写查询所有信息的方法
4、修改StudentService.java文件,新增分页方法
/**
*
* @Title: getPage
* @Description: TODO(分页查询)
* @param: @param userId
* @param: @param page
* @param: @param row
* @param: @return
* @return: PageInfo
* @throws
*/
public PageInfo getPage(Integer pageNum, Integer pageSize);
5、修改StudentServiceImpl.java文件,实现上述方法
/**
* 查询所有学生
*/
@Override
public List selectAll() {
return studentMapper.selectAll();
}
/**
* 分页查询
*/
@Override
public PageInfo getPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List lists = selectAll();
PageInfo pageInfo = new PageInfo<>(lists);
return pageInfo;
}
6、封装一个结果集Bean
/**
*
* @ClassName: ResultBean
* @Description:TODO(封装结果集)
* @author: willdas
* @date: 2018年11月2日 下午5:03:49
*
* @param
*/
public class ResultBean {
private Boolean success;
private T data;
public ResultBean(Boolean success) {
super();
this.success = success;
}
public ResultBean(Boolean success, T data) {
super();
this.success = success;
this.data = data;
}
public Boolean isSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
7、修改StudentController.java,新增分页方法
/**
*
* @Title: getPage
* @Description: TODO(分页查询)
* @param: @param pageNum
* @param: @param pageSize
* @param: @return
* @return: ResultBean>
* @throws
*/
@GetMapping("getPage")
public ResultBean> getPage(Integer pageNum,Integer pageSize){
ResultBean> resultBean = null;
PageInfo pages = studentService.getPage(pageNum, pageSize);
if (pages.getList().size() != 0) {
resultBean = new ResultBean<>(true, pages);
}else{
resultBean = new ResultBean<>(false, pages);
}
return resultBean;
}
8、修改启动类DemoApplication.java
@EnableTransactionManagement //开启事务支持
@SpringBootApplication(exclude = PageHelperAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
9、项目整体目录
10、先Maven clean,然后Maven install,最后右键DemoApplication.java,Run As启动程序
11、打开Postman,开始测试,分页查询:http://127.0.0.1/student/getPage? pageNum = 1&pageSize = 10