目录
表结构及数据
-- 表结构
CREATE TABLE IF NOT EXISTS user
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`username` varchar(30) DEFAULT NULL COMMENT '姓名',
`password` varchar(50) DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`),
UNIQUE KEY `user_UN` (`username`)
);
-- h2数据库
INSERT INTO user (id, username, password) VALUES (1, 'adminH2', 'xxx'), (2, 'testH2', 'xxx');
-- MySQL数据库
INSERT INTO user (id, username, password) VALUES (1, 'admin', 'xxx'), (2, 'test', 'xxx');
依赖
<!-- H2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency>
配置
application.properties文件
spring.datasource.data=classpath:db/data-h2.sql
spring.datasource.schema=classpath:db/schema-h2.sql
#设置默认的数据源或者数据源组,默认值即为master
spring.datasource.dynamic.primary=h2
#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
spring.datasource.dynamic.strict=false
#h2数据源,这里的h2可以根据需要自定义命名
spring.datasource.dynamic.datasource.h2.url=jdbc:h2:file:./db/test
spring.datasource.dynamic.datasource.h2.username=root
spring.datasource.dynamic.datasource.h2.password=xxx
spring.datasource.dynamic.datasource.h2.driver-class-name=org.h2.Driver
#mysql数据源,这里的mysql可以根据需要自定义命名
spring.datasource.dynamic.datasource.mysql.url=jdbc:mysql://192.168.111.222:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&autoReconnect=true&useSSL=false
spring.datasource.dynamic.datasource.mysql.username=root
spring.datasource.dynamic.datasource.mysql.password=xxx
spring.datasource.dynamic.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
启动类添加@Mapper注解
在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 文件夹:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.qq.multidatasource")
public class MultiDatasourceApplication {
public static void main(String[] args) {
SpringApplication.run(MultiDatasourceApplication.class, args);
}
}
编码
实体类
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId
private Long id;
private String username;
private String password;
}
Mapper类
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qq.multidatasource.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
Service类
H2数据库操作
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qq.multidatasource.entity.User;
import com.qq.multidatasource.mapper.UserMapper;
import org.springframework.stereotype.Service;
//使用 @DS 切换数据源
//配置文件中设置h2是默认数据库,故这里可以不加@DS
//@DS("h2")
@Service
public class H2UserService extends ServiceImpl<UserMapper, User> {
}
MySQL数据库操作
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qq.multidatasource.entity.User;
import com.qq.multidatasource.mapper.UserMapper;
import org.springframework.stereotype.Service;
//使用 @DS 切换数据源
@DS("mysql")
@Service
public class MysqlUserService extends ServiceImpl<UserMapper, User> {
}
测试
@Test测试
import com.qq.multidatasource.entity.User;
import com.qq.multidatasource.service.H2UserService;
import com.qq.multidatasource.service.MysqlUserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MultiDatasourceApplicationTests {
@Autowired
private H2UserService h2UserService;
@Autowired
private MysqlUserService mysqlUserService;
@Test
void testH2() {
List<User> h2List = h2UserService.list();
System.out.println("查询H2:");
h2List.forEach(System.out::println);
System.out.println("查询MySQL:");
List<User> mysqlList = mysqlUserService.list();
mysqlList.forEach(System.out::println);
}
}
测试结果
查询H2:
User(id=1, username=adminH2, password=xxx)
User(id=2, username=testH2, password=xxx)
查询MySQL:
User(id=1, username=admin, password=xxx)
User(id=2, username=test, password=xxx)
官网
参考:
https://baomidou.com/pages/a61e1b/
多数据源配置
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
slave_1:
url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
slave_2:
url: ENC(xxxxx) # 内置加密,使用请查看详细文档
username: ENC(xxxxx)
password: ENC(xxxxx)
driver-class-name: com.mysql.jdbc.Driver
#......省略
#以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2
# 多主多从 纯粹多库(记得设置primary) 混合配置
spring: spring: spring:
datasource: datasource: datasource:
dynamic: dynamic: dynamic:
datasource: datasource: datasource:
master_1: mysql: master:
master_2: oracle: slave_1:
slave_1: sqlserver: slave_2:
slave_2: postgresql: oracle_1:
slave_3: h2: oracle_2:
@DS注解
@DS 可以注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解。
注解 | 结果 |
---|---|
没有@DS | 默认数据源 |
@DS("dsName") | dsName可以为组名也可以为具体某个库的名称 |
@Service
@DS("slave")
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List selectAll() {
return jdbcTemplate.queryForList("select * from user");
}
@Override
@DS("slave_1")
public List selectByCondition() {
return jdbcTemplate.queryForList("select * from user where age >10");
}
}
标签:数据源,dynamic,plus,datasource,spring,mybatis,import,com
From: https://www.cnblogs.com/qode/p/17543102.html