mybatis支持多种数据源

有时候 代码中需要支持 多种数据源部署 (注意:非多数据源)
比如 同时支持 pgsqlmysql, 但因为某些 SQL 语法的不同,需要 根据不同的 数据库 执行不同的 SQL
这里 用到了 databaseIdProvider
mybatis – MyBatis 3 | Configuration

代码参考这个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import com.baomidou.mybatisplus.annotation.DbType;  
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class MybatisPlusConfig {

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}

@Bean
public DatabaseIdProvider getDatabaseIdProvider(){
DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties properties = new Properties();
properties.setProperty("Oracle","oracle");
properties.setProperty("MySQL","mysql");
properties.setProperty("DB2","db2");
properties.setProperty("Derby","derby");
properties.setProperty("H2","h2");
properties.setProperty("HSQL","hsql");
properties.setProperty("Informix","informix");
properties.setProperty("MS-SQL","ms-sql");
properties.setProperty("PostgreSQL","postgresql");
properties.setProperty("Sybase","sybase");
properties.setProperty("Hana","hana");
databaseIdProvider.setProperties(properties);
return databaseIdProvider;
}
}

使用时

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
<mapper namespace="io.datavines.server.repository.mapper.ConfigMapper">
<select id="getNow" resultType="java.lang.String">
select DATE_FORMAT(now(), '%Y-%m-%d') AS now_date
</select>
<select id="getNow" resultType="java.lang.String" databaseId="postgresql">
SELECT CURRENT_DATE;
</select>
</mapper>

这里 properties 设置的 keyvalue

  • key 是 从 DatabaseMetaData.getDatabaseProductName() 来, 不同数据库不一样
  • value 是我们自定义的别名,通过别名我们可以在SQL语句中标识适用于哪种数据库运行

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!