springboot 통합 fly way 자동 생 성 표 의 상세 한 설정

Flayway 는 데이터베이스 버 전 관리 도구 로 데이터베이스 버 전 자동 업 그 레이 드 를 지원 합 니 다.Migrations 는 sql 스 크 립 트 로 쓸 수도 있 고 자바 코드 에 쓸 수도 있 습 니 다.Command Line 과 자바 api 뿐만 아니 라 Build 구축 도구 와 Spring boot 도 지원 하 며 분포 식 환경 에서 데이터 베 이 스 를 안전 하고 안전하게 업그레이드 할 수 있 으 며 실패 복구 도 지원 합 니 다.
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'는Vx.x.x__xxx.sql데이터베이스 파일 이 라 고 명명 합 니 다.
在这里插入图片描述
일반적인 springboot 프로젝트 가 여기까지 진행 되면 flyway 설정 이 완 료 됩 니 다.그러나 ruoyi 프로젝트 가 여기 서 시작 되면 오류 가 발생 할 수 있 습 니 다.주로 세 곳 에서@PostConstruct주 해 를 사 용 했 습 니 다.시스템 은 데이터 베이스 에서 설정 정 보 를 불 러 오고 bean 을 구성 한 후에 실 행 됩 니 다.이때 flyway 의 데이터 베이스 설정 로드 가 실행 되 지 않 았 습 니 다.프로젝트 를 처음 수행 하 는 경우 데이터 베 이 스 는 아직 표 구조 정보 가 없어 잘못 보고 할 수 있다.
프로젝트 항목 에 따라 시작 하 는 것 이 캐 시 에 불 러 오 는 설정 인 이 세 곳 의 불 러 오 는 시 기 를 직접 바 꾸 면 됩 니 다.
우선,세 곳 의 설정 을 주석 하여 불 러 옵 니 다.ruoyi-system의 매개 변수 캐 시 설정
在这里插入图片描述 com.ruoyi.system.service.impl.SysConfigServiceImpl의 사전 정보 캐 시 설정
在这里插入图片描述 ruoyi-systemcom.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 자동 창 표 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!

좋은 웹페이지 즐겨찾기