springboot mybatis项目整合加入PageHelper分页插件
        【摘要】 
                    springboot整合mybaits我就不介绍了,网上大把教程,这里直接讲解整合PageHelper 分页 
  
1、pom.xml加入依赖 
<!-- mybatis分页插件pagehelp 数据驱动 --><dependency>	<groupId>com.github.pagehe...
    
    
    
    springboot整合mybaits我就不介绍了,网上大把教程,这里直接讲解整合PageHelper 分页
1、pom.xml加入依赖
  
   - 
    
     
    
    
     
      <!-- mybatis分页插件pagehelp 数据驱动 -->
     
    
 
   - 
    
     
    
    
     
      <dependency>
     
    
 
   - 
    
     
    
    
     	<groupId>com.github.pagehelper</groupId>
     
    
 
   - 
    
     
    
    
     	<artifactId>pagehelper</artifactId>
     
    
 
   - 
    
     
    
    
     	<version>4.1.6</version>
     
    
 
   - 
    
     
    
    
     
      </dependency>
     
    
 
  
 
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<Article> objects = PageHelper.startPage(1, 2);
     
    
 
   - 
    
     
    
    
     
      		List<Article> 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
  
   - 
    
     
    
    
     
      <?xml version="1.0" encoding="UTF-8"?>
     
    
 
   - 
    
     
    
    
     
      <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>com.spring.boot</groupId>
     
    
 
   - 
    
     
    
    
     	<artifactId>spriongboot-mybatis</artifactId>
     
    
 
   - 
    
     
    
    
     	<version>0.0.1-SNAPSHOT</version>
     
    
 
   - 
    
     
    
    
     	<packaging>jar</packaging>
     
    
 
   - 
    
     
    
    
     	
     
    
 
   - 
    
     
    
    
     	<name>love-qdf</name>
     
    
 
   - 
    
     
    
    
     	<description>Demo project for Spring Boot</description>
     
    
 
   - 
    
     
    
    
     	
     
    
 
   - 
    
     
    
    
     	<parent>
     
    
 
   - 
    
     
    
    
     		<groupId>org.springframework.boot</groupId>
     
    
 
   - 
    
     
    
    
     		<artifactId>spring-boot-starter-parent</artifactId>
     
    
 
   - 
    
     
    
    
     		<version>2.1.1.RELEASE</version>
     
    
 
   - 
    
     
    
    
     		<relativePath/> <!-- lookup parent from repository -->
     
    
 
   - 
    
     
    
    
     	</parent>
     
    
 
   - 
    
     
    
    
     	
     
    
 
   - 
    
     
    
    
     	<properties>
     
    
 
   - 
    
     
    
    
     		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     
    
 
   - 
    
     
    
    
     		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     
    
 
   - 
    
     
    
    
     		<java.version>1.8</java.version>
     
    
 
   - 
    
     
    
    
     	</properties>
     
    
 
   - 
    
     
    
    
     	
     
    
 
   - 
    
     
    
    
     	<dependencies>
     
    
 
   - 
    
     
    
    
     		<dependency>
     
    
 
   - 
    
     
    
    
     			<groupId>org.springframework.boot</groupId>
     
    
 
   - 
    
     
    
    
     			<artifactId>spring-boot-starter-jdbc</artifactId>
     
    
 
   - 
    
     
    
    
     		</dependency>
     
    
 
   - 
    
     
    
    
     		<dependency>
     
    
 
   - 
    
     
    
    
     			<groupId>org.springframework.boot</groupId>
     
    
 
   - 
    
     
    
    
     			<artifactId>spring-boot-starter-web</artifactId>
     
    
 
   - 
    
     
    
    
     		</dependency>
     
    
 
   - 
    
     
    
    
     		<dependency>
     
    
 
   - 
    
     
    
    
     			<groupId>org.mybatis.spring.boot</groupId>
     
    
 
   - 
    
     
    
    
     			<artifactId>mybatis-spring-boot-starter</artifactId>
     
    
 
   - 
    
     
    
    
     			<version>1.3.2</version>
     
    
 
   - 
    
     
    
    
     		</dependency>
     
    
 
   - 
    
     
    
    
     		
     
    
 
   - 
    
     
    
    
     		<dependency>
     
    
 
   - 
    
     
    
    
     			<groupId>mysql</groupId>
     
    
 
   - 
    
     
    
    
     			<artifactId>mysql-connector-java</artifactId>
     
    
 
   - 
    
     
    
    
     			<scope>runtime</scope>
     
    
 
   - 
    
     
    
    
     		</dependency>
     
    
 
   - 
    
     
    
    
     		<dependency>
     
    
 
   - 
    
     
    
    
     			<groupId>org.springframework.boot</groupId>
     
    
 
   - 
    
     
    
    
     			<artifactId>spring-boot-starter-test</artifactId>
     
    
 
   - 
    
     
    
    
     			<scope>test</scope>
     
    
 
   - 
    
     
    
    
     		</dependency>
     
    
 
   - 
    
     
    
    
     		<!-- mybatis分页插件pagehelp 数据驱动 -->
     
    
 
   - 
    
     
    
    
     		<dependency>
     
    
 
   - 
    
     
    
    
     			<groupId>com.github.pagehelper</groupId>
     
    
 
   - 
    
     
    
    
     			<artifactId>pagehelper</artifactId>
     
    
 
   - 
    
     
    
    
     			<version>4.1.6</version>
     
    
 
   - 
    
     
    
    
     		</dependency>
     
    
 
   - 
    
     
    
    
     	</dependencies>
     
    
 
   - 
    
     
    
    
     	
     
    
 
   - 
    
     
    
    
     	<build>
     
    
 
   - 
    
     
    
    
     		<finalName>love-qdf</finalName>
     
    
 
   - 
    
     
    
    
     		<plugins>
     
    
 
   - 
    
     
    
    
     			<plugin>
     
    
 
   - 
    
     
    
    
     				<groupId>org.springframework.boot</groupId>
     
    
 
   - 
    
     
    
    
     				<artifactId>spring-boot-maven-plugin</artifactId>
     
    
 
   - 
    
     
    
    
     			</plugin>
     
    
 
   - 
    
     
    
    
     		</plugins>
     
    
 
   - 
    
     
    
    
     	</build>
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      </project>
     
    
 
  
 
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
     
    
 
  
 
文章来源: yexindong.blog.csdn.net,作者:java叶新东老师,版权归原作者所有,如需转载,请联系作者。
原文链接:yexindong.blog.csdn.net/article/details/84793738
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)