xxl-job适配sqlite本地数据库及mysql数据库。可根据配置指定使用哪种数据库。

xxl-job整合sqlite

  1. 根据mysql的脚本,整理为sqlite的脚本,详见tables_xxl_job.sqllite.sql

  2. 创建sqlite数据库文件并执行上述sql,得到xxl-job-admin的resource下db/xxl-job

  3. xxl-job-admin的pom中添加sqlite驱动依赖

    xml 复制代码
    <!--sqlite-jdbc -->
    		<dependency>
    			<groupId>org.xerial</groupId>
    			<artifactId>sqlite-jdbc</artifactId>
    			<version>3.43.2.1</version>
    		</dependency>
  4. 修改application.properties配置

    添加配置:

    properties 复制代码
    # mysql||sqlite
    spring.profiles.active=${CURRENT_DBTYPE:sqlite}
    #spring.profiles.active=${CURRENT_DBTYPE:mysql}
    database.type: ${spring.profiles.active}

    mybatis配置:

    properties 复制代码
    ### mybatis
    mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
    mybatis.configuration.database-id=${database.type}
    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    注释掉datasource相关配置

    properties 复制代码
    ### xxl-job, datasource
    #spring.datasource.url=jdbc:mysql://127.0.0.1:3306/srvmonitor?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    #spring.datasource.username=root
    #spring.datasource.password=root
    #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  5. 创建application-mysql.properties及application-sqlite.properties

    application-mysql.properties:

    properties 复制代码
    ### mysql
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:127.0.0.1:3306}/srvmonitor?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    application-sqlite.properties:

    properties 复制代码
    ### sqllite
    spring.datasource.driver-class-name=org.sqlite.JDBC
    spring.datasource.url=jdbc:sqlite::resource:${CONNECTION_URL:db/xxl_job}?date_string_format=yyyy-MM-dd HH:mm:ss
  6. 修改XxlJobAdminApplication启动类

    java 复制代码
    @SpringBootApplication
    public class XxlJobAdminApplication implements ApplicationContextAware {
    
    	public static void main(String[] args) {
            SpringApplication.run(XxlJobAdminApplication.class, args);
    	}
    
    	@Bean
    	public DatabaseIdProvider getDatabaseIdProvider() {
    		DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
    		Properties properties = new Properties();
    		properties.setProperty("SQLite", "sqlite");
    		properties.setProperty("MySQL", "mysql");
    		databaseIdProvider.setProperties(properties);
    		return databaseIdProvider;
    	}
    
    	@Override
    	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    		Environment environment = applicationContext.getBean(Environment.class);
    		String url = environment.getProperty("spring.datasource.url");
    		if (!StringUtils.isEmpty(url)) {
    			DataSourceUtil.setDbType(url);
    		}
    	}
    }
  7. 修改JobScheduleHelper中start方法中适配sqlite的语法

     preparedStatement = conn.prepareStatement(  "select * from xxl_job_lock where lock_name = 'schedule_lock' for update" );
                            if ("sqlite".equalsIgnoreCase(DataSourceUtil.getDbType())) {
                                preparedStatement = conn.prepareStatement("select * from xxl_job_lock where lock_name = 'schedule_lock'");
                            } else {
                                preparedStatement = conn.prepareStatement("select * from xxl_job_lock where lock_name = 'schedule_lock' for update");
                            }
                            preparedStatement.execute();
    
  8. 所有dao中pageList、pageListCount、save方法对应的xml,按照databaseId为mysql和sqlite分别写两套

    sqlite不支持CONCAT函数

    sqlite不支持useGeneratedKeys="true"来自动获取新增后的id(需要用到的地方,改成用select last_insert_rowid()获取最新的id)

相关推荐
远歌已逝1 分钟前
维护在线重做日志(二)
数据库·oracle
qq_433099401 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz1 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
工业甲酰苯胺3 小时前
Redis性能优化的18招
数据库·redis·性能优化
世间万物皆对象4 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了4 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
i道i5 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
小怪兽ysl5 小时前
【PostgreSQL使用pg_filedump工具解析数据文件以恢复数据】
数据库·postgresql
qq_17448285755 小时前
springboot基于微信小程序的旧衣回收系统的设计与实现
spring boot·后端·微信小程序
wqq_9922502775 小时前
springboot基于微信小程序的食堂预约点餐系统
数据库·微信小程序·小程序