zoukankan      html  css  js  c++  java
  • SpringBoot整合flyway

    1. 引入依赖
      pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <groupId>com.yang</groupId>
        <artifactId>fly-way-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>fly-way-demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-core</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.flywaydb</groupId>
                    <artifactId>flyway-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    1. 第一版sql脚本
      src/main/resources/db/migration/V1__sys_dept.sql
    SET FOREIGN_KEY_CHECKS=0;
    -- ----------------------------
    -- Table structure for sys_dept
    -- ----------------------------
    DROP TABLE IF EXISTS `sys_dept`;
    CREATE TABLE `sys_dept` (
      `dept_id` int(20) NOT NULL AUTO_INCREMENT,
      `name` varchar(50) DEFAULT NULL COMMENT '部门名称',
      `sort` int(11) DEFAULT '0' COMMENT '排序',
      `create_time` datetime DEFAULT NULL COMMENT '创建时间',
      `update_time` datetime DEFAULT NULL COMMENT '修改时间',
      `del_flag` char(1) DEFAULT '0' COMMENT '是否删除  -1:已删除  0:正常',
      `parent_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`dept_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='部门管理';
    
    1. 第二版sql脚本
      src/main/resources/db/migration/V1.1__sys_dept.sql
    INSERT INTO `sys_dept` VALUES (1, '总经办', 0, '2020-03-13 13:13:16', '2020-03-13 13:14:31', '0', 0);
    INSERT INTO `sys_dept` VALUES (2, '行政中心', 1, '2020-03-13 13:13:30', NULL, '0', 1);
    INSERT INTO `sys_dept` VALUES (3, '技术中心', 2, '2020-03-13 13:14:55', NULL, '0', 1);
    INSERT INTO `sys_dept` VALUES (4, '运营中心', 3, '2020-03-13 13:15:15', NULL, '0', 1);
    INSERT INTO `sys_dept` VALUES (5, '研发中心', 5, '2020-03-13 13:15:34', NULL, '0', 3);
    INSERT INTO `sys_dept` VALUES (6, '产品中心', 6, '2020-03-13 13:15:49', NULL, '0', 3);
    INSERT INTO `sys_dept` VALUES (7, '测试中心', 7, '2020-03-13 13:16:02', NULL, '0', 3);
    
    1. 项目配置文件,主要是dataSource,flyway的采用约定值
      src/main/resources/application.properties
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/fly_way?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    1. 一些flyway配置项
    flyway.baseline-description对执行迁移时基准版本的描述.
    flyway.baseline-on-migrate当迁移时发现目标schema非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认false.
    flyway.baseline-version开始执行基准迁移时对现有的schema的版本打标签,默认值为1.
    flyway.check-location检查迁移脚本的位置是否存在,默认false.
    flyway.clean-on-validation-error当发现校验错误时是否自动调用clean,默认false.
    flyway.enabled是否开启flywary,默认true.
    flyway.encoding设置迁移时的编码,默认UTF-8.
    flyway.ignore-failed-future-migration当读取元数据表时是否忽略错误的迁移,默认false.
    flyway.init-sqls当初始化好连接时要执行的SQL.
    flyway.locations迁移脚本的位置,默认db/migration.
    flyway.out-of-order是否允许无序的迁移,默认false.
    flyway.password目标数据库的密码.
    flyway.placeholder-prefix设置每个placeholder的前缀,默认${.
    flyway.placeholder-replacementplaceholders是否要被替换,默认true.
    flyway.placeholder-suffix设置每个placeholder的后缀,默认}.
    flyway.placeholders.[placeholder name]设置placeholder的value
    flyway.schemas设定需要flywary迁移的schema,大小写敏感,默认为连接默认的schema.
    flyway.sql-migration-prefix迁移文件的前缀,默认为V.
    flyway.sql-migration-separator迁移脚本的文件名分隔符,默认__
    flyway.sql-migration-suffix迁移脚本的后缀,默认为.sql
    flyway.tableflyway使用的元数据表名,默认为schema_version
    flyway.target迁移时使用的目标版本,默认为latest version
    flyway.url迁移时使用的JDBC URL,如果没有指定的话,将使用配置的主数据源
    flyway.user迁移数据库的用户名
    flyway.validate-on-migrate迁移时是否校验,默认为true.
    
    1. 启动项目可以看到业务表的变化及flyway_schema_history版本记录表的变化。
  • 相关阅读:
    11
    961. N-Repeated Element in Size 2N Array
    用numpy.pad()对图像进行填充及简单的图像处理
    709. To Lower Case
    929. Unique Email Addresses
    771. Jewels and Stones
    谭凯---访谈录
    如何拍照
    主题阅读法
    社会各职业工作重心和流程
  • 原文地址:https://www.cnblogs.com/JaxYoun/p/13218667.html
Copyright © 2011-2022 走看看