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



一、项目目录结构图


Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用_第1张图片





二、pom依赖





	4.0.0
	com.appleyk
	Spring-Boot-MyBatis-PageHelper
	0.0.1-SNAPSHOT
	war
	Spring-Boot 集成Mybatis+多数据源配置+Mybatis通用分页插件的使用
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.12.RELEASE
	
	
	
		1.8
		1.1.1
		1.1.1
		2.9.1
	
	
		
			org.springframework.boot
			spring-boot-starter-web
			
				
					com.fasterxml.jackson.core
					jackson-core
				
			
		
		
		
		
		
			org.springframework.boot
			spring-boot-devtools
			
			
			true
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			junit
			junit
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			${mybatis.version}
		
		
		
			com.github.pagehelper
			pagehelper-spring-boot-starter
			${pagehelper.version}
				
		
		
			mysql
			mysql-connector-java
		
		
		
		
			tk.mybatis
			mapper-spring-boot-starter
			1.1.5
		
		
		
			com.fasterxml.jackson.core
			jackson-core
			${jackson.version}
		
		
			com.fasterxml.jackson.core
			jackson-annotations
			${jackson.version}
		
		
			com.fasterxml.jackson.core
			jackson-databind
			${jackson.version}
		
	




三、属性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结果进行包装



PageInfo.java


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

import com.github.pagehelper.Page;

/**
 * 对Page结果进行包装
 * 

* 新增分页的多项属性,主要参考: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 implements Serializable { private static final long serialVersionUID = 1L; //当前页 private int pageNum; //每页的数量 //private int pageSize; //总记录数 private long total; //总页数 private int pages; //结果集 private List list; //是否为第一页 //private boolean isFirstPage = false; //是否为最后一页 //private boolean isLastPage = false; public PageInfo() { } /** * 包装Page对象 * * @param list */ public PageInfo(List 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 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 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 getList() { return list; } public void setList(List 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】



Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用_第2张图片



七、Service服务查询电影信息



Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用_第3张图片


八、测试


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


Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用_第4张图片




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


Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用_第5张图片




九、GitHub项目地址


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



Spring-Boot+Mybaits+MySql多数据源+通用分页插件PageHelper的使用_第6张图片



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

你可能感兴趣的:(mybatis,Spring-Boot,mybatis分页插件)