IDEA中MyBatis Migrations Maven plugin 使用总结

来源:互联网 发布:淘宝售后快捷短语话术 编辑:程序博客网 时间:2024/06/08 04:05

        MyBatis/migrations-maven-plugin( 下载地址下文简称migrations)是一个开源的用于DB版本控制的命令行插件,Migrations通过生成统一格式的DDL脚本模板、在目标DB中记录版本信息和当前状态,使得多人开发团队可以在多个DB环境上井然有序的工作,为敏捷和迭代开发提供强有力的技术保障。

        IDEA 中关于pom.xml配置

<build>
           <finalName>${project.artifactId}</finalName>
           <plugins>
                          <plugin>
                                       <groupId>org.mybatis.maven</groupId>
                                       <artifactId>migrations-maven-plugin</artifactId>
                                       <version>X.X</version>
                                       <configuration>
                                                   <repository>scripts/development</repository>
                                                   <skip>true</skip>
                                       </configuration>
                                       <dependencies>
                                                  <dependency>
                                                              <groupId>mysql</groupId>
                                                              <artifactId>mysql-connector-java</artifactId>
                                                              <version>5.1.35</version>
                                                              <scope>runtime</scope>
                                                   </dependency>
                                        </dependencies>
                                        <executions>
                                                   <execution>
                                                             <id>migration-chack</id>
                                                             <phase>test</phase>
                                                             <goals>
                                                                       <goal>up</goal>
                                                                       <goal>pending</goal>
                                                             </goals>
                                        </execution>
                              </executions>
                    </plugin>
           </plugins>
</build>


一、初始化migration repository

mvn migration:init —Dmigration.skip=false

根据上面配置的仓库地址,会默认在项目的根目录下生成scripts/development文件夹,然后会发现此文件夹下增加了drivers、environments、scripts目录,它们分别用来存放DB驱动、各个环境DB连接参数和DB变更脚本

(1)配置数据库驱动-可选

        将项目实际使用的数据库驱动jar拷贝到drivers目录下;

(2)配置数据库连接信息

        打开environments目录,编辑development.properties为Migrations指定开发环境DB的连接参数,修改时区为time_zone=GMT+8:00

      (参照development.properties创建uat.properties、sit.properties等用于指定uat环境和集成测试环境DB连接参数;

(3)数据初始状态

         scripts目录中的bootstrap.sql用于记录数据库的最原始的状态,即你在使用Migrations工具之前的数据库Scheme;

二、创建新的scripts脚本

mvn migration:new -Dmigration.description="my_first_schema_migration"
这样就在scripts/development/scripts文件目录下生成一个空的sql文件:20160630112233_my_first_schema_migration.sql,同时会在数据库生成默认的 changelog表记录导入日志

--// First migration.-- Migration SQL that makes the change goes here.--//@UNDO-- SQL to undo the change goes here.
三、查看文件状态

mvn migration:status -Dmigration.skip=false[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building test--项目名[INFO] ------------------------------------------------------------------------[INFO][INFO] --- migrations-maven-plugin:1.1.2:status (default-cli) @ **** ---[INFO] Executing  MyBatis Migration Schema StatusCommand[INFO] ID             Applied At          Description[INFO] ================================================================================[INFO] 20160630112233 ...pending...          my first schema migration[INFO][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.387 s[INFO] Finished at: 2016-06-30T19:27:25+08:00[INFO] Final Memory: 12M/153M[INFO] ------------------------------------------------------------------------
四、执行pending 状态的文件

mvn migration:up -Dmigration.skip=false[INFO] Executing  Apache Migration StatusCommand[INFO] ID             Applied At             Description[INFO] ================================================================================[INFO] [INFO] 20160630112233    2016-06-30 19:51:17    my first schema migration[INFO]
如果是多模块项目则path :
mvn migration:up -pl 模块名 -Dmigration.skip=false
相关地址:http://www.mybatis.org/migrations/index.html

1 0
原创粉丝点击