如果使用过
**starters-jdevelops-boot-web-jap**
,依赖可以忽略下面的步骤用(我默认注入了)
介绍
- 由于我们项目JPA使用的比较多,很多时候都是直接开项目写实体之后再去手动的创建一个数据库,为了节省这步操作于是有了这玩意
- 支持 mysql 5.7+
- 支持 postgresql
引入依赖
<= 2.0.4
<!-- 主依赖 -->
<dependency>
<groupId>cn.jdevelops</groupId>
<artifactId>utils-spring</artifactId>
<version>[2.0.2,2.0.4]</version>
<!-- <version>2.0.2</version>-->
</dependency>
<!-- 如果项目中没有的话需要引入guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
2.0.5 +
<!-- 主依赖 -->
<dependency>
<groupId>cn.jdevelops</groupId>
<artifactId>caches-auto-schema</artifactId>
<version>2.0.5</version>
</dependency>
<!-- 如果报错请引入guava(我测试没有引入没报错 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
使用
在主函数上加入注解 @EnableAutoSchema
@SpringBootApplication
@EnableAutoSchema
public class XxlJobAdminApplication {
public static void main(String[] args) {
SpringApplication.run(XxlJobAdminApplication.class, args);
}
}
效果
- 会根据你配置文件中的数据库连接地址进行自动建一个名叫test_auto_schema库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test_auto_schema?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
其他功能
关闭自动创建功能(二选一)
1. 注释掉开启注解
@EnableAutoSchema
2. 配置文件中设置关闭
jdevelops:
database:
init-enable: true
创建数据库时直接初始化表
1. 配置文件中加入
jdevelops.database.init-script=db/**test_auto_schema**.sql
2. resources
目录下放入待初始化的sql
这个操作会覆盖上面的自动建库,所以一定要有建库语句
``sql CREATE database if NOT EXISTS
test_auto_schemadefault character set utf8mb4 collate utf8mb4_unicode_ci; use
test_auto_schema`;
SET NAMES utf8mb4;
CREATE TABLE IF NOT EXISTS xxl_job_info
(
id
int(11) NOT NULL AUTO_INCREMENT,
job_group
int(11) NOT NULL COMMENT ‘执行器主键ID’,
job_desc
varchar(255) NOT NULL,
add_time
datetime DEFAULT NULL,
update_time
datetime DEFAULT NULL,
author
varchar(64) DEFAULT NULL COMMENT ‘作者’,
alarm_email
varchar(255) DEFAULT NULL COMMENT ‘报警邮件’,
schedule_type
varchar(50) NOT NULL DEFAULT ‘NONE’ COMMENT ‘调度类型’,
schedule_conf
varchar(128) DEFAULT NULL COMMENT ‘调度配置,值含义取决于调度类型’,
misfire_strategy
varchar(50) NOT NULL DEFAULT ‘DO_NOTHING’ COMMENT ‘调度过期策略’,
executor_route_strategy
varchar(50) DEFAULT NULL COMMENT ‘执行器路由策略’,
executor_handler
varchar(255) DEFAULT NULL COMMENT ‘执行器任务handler’,
executor_param
varchar(512) DEFAULT NULL COMMENT ‘执行器任务参数’,
executor_block_strategy
varchar(50) DEFAULT NULL COMMENT ‘阻塞处理策略’,
executor_timeout
int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘任务执行超时时间,单位秒’,
executor_fail_retry_count
int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘失败重试次数’,
glue_type
varchar(50) NOT NULL COMMENT ‘GLUE类型’,
glue_source
mediumtext COMMENT ‘GLUE源代码’,
glue_remark
varchar(128) DEFAULT NULL COMMENT ‘GLUE备注’,
glue_updatetime
datetime DEFAULT NULL COMMENT ‘GLUE更新时间’,
child_jobid
varchar(255) DEFAULT NULL COMMENT ‘子任务ID,多个逗号分隔’,
trigger_status
tinyint(4) NOT NULL DEFAULT ‘0’ COMMENT ‘调度状态:0-停止,1-运行’,
trigger_last_time
bigint(13) NOT NULL DEFAULT ‘0’ COMMENT ‘上次调度时间’,
trigger_next_time
bigint(13) NOT NULL DEFAULT ‘0’ COMMENT ‘下次调度时间’,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
注意事项
- postgresql 在2.0.6之前存在 url只能写成(jdbc:postgresql://127.0.0.1:5432/test_auto_schema)
- 后面跟参数会报错
- 2.0.6 修复了这个问题
示例项目地址
https://github.com/en-o/Jdevelops-Example/tree/main/AutoSchema