- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章springboot集成flyway自动创表的详细配置由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
flayway是一款数据库版本控制管理工具,,支持数据库版本自动升级,migrations可以写成sql脚本,也可以写在java代码里;不仅支持command line和java api ,也支持build构建工具和spring boot,也可以在分布式环境下能够安全可靠安全地升级数据库,同时也支持失败恢复.
flyway最核心的就是用于记录所有版本演化和状态的metadata表,flyway首次启动会创建默认名为schema_version的元素局表。 表中保存了版本,描述,要执行的sql脚本等; 。
在ruoyi-admin这个module里面添加flyway依赖 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<dependency>
<groupid>org.flywaydb</groupid>
<artifactid>flyway-core</artifactid>
</dependency>
<!-- 不是必须的 -->
<build>
<plugins>
<plugin>
<groupid>org.flywaydb</groupid>
<artifactid>flyway-maven-plugin</artifactid>
<version>
5.2
.
1
</version>
</plugin>
</plugins>
</build>
|
yml 配置flyway 。
1
2
3
4
5
6
7
8
9
|
spring:
# 配置flyway数据版本管理
flyway:
enabled:
true
baseline-on-migrate:
true
clean-on-validation-error:
false
sql-migration-prefix: v
sql-migration-suffixes: .sql
locations: classpath:db/migration/mysql
|
配置sql脚本 在若依项目中的sql目录下有两个初始化sql脚本,在ruoyi-admin模块下的"resources/db/migration/mysql",命名为:vx.x.x__ xxx.sql数据库文件.
一般的springboot项目进行到这里,flyway配置就完成了,不过ruoyi项目到这里启动的话,还是会报错,主要是有三个地方用到了@postconstruct注解,系统需要从数据库中加载配置信息,并且是构造bean后就执行,此时flaway的数据库配置加载还没执行,如果是第一次执行项目的话,数据库都还没有表结构信息,所以会报错。 直接改若依项目项目启动是加载到缓存的配置的这三个地方的加载时机就行了。 首先,注释掉三个地方的配置加载。 ruoyi-system中com.ruoyi.system.service.impl.sysconfigserviceimpl的参数缓存配置 。
ruoyi-system中com.ruoyi.system.service.impl.sysdicttypeserviceimpl的字典信息缓存配置 。
ruoyi-quartz中com.ruoyi.quartz.service.impl.sysjobserviceimpl的定时任务配置 。
在ruoyi-system中新增一个配置类com.ruoyi.system.config.runtimeconfig,内容如下,在项目加载完成后flyaway加载完成后再执行这些参数的缓存配置.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
import
org.quartz.scheduler;
import
org.quartz.schedulerexception;
import
org.slf4j.logger;
import
org.slf4j.loggerfactory;
import
org.springframework.beans.factory.annotation.autowired;
import
org.springframework.context.applicationlistener;
import
org.springframework.context.event.contextrefreshedevent;
import
org.springframework.stereotype.component;
import
java.util.list;
/**
*
* @author: 云诺
* @date: 2021/6/25
* @description: 将项目启动后flyway创建好表加载到缓存
*/
@component
public
class
runtimeconfig
implements
applicationlistener<contextrefreshedevent> {
private
final
static
logger logger = loggerfactory.getlogger(runtimeconfig.
class
);
@autowired
private
sysconfigmapper configmapper;
@autowired
private
sysdicttypemapper dicttypemapper;
@autowired
private
sysdictdatamapper dictdatamapper;
@autowired
private
scheduler scheduler;
@autowired
private
sysjobmapper jobmapper;
/**
* 项目启动时,初始化参数
*/
@override
public
void
onapplicationevent(contextrefreshedevent contextrefreshedevent) {
logger.info(
"init param ..."
);
this
.initparam();
logger.info(
"init dict ..."
);
this
.initdict();
try
{
logger.info(
"init job ..."
);
this
.initjob();
}
catch
(schedulerexception e) {
e.printstacktrace();
}
catch
(taskexception e) {
e.printstacktrace();
}
}
/**
* 初始化定时任务信息到缓存
*
* @throws schedulerexception
* @throws taskexception
*/
public
void
initjob()
throws
schedulerexception, taskexception {
scheduler.clear();
list<sysjob> joblist = jobmapper.selectjoball();
for
(sysjob job : joblist) {
scheduleutils.createschedulejob(scheduler, job);
}
}
/**
* 初始化参数到缓存
*/
public
void
initparam() {
list<sysconfig> configslist = configmapper.selectconfiglist(
new
sysconfig());
for
(sysconfig config : configslist)
{
cacheutils.put(getcachename(), getcachekey(config.getconfigkey()), config.getconfigvalue());
}
}
/**
* 初始化字典到缓存
*/
public
void
initdict() {
list<sysdicttype> dicttypelist = dicttypemapper.selectdicttypeall();
for
(sysdicttype dicttype : dicttypelist)
{
list<sysdictdata> dictdatas = dictdatamapper.selectdictdatabytype(dicttype.getdicttype());
dictutils.setdictcache(dicttype.getdicttype(), dictdatas);
}
}
/**
* 设置cache key
*
* @param configkey 参数键
* @return 缓存键key
*/
private
string getcachekey(string configkey)
{
return
constants.sys_config_key + configkey;
}
/**
* 获取cache name
*
* @return 缓存名
*/
private
string getcachename()
{
return
constants.sys_config_cache;
}
}
|
到这里就可以正常启动项目了 。
以上就是springboot集成flyway自动创表的详细内容,更多关于springboot自动创表的资料请关注我其它相关文章! 。
原文链接:https://blog.csdn.net/ayunnuo/article/details/118220339 。
最后此篇关于springboot集成flyway自动创表的详细配置的文章就讲到这里了,如果你想了解更多关于springboot集成flyway自动创表的详细配置的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!