> ###基于xxl-job V2.4.0版本改造,由于目前版本只支持mysql使用,在现实工作中会有 多种数据库的可能,所以花时间进行了一个简单的适配,将常用的数据库进行了一个简单的适配 具体适配是使用mybatis的databaseId进行适配的,这个只是暂时的解决办法, 许大神后续的计划中会把orm层改成jpa,这样就不用再单独区分数据库了,所以大家 没必要去改造成mybatis-plus或者通用mapper,后续根据官网的更新就好了,毕竟改造 太多,回头官网更新不太顺利。
"水平一般,能力有限,不足之处,还望多指教"
[码云仓库地址](http://https://gitee.com/fightingYouth/xxl-job-otherDB)
1、pom中引用数据库包(有可能会找不到,因为有些包在我自己私服上面),相信做技术的都是有办法搞定的
``` xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!-- 金仓 -->
<dependency>
<groupId>com.xxx.3rd</groupId>
<artifactId>kingbase8</artifactId>
<version>${kingbase.verison}</version>
</dependency>
<!-- 达梦 -->
<dependency>
<groupId>com.xxx.3rd</groupId>
<artifactId>dm8</artifactId>
<version>${dm8.verison}</version>
</dependency>
<!-- oracle -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.verison}</version>
</dependency>
<!-- pgsql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${pgsql.verison}</version>
</dependency>
```
##### 2、application配置文件修改
```
#mysql数据库配置
#spring.datasource.url=jdbc:mysql://10.30.100.73:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#人大金仓数据库配置
#spring.datasource.url=jdbc:kingbase8://10.30.100.76:54321/XXL_JOB?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding¤tSchema=PUBLIC,SYS_CATALOG
#spring.datasource.username=SYSTEM
#spring.datasource.password=password123
#spring.datasource.driver-class-name=com.kingbase8.Driver
##达梦数据库配置
#spring.datasource.url=jdbc:dm://10.30.100.76:5236/XXL_JOB?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
#spring.datasource.username=XXL_JOB
#spring.datasource.password=password123
#spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
###ORACLE数据库配置
spring.datasource.url=jdbc:oracle:thin:@10.30.100.10:1521:orcl
spring.datasource.username=XXL_JOB
spring.datasource.password=password123
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
####postgreSQL数据库配置
#spring.datasource.url=jdbc:postgresql://10.30.100.9:5432/xxl-job
#spring.datasource.username=xxl-job
#spring.datasource.password=password123
#spring.datasource.driver-class-name=org.postgresql.Driver
```
配置文件这里面有个小坑,因为oracle的特殊性,不支持自增,查询方式也不同,分页也
不一样,所以适配oracle比较复杂,就在数据库这块单独把oracle的mapper拉了出来
```
### mybatis
#mysql数据库配置mybatis
##oracle使用的语法
mybatis.mapper-locations=classpath:/mybatis-oracle-mapper/*Mapper.xml
##其他数据库使用的语法
#mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
这块应该大家能理解,毕竟项目中一般只用一种,所以改一下就好了
##oracle使用的语法
spring.datasource.hikari.connection-test-query=SELECT * from dual
##其他数据库使用的语法
#spring.datasource.hikari.connection-test-query=SELECT 1
```
##### 3、增加多数据库配置
```java
/**
* 自动识别使用的数据库类型
* 在mapper.xml中databaseId的值就是跟这里对应,
* 如果没有databaseId选择则说明该sql适用所有数据库
* */
@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");
properties.setProperty("DM","dm");
properties.setProperty("KingbaseES","kingbase");
properties.setProperty("KingBase8","kingbase");
databaseIdProvider.setProperties(properties);
return databaseIdProvider;
}
```
##### 4、在数据库中做多数据库兼容
``` xml
<select id="findAll" parameterType="java.util.HashMap" resultMap="XxlJobRegistry">
SELECT <include refid="Base_Column_List" />
FROM xxl_job_registry t
<if test="_databaseId=='mysql'">
WHERE t.update_time <![CDATA[ > ]]> DATE_ADD(#{nowTime},INTERVAL -#{timeout} SECOND)
</if>
<if test="_databaseId=='dm'">
WHERE t.update_time <![CDATA[ > ]]> (now()-1/(24*60*60)*#{timeout})
</if>
<if test="_databaseId=='kingbase'||_databaseId=='postgresql'">
WHERE t.update_time <![CDATA[ > ]]> date '${nowTime}' - INTERVAL '${timeout} second'
</if>
</select>
```
[码云仓库地址](http://https://gitee.com/fightingYouth/xxl-job-otherDB)