Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用

版权声明:有问题的请留言 喜欢的请给个赞 --------------------------------不定时会更新,因为学习,所以快乐,因为分享,所以便捷!转载请标注出处,哈哈! https://blog.csdn.net/Appleyk/article/details/80341206



一、项目目录结构图







二、pom依赖




<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.appleyk</groupId>
	<artifactId>Spring-Boot-MyBatis-PageHelper</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<description>Spring-Boot 集成Mybatis+多数据源配置+Mybatis通用分页插件的使用</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.12.RELEASE</version>
	</parent>
	<!-- 使用Java8,嘗試使用新特新【stream和lambda】 -->
	<properties>
		<java.version>1.8</java.version>
		<mybatis.version>1.1.1</mybatis.version>
		<pagehelper.version>1.1.1</pagehelper.version>
		<jackson.version>2.9.1</jackson.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>com.fasterxml.jackson.core</groupId>
					<artifactId>jackson-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 添加热部署 devtools:监听文件变动 -->
		<!-- 当Java文件改动时,Spring-boo会快速重新启动 -->
		<!-- 最简单的测试,就是随便找一个文件Ctrl+S一下,就可以看到效果 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<!-- optional=true,依赖不会传递 -->
			<!-- 本项目依赖devtools;若依赖本项目的其他项目想要使用devtools,需要重新引入 -->
			<optional>true</optional>
		</dependency>
		<!-- Spring 单元测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- JUnit单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!-- pageHelper 分页插件-->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>${pagehelper.version}</version>
		</dependency>		
		<!-- MySql驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
		<!-- mybatis通用mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>1.1.5</version>
		</dependency>
		<!-- jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>
	</dependencies>
</project>




三、属性properties文件


server.port=8085
server.session.timeout=10
server.tomcat.uri-encoding=utf8

#在application.properties文件中引入日志配置文件
#=====================================  log  =============================
logging.config=classpath:logback-boot.xml


#主数据源 == master
spring.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.master.url=jdbc\:mysql\://localhost\:3306/movie?useUnicode\=true&autoReconnect=true&useSSL=false&characterEncoding\=utf-8&useSSL=true
spring.datasource.master.username=root
spring.datasource.master.password=root

# 下面为连接池的补充设置,应用到上面所有数据源中  
# 初始化大小,最小,最大  
spring.datasource.master.initialSize=5  
spring.datasource.master.minIdle=5  
spring.datasource.master.maxActive=20  
# 配置获取连接等待超时的时间  
spring.datasource.master.maxWait=60000  
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
spring.datasource.master.filters=stat,wall,log4j  


#从数据源 == slave
spring.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.slave.url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&autoReconnect=true&useSSL=false&characterEncoding\=utf-8&useSSL=true
spring.datasource.slave.username=root
spring.datasource.slave.password=root

# 下面为连接池的补充设置,应用到上面所有数据源中  
# 初始化大小,最小,最大  
spring.datasource.slave.initialSize=5  
spring.datasource.slave.minIdle=5  
spring.datasource.slave.maxActive=20  
# 配置获取连接等待超时的时间  
spring.datasource.slave.maxWait=60000  
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
spring.datasource.slave.filters=stat,wall,log4j  



四、数据



MySql数据库执行脚本语句下载地址:https://download.csdn.net/download/appleyk/10417081 【直接执行就可以导入】




五、通用PageInfo类,对Page<E>结果进行包装



PageInfo.java


package com.appleyk.paging;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.github.pagehelper.Page;

/**
 * 对Page<E>结果进行包装
 * <p/>
 * 新增分页的多项属性,主要参考:http://bbs.csdn.net/topics/360010907
 *
 * @author liuzh/abel533/isea533
 * @version 3.3.0
 * @since 3.2.2
 * 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    //当前页
    private int pageNum;
    //每页的数量
    //private int pageSize;
    //总记录数
    private long total;
    //总页数
    private int pages;
    //结果集
    private List<T> list;
    //是否为第一页
    //private boolean isFirstPage = false;
    //是否为最后一页
    //private boolean isLastPage = false;


    public PageInfo() {
    }

    /**
     * 包装Page对象
     *
     * @param list
     */
    public PageInfo(List<T> list) {
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            //this.pageSize = page.getPageSize();

            this.pages = page.getPages();
            this.list = page;
            this.total = page.getTotal();
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            //this.pageSize = list.size();

            this.pages = 1;
            this.list = list;
            this.total = list.size();
        }
        if (list instanceof Collection) {
            //判断页面边界
            judgePageBoudary();
        }
    }
    
    public PageInfo(PageInfo<?> pageInfo, List<T> list) { 
    	this.pageNum = pageInfo.getPageNum();
    	this.pages = pageInfo.getPages() == 0 ? 1 : pageInfo.getPages();
    	this.total = pageInfo.getTotal();
    	this.list = list;
    }
    
    public PageInfo(PageInfo<?> pageInfo1, PageInfo<?> pageInfo2, List<T> list) { 
    	this.pageNum = pageInfo1.getPageNum();
    	int pages1 = pageInfo1.getPages() == 0 ? 1 : pageInfo1.getPages();
    	int pages2 = pageInfo2.getPages() == 0 ? 1 : pageInfo2.getPages();
    	this.pages = pages1 > pages2 ? pages1 : pages2;
    	this.total = pageInfo1.getTotal() + pageInfo2.getTotal();
    	this.list = list;
    }

    /**
     * 判定页面边界
     */
    private void judgePageBoudary() {
        //isFirstPage = pageNum == 1;
        //isLastPage = pageNum == pages;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("PageInfo{");
        sb.append("pageNum=").append(pageNum);
        //sb.append(", pageSize=").append(pageSize);
        sb.append(", total=").append(total);
        sb.append(", pages=").append(pages);
        sb.append(", list=").append(list);
        //sb.append(", isFirstPage=").append(isFirstPage);
        //sb.append(", isLastPage=").append(isLastPage);
        sb.append(", navigatepageNums=");
        sb.append('}');
        return sb.toString();
    }
}



六、实现电影信息查询【Controller】






七、Service服务查询电影信息





八、测试


(1)按每页10条记录分,显示第2页的记录集






(2)按每页2条记录分,显示第78页的记录集






九、GitHub项目地址


项目下载地址:Spring-Boot-MyBatis-PageHelper






有问题请扫问题码进行有偿提问!

阅读更多

扫码向博主提问

appleyk

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • SpringBoot
  • neo4j
  • Linux
  • mongodb
  • Java
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页