美文网首页
Dynamic DataSource初体验

Dynamic DataSource初体验

作者: iakuil | 来源:发表于2020-01-10 10:01 被阅读0次
Dynamic DataSource

关键字:Spring Boot,MyBatis,多数据源

最近做一个工具性质的Demo,需要同时连接MySQL和Oracle。按照以往的经验,需要两个JavaConfig来配置Datasource,所以不能直接使用MybatisAutoConfiguration。

偶然看到一个项目Dynamic DataSource,几行配置就搞定,感觉很方便,故尝试之。

以下就是application.yml相关配置:

spring:
  datasource:
    dynamic:
      primary: mysql
      datasource:
        mysql:
          type: com.zaxxer.hikari.HikariDataSource
          driverClassName: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://_your_mysql_host_:33060/test?characterEncoding=utf8
          username: test
          password: test
        oracle:
          type: com.zaxxer.hikari.HikariDataSource
          driverClassName: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@_your_oracle_host_:1521:orcl
          username: test
          password: test

mybatis:
  type-aliases-package: com.yodinfo.poc.domain
  mapper-locations: classpath*:mapper/**/*.xml

pagehelper:
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

是不是感觉so easy啊?

PS. 有一点需要注意,pagehelper分页插件不要配置helperDialect参数,让其自动判断数据库类型。

相关文章

网友评论

      本文标题:Dynamic DataSource初体验

      本文链接:https://www.haomeiwen.com/subject/qkbsactx.html