springboot集成flyway自動(dòng)創(chuàng)表的詳細(xì)配置
Flayway是一款數(shù)據(jù)庫(kù)版本控制管理工具,,支持?jǐn)?shù)據(jù)庫(kù)版本自動(dòng)升級(jí),Migrations可以寫成sql腳本,也可以寫在java代碼里;不僅支持Command Line和java api ,也支持Build構(gòu)建工具和Spring boot,也可以在分布式環(huán)境下能夠安全可靠安全地升級(jí)數(shù)據(jù)庫(kù),同時(shí)也支持失敗恢復(fù)。
Flyway最核心的就是用于記錄所有版本演化和狀態(tài)的MetaData表,F(xiàn)lyway首次啟動(dòng)會(huì)創(chuàng)建默認(rèn)名為SCHEMA_VERSION的元素局表。 表中保存了版本,描述,要執(zhí)行的sql腳本等;
在ruoyi-admin這個(gè)module里面添加flyway依賴
<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
spring: # 配置flyway數(shù)據(jù)版本管理 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腳本
在若依項(xiàng)目中的sql目錄下有兩個(gè)初始化sql腳本,在ruoyi-admin模塊下的"resources/db/migration/mysql
",命名為:V
x.x.x__
xxx.sql
數(shù)據(jù)庫(kù)文件。
一般的springboot項(xiàng)目進(jìn)行到這里,flyway配置就完成了,不過(guò)ruoyi項(xiàng)目到這里啟動(dòng)的話,還是會(huì)報(bào)錯(cuò),主要是有三個(gè)地方用到了@PostConstruct
注解,系統(tǒng)需要從數(shù)據(jù)庫(kù)中加載配置信息,并且是構(gòu)造bean后就執(zhí)行,此時(shí)flaway的數(shù)據(jù)庫(kù)配置加載還沒(méi)執(zhí)行,如果是第一次執(zhí)行項(xiàng)目的話,數(shù)據(jù)庫(kù)都還沒(méi)有表結(jié)構(gòu)信息,所以會(huì)報(bào)錯(cuò)。
直接改若依項(xiàng)目項(xiàng)目啟動(dòng)是加載到緩存的配置的這三個(gè)地方的加載時(shí)機(jī)就行了。
首先,注釋掉三個(gè)地方的配置加載。
ruoyi-system
中com.ruoyi.system.service.impl.SysConfigServiceImpl
的參數(shù)緩存配置
ruoyi-system
中com.ruoyi.system.service.impl.SysDictTypeServiceImpl
的字典信息緩存配置
ruoyi-quartz
中com.ruoyi.quartz.service.impl.SysJobServiceImpl
的定時(shí)任務(wù)配置
在ruoyi-system
中新增一個(gè)配置類com.ruoyi.system.config.RuntimeConfig
,內(nèi)容如下,在項(xiàng)目加載完成后flyaway加載完成后再執(zhí)行這些參數(shù)的緩存配置。
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: 將項(xiàng)目啟動(dòng)后flyway創(chuàng)建好表加載到緩存 */ @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; /** * 項(xiàng)目啟動(dòng)時(shí),初始化參數(shù) */ @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(); } } /** * 初始化定時(shí)任務(wù)信息到緩存 * * @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); } } /** * 初始化參數(shù)到緩存 */ 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); } } /** * 設(shè)置cache key * * @param configKey 參數(shù)鍵 * @return 緩存鍵key */ private String getCacheKey(String configKey) { return Constants.SYS_CONFIG_KEY + configKey; } /** * 獲取cache name * * @return 緩存名 */ private String getCacheName() { return Constants.SYS_CONFIG_CACHE; } }
到這里就可以正常啟動(dòng)項(xiàng)目了
以上就是springboot集成flyway自動(dòng)創(chuàng)表的詳細(xì)內(nèi)容,更多關(guān)于springboot自動(dòng)創(chuàng)表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)遷移的實(shí)現(xiàn)示例
- SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)管理的操作方法
- SpringBoot項(xiàng)目集成Flyway詳細(xì)過(guò)程
- SpringBoot整合flyway實(shí)現(xiàn)自動(dòng)創(chuàng)建表的方法
- SpringBoot集成Flyway進(jìn)行數(shù)據(jù)庫(kù)版本遷移管理的步驟
- SpringBoot使用flyway初始化數(shù)據(jù)庫(kù)
- SpringBoot整合flyway實(shí)現(xiàn)步驟解析
- Flyway詳解及Springboot集成Flyway的詳細(xì)教程
- SpringBoot項(xiàng)目集成Flyway進(jìn)行數(shù)據(jù)庫(kù)版本控制的詳細(xì)教程
- SpringBoot整合Flyway的方法(數(shù)據(jù)庫(kù)版本遷移工具)
- springboot配置flyway(入門級(jí)別教程)
相關(guān)文章
Java描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之鏈表的增刪改查詳解
這篇文章主要給大家介紹了關(guān)于Java描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之鏈表的增刪改查的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05Mybatis批量插入index out of range錯(cuò)誤的解決(較偏的錯(cuò)誤)
這篇文章主要介紹了Mybatis批量插入index out of range錯(cuò)誤的解決(較偏的錯(cuò)誤),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java timezone設(shè)置和mybatis連接數(shù)據(jù)庫(kù)時(shí)區(qū)設(shè)置方式
這篇文章主要介紹了Java timezone設(shè)置和mybatis連接數(shù)據(jù)庫(kù)時(shí)區(qū)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09spring boot中xalan引入報(bào)錯(cuò)系統(tǒng)找不到指定的文件原因分析
這篇文章主要介紹了spring boot中xalan引入報(bào)錯(cuò)系統(tǒng)找不到指定的文件,主要原因是內(nèi)嵌的tomcat9.0.36,本文給大家分享最新解決方法,需要的朋友可以參考下2023-08-08源碼解讀Spring-Integration執(zhí)行過(guò)程
Spring-Integration基于Spring,在應(yīng)用程序中啟用了輕量級(jí)消息傳遞,并支持通過(guò)聲明式適配器與外部系統(tǒng)集成,今天主要是看個(gè)簡(jiǎn)單的hello word進(jìn)來(lái)分析下整個(gè)執(zhí)行過(guò)程,感興趣的朋友一起看看吧2021-06-06