xxl-job适配postgresql数据库

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

获取源代码

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

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

建立数据库

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

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

xxl-job适配postgresql数据库_第1张图片

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

xxl-job适配postgresql数据库_第2张图片

创建 PostgreSQL 序列

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 数据库表自增主键

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


  org.postgresql
  postgresql
  42.3.1

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

xxl-job适配postgresql数据库_第3张图片

修改配置文件,指定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 ( 。

编译运行,成功

xxl-job适配postgresql数据库_第4张图片

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

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

你可能感兴趣的:(数据库,springboot)