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

springboot,mybatis-plus配置多数据源

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

springboot,mybatis-plus配置多数据源

mybatis_plus配置多数据源官方文档

1. 引入依赖dynamic-datasource-spring-boot-starter:
写这片博客时最新依赖的最新版本是3.5.1
 		
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.5.1
        
2.配置文件
server:
  port: 8890
spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure #因为Spring Boot 框架会自动配置数据源,自动从yml中读取数据源信息,因此我们在配置自定义的数据源的时候,需要exclude = DataSourceAutoConfiguration.class来禁掉数据源的自动配置
  datasource:
    dynamic:
      primary: mysql  #设置默认的数据源或者数据源组,默认值即为mysql
      datasource:
        mysql:
          driver-class-name: com.mysql.jdbc.Driver
          url: ***
          username: ***
          password: ***
        postgresql:
          url: ***
          username: **
          password: **
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      logic-delete-value: 1 # 逻辑已删除值(默认为 Y)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 N)
  mapper-locations: classpath:mapper/*.xml
3.使用@DS切换数据源
注解结果
没有@DS默认数据源
有@DS(“mysql”)以mysql为名也可以为具体某个库的名称
@Service
@DS("mysql")
public class UserServiceImpl implements UserService {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public List selectAll() {
    return  jdbcTemplate.queryForList("select * from user");
  }
  
  @Override
  @DS("mysql")
  public List selectByCondition() {
    return  jdbcTemplate.queryForList("select * from user where age >10");
  }
}

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

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

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