好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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 ",命名为: v x.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自动创表的详细配置的详细内容...

  阅读:44次