在写之前说一下,该实现只针对于提供多个数据库切换,并非项目支持多数据源动态切换。
近期我有一个需求,是把我们的项目本来从pg库切换到pg库和tidb库兼容的版本。(TIDB相关文档的链接:https://docs.pingcap.com/zh/tidb/stable)
pg库和tidb之间的互通性还算可以,但是有些函数已经不支持,并且有些操作符也不同,字段类型也不一样,比如json类型的存储,具体可以看文档。
当然对于这个多个数据库兼容的DatabaseIdProvider的核心就是databaseId属性。
databaseId属性: 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;
如果带或者不带的语句都有,则不带的会被忽略。新增,修改和删除都有这个属性。
话不多说,直接上代码。
1.先引入相对应的jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--oracle-->
<dependency>
<groupId> mysql </groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--postgresql-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>
<!-- druid数据库连接池组件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
2.通过application.yml或者properties添加两个数据库的支持
# 数据源配置
spring:
datasource:
hikari:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test?&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
# 数据源配置
spring:
datasource:
hikari:
jdbc-url: jdbc:postgresql://127.0.0.1:5432/test
driver-class-name: org.postgresql.Driver
username: root
password: 123456
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/**/*.xml
type-aliases-package: com.valten.**.model
# showSql 控制台打印sql日志
logging:
level:
com:
valten:
dao: debug
想用哪个,注释另外一个,或者指定别的配置文件,单独抽出来,这里建议抽出来,我这里是单纯为了方便写。
3.创建数据源的配置类
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.Properties;
/**
* 多数据库支持配置文件
* @Author tong(BelenChan)
* @Date 2021-02-23
*/
@Configuration
public class DataSourceConfig {
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties("spring.datasource.hikari")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DatabaseIdProvider databaseIdProvider() {
DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties p = new Properties();
p.setProperty("Oracle", "oracle");
p.setProperty("TiDB", "tidb");
p.setProperty("PostgreSQL", "postgresql");
p.setProperty("DB2", "db2");
p.setProperty("SQL Server", "sqlserver");
databaseIdProvider.setProperties(p);
return databaseIdProvider;
}
@Primary
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setDatabaseIdProvider(databaseIdProvider());
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
return factoryBean;
}
}
4.然后再各自对应的mapper.xml里面添加baseId属性
<select id="queryDataList" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<if test="_databaseId == 'tidb'">
id
</if>
<if test="_databaseId == 'postgresql'">
app_code
</if>
from paas_mt_user
where user_id = #{id,jdbcType=CHAR}
</select>
后面会更新两个不同的数据库应该注意更改哪些内容(仅针对于我写的项目)