springboot mybatis项目整合加入PageHelper分页插件

springboot整合mybaits我就不介绍了,网上大把教程,这里直接讲解整合PageHelper 分页

 

1、pom.xml加入依赖



	com.github.pagehelper
	pagehelper
	4.1.6

2、在入口类使用Configuration注册分页插件

	/*
	 * 注册MyBatis分页插件PageHelper
	 */
	@Configuration
	public class MybatisConf {
		@Bean
		public PageHelper pageHelper() {
			System.out.println("MyBatisConfiguration.pageHelper()");
			PageHelper pageHelper = new PageHelper();
			Properties p = new Properties();
			p.setProperty("offsetAsPageNum", "true");
			p.setProperty("rowBoundsWithCount", "true");
			p.setProperty("reasonable", "true");
			pageHelper.setProperties(p);
			return pageHelper;
		}
	}

3、配置文件 在application.yml 加入以下内容,其实不加也可以,默认的配置就够我们用了

#pagehelper分页配置 
pagehelper: 
  helperDialect: mysql 
  reasonable: true 
  supportMethodsArguments: true 
  params: count=countSql

4、使用方法,在查询列表之前先new一个分页出来

public ResponseCommand list() {
		
		Article searchModel = new Article();
		//分页插件-一定要在查询方法之前加上,参数(1,2)表示查询第1页,每页2条记录
		Page
objects = PageHelper.startPage(1, 2); List
list = articleMapper.queryModelList(searchModel); //查询完成之后就可以使用对象获取总数 System.out.println("总记录数:"+objects.getTotal()); System.out.println("总页数:"+objects.getPageNum()); System.out.println("每页记录数:"+objects.getPageSize()); System.out.println("当前页记录数:"+objects.size()); System.out.println("总页数:"+objects.getPages()); long total = objects.getTotal(); ResponseCommand command = new ResponseCommand(); command.setStatus(true); command.setResult(list); return command; }

是不是很简单呢?

#################以下为全部代码,大神请忽略,新手可以看下###################

pom.xml



	4.0.0
	
	com.spring.boot
	spriongboot-mybatis
	0.0.1-SNAPSHOT
	jar
	
	love-qdf
	Demo project for Spring Boot
	
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	
	
		UTF-8
		UTF-8
		1.8
	
	
	
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			com.github.pagehelper
			pagehelper
			4.1.6
		
	
	
	
		love-qdf
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



springboot 入口类

package com.spring.boot;

import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@SpringBootApplication
@MapperScan("com.data.dao")
public class SpriongbootMybatisApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpriongbootMybatisApplication.class, args);
	}
	
	
	
	/*
	 * 注册MyBatis分页插件PageHelper
	 */
	@Configuration
	public class MybatisConf {
		@Bean
		public PageHelper pageHelper() {
			System.out.println("MyBatisConfiguration.pageHelper()");
			PageHelper pageHelper = new PageHelper();
			Properties p = new Properties();
			p.setProperty("offsetAsPageNum", "true");
			p.setProperty("rowBoundsWithCount", "true");
			p.setProperty("reasonable", "true");
			pageHelper.setProperties(p);
			return pageHelper;
		}
	}
}

application.yml配置文件.

server:
    port: 8081


spring:
  datasource:
    name: SpringBoot-MyBatis
    url: jdbc:mysql://192.168.1.101:3306/article_dev?useUnicode=true
    username: root
    password: 123
#    type: com.alibaba.druid.pool.DruidDataSource # 使用druid 数据源
    driver-class-name: com.mysql.jdbc.Driver
#    dbcp2:
#      min-idle: 1
#      max-idle: 2
#      initial-size: 1
#      time-between-eviction-runs-millis: 3000
#      min-evictable-idle-time-millis: 300000
#      validation-query: SELECT "ZTM" FROM DUAL
#      test-while-idle: true
#      test-on-borrow: false
#      test-on-return: false

mybatis:
  mapper-locations: classpath:sqlMapper/*Mapper.xml
  type-aliases-package: com.data.model

#pagehelper分页配置 
pagehelper: 
  helperDialect: mysql 
  reasonable: true 
  supportMethodsArguments: true 
  params: count=countSql

 

你可能感兴趣的:(PageHelper,PageHelper)