spring mvc接入flyway来对数据库进行版本控制

spring mvc是通过maven来进行管理的

1:在pom.xml中的dependencies里面加入

    <dependency>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-core</artifactId>
      <version>4.0.3</version>
    </dependency>

2:在pom.xml中的plugins中加入


      <plugin>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-maven-plugin</artifactId>
        <version>4.0.3</version>
      </plugin>

3:设置flyway相关属性可以让flyway可以与mysql进行连接

3.1:在pom.xml同级目录下新建flyway.properties文件

文件的内容大致为:

flyway.user=username
flyway.password=password
flyway.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
flyway.baselineOnMigrate=true

这个方法也可以改变文件的名称,也就是说可以不叫flyway.properties ,可以命名为xxxxx.properties

但是这个在最后执行命令的时候要加上参数 -Dflyway.configFile=xxxxx.properties

注意这个文件必须要与pom.xml同级

3.2:在plugin里面进行配置,例如:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.0.3</version>
    <configuration>
        <user>databaseUser</user>
        <password>databasePassword</password>
        <schemas>
            <schema>schemaName</schema>
        </schemas>
        ...
    </configuration>
</plugin>

3.3:通过Maven Properties来进行配置,在pom.xml中的properties里面设置,例如:

<project>
    ...
    <properties>
        <flyway.user>databaseUser</flyway.user>
        <flyway.password>databasePassword</flyway.password>
        <flyway.schemas>schemaName</flyway.schemas>
        ...
    </properties>
    ...
</project>

4:在resources文件夹下新建db/migration目录

在这个目录里面可以创建你的migration文件。

注意文件的命名规则:

<Prefix><Version>__<Description>.sql

Where:

  • <Prefix> – Default prefix is V, which may be configured in the above configuration file using the flyway.sqlMigrationPrefix property.
  • <Version> – Migration version number. Major and minor versions may be separated by an underscore. Migration version should always start with 1.
  • <Description> – Textual description of the migration. The description needs to be separated from the version numbers with a double underscore.

Example: V1_1_0__my_first_migration.sql

5:最后一步就是执行flyway

mvn clean flyway:migrate

6:调用的过程中出现过的错误

6.1:flyway转化为mysql表之后中文乱码 这是因为我刚开始设置的是

flyway.url=jdbc:mysql://localhost:3306/database

与数据库连接的编码没有设置为utf-8造成的,改为

flyway.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull

就可以了

6.2:flyway.url是必须项,不设置会报错,具体的属性描述见https://flywaydb.org/documentation/maven/migrate

6.3:出现错误 Found non-empty schema "public" without metadata table! Use init() or set initOnMigrate to true to initialize the metadata table.

这是因为没有加入 flyway.baselineOnMigrate=true 这个属性 ,所以报错啦

7:以上是通过命令来执行flyway,那能不能够通过程序运行来执行flyway呢,这个是可以的。

步骤可以是一下:

第一、二步跟上面的1、2步一致。

第三步:在resources下面创建spring-context-flyway.xml文件

文件里面的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" >

	<description>flyway Configuration</description>


	 <!--flyway 配置-->
	<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
		<property name="dataSource" ref="dataSource"/>
		<property name="encoding" value="UTF-8"/>
		<property name="locations" value="db/migration"/>
		<property name="baselineOnMigrate" value="true"/>
		<property name="placeholderReplacement" value="false" />
		<property name="baselineVersion" >
			<bean class="org.flywaydb.core.api.MigrationVersion" factory-method="fromVersion">
				<constructor-arg value="0"/>
			</bean>
		</property>
	</bean>

</beans>

其中dataSource指向的是mybatis.xml文件中定义的mysql数据源配置。

第四步:在web.xml中的

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        .......
    </param-value>
</context-param>

中加入

classpath:spring-context-flyway.xml

最终变成

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            ......
            classpath:spring-context-flyway.xml
        </param-value>
    </context-param>

启动程序,就会自动执行flyway

执行之后在数据库里面会增加一个表名为schema_version

表结构如下:

ok,大功告成啦!

猜你喜欢

转载自blog.csdn.net/wahaha13168/article/details/81172418
今日推荐