前段时间写了一篇基于mybatis实现的多数据源博客。感觉不是很好,这次打算加入git,来搭建一个基于Mybatis-Plus的多数据源项目
Mybatis-Plus就是香
前言:该项目分为master数据源与local数据源。假定master数据源为线上数据库,local为本地数据库。后续我们将通过xxl-job的方式,将线上(master)中的数据同步到本地(local)中
项目git地址 抽时间把项目提交到git仓库,方便大家直接克隆
sql文件已置于项目中,数据库使用的mysql
创建项目
我这里使用的idea,正常创建spring boot项目就行了。
sql
-- 创建master数据库
CREATE DATAbase db_master;
USE db_master;
-- 用户表
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键id',
username VARCHAr(20) NOT NULL COMMENT '用户名',
pwd VARCHAr(50) NOT NULL COMMENT '密码',
phone CHAr(11) NOT NULL COMMENT '手机号',
email VARCHAr(50) COMMENT '邮箱',
gender CHAr(1) COMMENT '性别 1-->男 0-->女',
age INT COMMENT '年龄',
created_time TIMESTAMP NULL DEFAULT NULL COMMENT '创建时间',
updated_time TIMESTAMP NULL DEFAULT NULL COMMENT '修改时间',
deleted_time TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
type INT DEFAULT 1 COMMENT '状态 1-->启用 2-->禁用 3-->软删除'
)ENGINE=INNODB;
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('supperAdmin','admin123','13000000000','super@admin.com',1,20);
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('admin','admin','13200840033','admin@163.com',0,14);
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('wanggang','wanggang','13880443322','wang@gang.cn',1,36);
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('xiaobao','xiaobao','18736450102','xiao@bao.com',0,22);
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('zhaoxing','zhaoxing','18966004400','zhao@xing.com',1,29);
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('chenyun','chenyun','13987613540','chen@yun.com',1,25);
INSERT INTO users(username,pwd,phone,email,gender,age) VALUES('ouyangruixue','ouyangruixue','15344773322','ouyang@163.com',0,24);
-- 创建local数据库
CREATE DATAbase db_local;
USE db_local;
-- 本地库用户表
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键id',
username VARCHAr(20) NOT NULL COMMENT '用户名',
pwd VARCHAr(50) NOT NULL COMMENT '密码',
phone CHAr(11) NOT NULL COMMENT '手机号',
email VARCHAr(50) COMMENT '邮箱',
gender CHAr(1) COMMENT '性别 1-->男 0-->女',
age INT COMMENT '年龄',
created_time TIMESTAMP NULL DEFAULT NULL COMMENT '创建时间',
updated_time TIMESTAMP NULL DEFAULT NULL COMMENT '修改时间',
deleted_time TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
type INT DEFAULT 1 COMMENT '状态 1-->启用 2-->禁用 3-->软删除'
)ENGINE=INNODB;
-- 本地库测试表
CREATE TABLE local_test(
id INT PRIMARY KEY AUTO_INCREMENT,
str VARCHAr(20)
)ENGINE=INNODB;
INSERT INTO local_test(str) VALUES ('Hello');
INSERT INTO local_test(str) VALUES ('World');
INSERT INTO local_test(str) VALUES ('Mysql');
INSERT INTO local_test(str) VALUES ('^.^');
pom文件
目前只引入了lombok、mybatis-plus、mysql相关的依赖
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.projectlombok lomboktrue com.baomidou mybatis-plus-boot-starter3.2.0 mysql mysql-connector-javaruntime
application.yml
properties删除,yml创建。其实无所谓yml还是properties
server:
port: 8001
spring:
datasource:
master:
url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
local:
url: jdbc:mysql://localhost:3306/db_local?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:mappers
@Bean(name = "localSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 设置Mybatis全局配置路径
bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/local
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/master/*.xml"));
return bean.getObject();
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
部分代码
Users实体类
package com.demo.model;
import lombok.Data;
import java.util.Date;
@Data
public class Users {
private Integer id;
private String username;
private String pwd;
private String phone;
private String email;
private Integer gender;
private Integer age;
private Date created_time;
private Date updated_time;
private Date deleted_time;
private Integer type;
}
UsersMapper
package com.demo.dao.master; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.demo.model.Users; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UsersMapper extends baseMapper{ }
UsersService
package com.demo.service.master; import com.baomidou.mybatisplus.extension.service.IService; import com.demo.model.Users; public interface UsersService extends IService{ }
UsersServiceImpl
package com.demo.service.master.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.demo.dao.master.UsersMapper; import com.demo.model.Users; import com.demo.service.master.UsersService; import org.springframework.stereotype.Service; @Service public class UsersServiceImpl extends ServiceImplimplements UsersService { }
UsersController
package com.demo.controller.master;
import com.demo.model.Users;
import com.demo.service.master.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/master")
public class UsersController {
@Autowired
private UsersService userssService;
@RequestMapping("/users")
public List list() {
return userssService.list();
}
}
最终的项目结构
运行结果
至此Spring Boot + Mybatis-Plus已经完成了多数据源的实现。
最后再写一点我遇到的坑吧
1、master与local中不要出现同名的文件
2、数据源配置中SqlSessionFactoryBean替换为MybatisSqlSessionFactoryBean
下一篇将会围绕xxl-job任务调度中心,介绍如何通过xxl-job实现本地库与线上库的定时同步
到此这篇关于Spring Boot + Mybatis-Plus实现多数据源的文章就介绍到这了,更多相关Spring Boot Mybatis-Plus多数据源内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



