springboot 통합 fly way 자동 생 성 표 의 상세 한 설정
6644 단어 springbootflyway자동 으로 표를 작성 하 다.
Flyway 의 가장 핵심 은 모든 버 전의 진화 와 상 태 를 기록 하 는 MetaData 표 입 니 다.Flyway 가 처음 시작 하면 기본 이름 인 SCHEMA 를 만 듭 니 다.VERSION 의 원소 국 표.표 에 버 전,설명,실행 할 sql 스 크 립 트 등 이 저장 되 어 있 습 니 다.
ruoyi-admin 이 module 에 fly way 의존 도 를 추가 합 니 다.
<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>
flyway 설정
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 을 구성 한 후에 실 행 됩 니 다.이때 flyway 의 데이터 베이스 설정 로드 가 실행 되 지 않 았 습 니 다.프로젝트 를 처음 수행 하 는 경우 데이터 베 이 스 는 아직 표 구조 정보 가 없어 잘못 보고 할 수 있다.프로젝트 항목 에 따라 시작 하 는 것 이 캐 시 에 불 러 오 는 설정 인 이 세 곳 의 불 러 오 는 시 기 를 직접 바 꾸 면 됩 니 다.
우선,세 곳 의 설정 을 주석 하여 불 러 옵 니 다.
ruoyi-system
의 매개 변수 캐 시 설정com.ruoyi.system.service.impl.SysConfigServiceImpl
의 사전 정보 캐 시 설정ruoyi-system
중com.ruoyi.system.service.impl.SysDictTypeServiceImpl
의 정시 퀘 스 트 설정ruoyi-quartz
에 설정 클래스com.ruoyi.quartz.service.impl.SysJobServiceImpl
를 추가 합 니 다.내용 은 다음 과 같 습 니 다.프로젝트 로 딩 이 끝 난 후에 flyaway 로 딩 이 완 료 된 후에 이 매개 변수의 캐 시 설정 을 실행 합 니 다.
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 자동 창 표 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin Springboot -- 파트 14 사용 사례 REST로 전환하여 POST로 JSON으로 전환前回 前回 前回 記事 の は は で で で で で で を 使っ 使っ 使っ て て て て て リクエスト を を 受け取り 、 reqeustbody で 、 その リクエスト の ボディ ボディ を を 受け取り 、 関数 内部 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.