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)

相关推荐
独行soc17 分钟前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘
White_Mountain35 分钟前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
Code apprenticeship36 分钟前
怎么利用Redis实现延时队列?
数据库·redis·缓存
百度智能云技术站39 分钟前
广告投放系统成本降低 70%+,基于 Redis 容量型数据库 PegaDB 的方案设计和业务实践
数据库·redis·oracle
老王笔记40 分钟前
GTID下复制问题和解决
mysql
装不满的克莱因瓶42 分钟前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
Lojarro2 小时前
【Spring】Spring框架之-AOP
java·mysql·spring
梦想平凡2 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
isolusion2 小时前
Springboot的创建方式
java·spring boot·后端
TianyaOAO3 小时前
mysql的事务控制和数据库的备份和恢复
数据库·mysql