Live2D

SpirngBoot 配置多数据源

引入maven依赖

 <!-- MyBatis-Plus 核心依赖 --> 
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
  </dependency>
  
 <!-- MyBatis-Plus 动态数据源依赖 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

yaml文件内容如下

spring:
  datasource:
    dynamic:
      primary: test # 设置默认数据源名称
      datasource:
        test: # 第一个数据源配置
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          username: root
          password: 123456
        test2: # 第二个数据源配置
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          username: root
          password: 123456

mapper加上 @DS注解

@Repository
@DS("test2")
public interface DeptMapper extends BaseMapper<Dept> {

}
@Repository
@DS("test")
public interface UserMapper extends BaseMapper<User> {

}

service层面

public interface UserService extends IService<User> {
}

实现类

@Service
@DS("slave")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

@DS("slave") 注解最好加上,否则执行批量插入的时候会报错

posted @ 2023-05-30 11:31  没有梦想的java菜鸟  阅读(22)  评论(0编辑  收藏  举报