栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习 --- mybatisplus配置多数据源

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习 --- mybatisplus配置多数据源

一、mybatisplus配置多数据源
spring:
  # 配置多数据源信息
  datasource:
    # 配置数据源类型
    dynamic:
      # 设置默认数据源或者数据源组
      primary: master
      strict: false
      datasource:
        master:
          url: jdbc:mysql://localhost:3307/mybatis_plus?characterEncoding=utf-8&SSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: 123456
        slave_1:
          url: jdbc:mysql://localhost:3307/mybatis_plus_1?characterEncoding=utf-8&SSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: 123456
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 配置统一表的前缀名
  global-config:
    db-config:
      table-prefix: t_
      # 配置全局的主键生成策略
      id-type: auto
  # 配置包的别名
  type-aliases-package: com.cjc.mybatisplus.pojo
  #配置扫描枚举包
  type-enums-package: com.cjc.mybatisplus.enums
二、在service实现类上添加@DS
@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl implements ProductService {
}
@Service
@DS("master")
public class UserServiceImpl extends ServiceImpl implements UserService {
}
三、在pom.xml中添加
 
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.5.0
        
四、测试
@SpringBootTest
public class MultiSourceTest {
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;

    @Test
    public void Test01(){
        User byId = userService.getById(3);
        System.out.println(byId);
        Product byId1 = productService.getById(1);
        System.out.println(byId1);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/759232.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号