xxl-job适配postgresql数据库

xxl-job支持了mysql数据库,其他的数据库适配得自己弄一下,下面以目前最新的2.4.1为例进行说明适配postgresql数据库的过程。

获取源代码

从github或gitee获取源代码,目前最新版本2.4.1

xxl官网:分布式任务调度平台XXL-JOB

建立数据库

源代码的doc目录下有mysql建库脚本,在mysql中利用脚本建立数据库,

利用navicat的数据传输功能,将mysql数据库导入到postgresql中,

使用 Navicat 编辑器,手动修改所有表的数字类型字段,添加默认值 0。主键不用添加默认值。例如: int4 NOT NULL 类型为默认 0。

创建 PostgreSQL 序列

sql 复制代码
CREATE SEQUENCE xxl_job_user_id_seq START 1;
CREATE SEQUENCE xxl_job_info_id_seq START 1;
CREATE SEQUENCE xxl_job_log_id_seq START 1;
CREATE SEQUENCE xxl_job_log_report_id_seq START 1;
CREATE SEQUENCE xxl_job_logglue_id_seq START 1;
CREATE SEQUENCE xxl_job_registry_id_seq START 1;
CREATE SEQUENCE xxl_job_group_id_seq START 1;

修改 PostgreSQL 数据库表自增主键

sql 复制代码
ALTER TABLE "public"."xxl_job_user" alter column ID set default nextval('xxl_job_user_id_seq'::regclass);
ALTER TABLE "public"."xxl_job_info" alter column ID set default nextval('xxl_job_info_id_seq'::regclass);
ALTER TABLE "public"."xxl_job_log" alter column ID set default nextval('xxl_job_log_id_seq'::regclass);
ALTER TABLE "public"."xxl_job_log_report" alter column ID set default nextval('xxl_job_log_report_id_seq'::regclass);
ALTER TABLE "public"."xxl_job_logglue" alter column ID set default nextval('xxl_job_logglue_id_seq'::regclass);
ALTER TABLE "public"."xxl_job_registry" alter column ID set default nextval('xxl_job_registry_id_seq'::regclass);
ALTER TABLE "public"."xxl_job_group" alter column ID set default nextval('xxl_job_group_id_seq'::regclass);

修改源代码

修改 POM.xml 依赖,添加postgresql

XML 复制代码
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.3.1</version>
</dependency>

在resource下面新建两个文件夹,mysql和postgresql,将mybatis-mapper文件夹下的xml文件拷贝到这两个文件夹下:

修改配置文件,指定xml文件的路径和数据库配置,这样以后就可以兼容两种数据库,根据配置信息使用相应的数据库。

XML 复制代码
mybatis.mapper-locations=classpath:/postgresql/*Mapper.xml

### xxl-job, datasource
#spring.datasource.url=jdbc:mysql://127.0.0.1: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:postgresql://127.0.0.1:5432/xxl_job?currentSchema=public
spring.datasource.schemaName=public
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver

修改postgresql目录下的 Mapper 文件

  • 去掉所有字段名的转义符 ` ,直接用空格替换。
  • 修改 LIMIT #{offset}, #{pagesize} 为 LIMIT #{pagesize} OFFSET #{offset} 。修改LIMIT 0, #{limit}为LIMIT #{limit} OFFSET 0。修改LIMIT 0, #{clearBeforeNum}为LIMIT #{clearBeforeNum} OFFSET 0。LIMIT #{pagesize}保持不变。
  • 修改DATE_ADD(#{nowTime},INTERVAL - #{timeout} SECOND) 为 (#{nowTime}::timestamp - '${timeout} SECONDS'::interval)。
  • 修改 WHERE !( 为 WHERE not ( 。

编译运行,成功

修改后的源代码及数据库建库脚本:

https://download.csdn.net/download/xuruilll/88576748

相关推荐
Elastic 中国社区官方博客21 小时前
让大模型思考,让小模型执行:在 Elastic Workflows 中拆分 LLM 成本
大数据·运维·数据库·人工智能·elasticsearch·ai
Faith_xzc21 小时前
一条 SQL 顶一条 Flink 链路?Doris Streaming Job 持续导入全景解析
大数据·数据库·sql·flink
这个DBA有点耶1 天前
多模数据库深度解读:从“多库拼装”到“一库多能”的架构演进
数据库·mysql·dba
程序员夏洛1 天前
MySQL 默认的事务隔离级别是什么?为什么选择这个级别?
数据库·mysql
艺术留白1 天前
MokaTest使用篇-SQL接口
数据库·sql
( •̀∀•́ )9201 天前
MySQL 自动备份与恢复方案
数据库·mysql·oracle
这个DBA有点耶1 天前
大事务的“事前预防”:监控、拦截、Kill,四层防线一次讲透
数据库·mysql·代码规范
tachibana21 天前
RAGAS 指标解读
数据库·人工智能·算法·机器学习·架构·大模型·llm
mlidongfeng1 天前
[AI][昇腾950]TA 学习
数据库·学习
bestsun9991 天前
rac集群环境下配置JDBC
数据库