Mybatis-Migrations 基本的使用方法与注意的地方

6 篇文章 0 订阅
4 篇文章 0 订阅

可参考的其他一些文章:

官网:http://mybatis.org/migrations/

网络:http://blog.csdn.net/daquan198163/article/details/9284611

http://wenku.baidu.com/view/0b2e4801a8114431b90dd83b.html

安装:

https://code.google.com/p/mybatis/wiki/Migration

http://mybatis.org/migrations/installation.html

MyBatis Migrations is a Java tool, so you must have Java installedin order to proceed

安装比较简单有下面4个步骤,不一一说明。

1.下载-2.解压-3.JAVA-4.环境变量

Migrate 操作

任意位置建立一个db目录,如:D:\PERA\migration\db

初始化

D:\PERA\migration\db>migrate init

------------------------------------------------------------------------

MyBatis Migrations - init

------------------------------------------------------------------------

Initializing: .

Creating: environments

Creating: scripts

Creating: drivers

Creating: README

Creating: development.properties

Creating: bootstrap.sql

Creating: 20131009084544_create_changelog.sql

Creating: 20131009084545_first_migration.sql

Done!

 

------------------------------------------------------------------------

MyBatis Migrations SUCCESS

Total time: 2s

Finished at: Wed Oct 09 16:45:45 CST 2013

Final Memory:2M/483M

查看db状态

D:\PERA\migration\db>migratestatus

------------------------------------------------------------------------

--MyBatis Migrations - status

------------------------------------------------------------------------

ID             Applied At          Description

================================================================================

20131009084544    ...pending...    create changelog

20131009084545    ...pending...    first migration

20131010010742    ...pending...    tan add table aa

20131010100011    ...pending...    tanqr add table pera

 

------------------------------------------------------------------------

--MyBatis Migrations SUCCESS

--Total time: 0s

--Finished at: Thu Oct 10 10:00:42 CST 2013

-- Final Memory: 21M/483M

 

 

Migrate new新建脚本

 

D:\PERA\migration\db>migrate new "pera add tabletan6"

------------------------------------------------------------------------

-- MyBatis Migrations - new

------------------------------------------------------------------------

Your migrations configuration did not find your customtemplate.  Using the d

Done!

 

------------------------------------------------------------------------

-- MyBatis Migrations SUCCESS

-- Total time: 1s

-- Finished at: Thu Oct 10 13:30:52 CST 2013

-- Final Memory: 2M/483M

------------------------------------------------------------------------

会生成脚本20131010133052_pera_add_table_tan6.sql

填写脚本内容:

修改20131010133052_pera_add_table_tan6.sql

 

--//tanqr add table pera

--Migration SQL that makes the change goes here.

 

 

createtable tan6 ( id number(30));

insertinto tan6 values(1011);

commit;

 

 

--//@UNDO

--SQL to undo the change goes here.

 

 

droptable tan6;

 

Migrate up

D:\PERA\migration\db>migrateup

 

------------------------------------------------------------------------

--MyBatis Migrations - up

------------------------------------------------------------------------

==========Applying: 20131010133052_pera_add_table_tan6.sql ================

--  tanqr add table pera

--Migration SQL that makes the change goes here.

createtable tan6 ( id number(30))

 

insertinto tan6 values(1011)

 

commit

 

 

------------------------------------------------------------------------

--MyBatis Migrations SUCCESS

--Total time: 0s

--Finished at: Thu Oct 10 13:31:16 CST 2013

--Final Memory: 24M/483M

------------------------------------------------------------------------

Migrate down

可以将脚本回滚至上一步

D:\PERA\migration\db>migrate down

------------------------------------------------------------------------

-- MyBatis Migrations - down

------------------------------------------------------------------------

========== Undoing:20131010133052_pera_add_table_tan6.sql ===============

-- @UNDO

-- SQL to undo the change goes here.

drop table tan6

 

 

------------------------------------------------------------------------

-- MyBatis Migrations SUCCESS

-- Total time: 0s

-- Finished at: Thu Oct 10 13:32:16 CST2013

-- Final Memory: 24M/483M

------------------------------------------------------------------------

Migrate version

可以回滚至执行过的任一时间

migrate script

可以将脚本打包

D:\PERA\migration\db>migratescript 20131010010742  20131010103007>update20131010.sql

 

Script后跟的是两个时间点,可以通过migrate status查看。

updata20131010.sql时自己启的名字,包括了这个时间段的所有内容。

完成后会在db根目录下生成该脚本。

Migrate bootstrap

D:\PERA\migration\dbqing>migratebootstrap

 

可以此命令初始化数据库

Pending处理

在多人处理时,可能会因为自己脚本创建的早,但是提交的迟,出现这种情况

D:\PERA\migration\user1>migratenew "611 erm create test3 tqr"

D:\PERA\migration\user1>migratestatus

------------------------------------------------------------------------

-- MyBatisMigrations - status

------------------------------------------------------------------------

ID             Applied At          Description

================================================================================

201310100827392013-10-10 17:23:49 create changelog

201310100827402013-10-10 17:23:49 first migration

201310110910242013-10-11 09:24:31 611 pera peraq add table qform

201310111358012013-10-11 14:00:47 611 erm dml test1

201310111359062013-10-11 14:01:17 611 erm create table test1 tqr

201310111359422013-10-11 14:01:17 611 erm dml test1 tqr

201310111415362013-10-11 15:30:42 611 erm create table test2 tqr

20131011153334    ...pending...    611 erm create test3 tqr

201310111534432013-10-11 15:35:04 611 erm create test4 tqr

 

此时migrateup则不会去执行

 

D:\PERA\migration\user1>migrateup

------------------------------------------------------------------------

-- MyBatisMigrations - up

------------------------------------------------------------------------

------------------------------------------------------------------------

-- MyBatisMigrations SUCCESS

-- Total time: 0s

-- Finished at: FriOct 11 15:36:26 CST 2013

-- Final Memory: 24M/483M

 

需要使用migratepending来处理

D:\PERA\migration\user1>migrate pending

------------------------------------------------------------------------

-- MyBatisMigrations - pending

------------------------------------------------------------------------

WARNING: Runningpending migrations out of order can create unexpected results.

==========Applying: 20131011153334_611_erm_create_test3_tqr.sql ===============

--  611 erm create test3 tqr

-- Migration SQLthat makes the change goes here.

 

------------------------------------------------------------------------

-- MyBatisMigrations SUCCESS

-- Total time: 1s

-- Finished at: FriOct 11 15:37:03 CST 2013

-- Final Memory:24M/483M

------------------------------------------------------------------------

再看状态

D:\PERA\migration\user1>migratestatus

-----------------------------------------------------------------------

-- MyBatisMigrations - status

-----------------------------------------------------------------------

ID             Applied At          Description

=======================================================================

201310100827392013-10-10 17:23:49 create changelog

201310100827402013-10-10 17:23:49 first migration

201310110910242013-10-11 09:24:31 611 pera peraq add table qform

201310111358012013-10-11 14:00:47 611 erm dml test1

201310111359062013-10-11 14:01:17 611 erm create table test1 tqr

201310111359422013-10-11 14:01:17 611 erm dml test1 tqr

201310111415362013-10-11 15:30:42 611 erm create table test2 tqr

201310111533342013-10-11 15:37:03 611 erm create test3 tqr

201310111534432013-10-11 15:35:04 611 erm create test4 tqr

 

-----------------------------------------------------------------------

-- MyBatisMigrations SUCCESS

-- Total time: 0s

-- Finished at: FriOct 11 15:37:08 CST 2013

-- Final Memory:24M/483M

-----------------------------------------------------------------------

 

 

需要注意的地方(初步使用的总结,可以纠正补充):
不建议修改migrate up成功的脚本。

1.      Migrate up成功的脚本,修改后,再次migrateup时不会去执行。但migrate down时会执行修改后的脚本。所以建议migrate up成功后不要对脚本再进行修改,如有变更用新脚本。详见《脚本修改migrate up down演示》

Migrate up不成功的问题

1.Migrate up没有成功的脚本,再次migrate up 时还会去执行。

2.当migrate up时,脚本中间有误时,会执行前段正确的,然后报错,后面的部分不会再执行。但migrate down时不会滚,却会回滚上一次操作,详见演示《错误1:事物性错误》

 

基于以上问题,建议1个脚本不要执行过多的DDL操作,如果是DML操作,中间不要有commit;

Bootstrap

如果执行了migrate up,将不能再次执行。

Migrate Bootstrap –force

 

bootstrap执行时,即使中间有错,抛出错误后仍然能继续执行后面的脚本。

 

1.       关于db路径

虽然多个项目,多个用户可以放到1个目录下,可以通过添加—path –env的方式可以区分,但是脚本会混在一起,可能有以下潜在问题。

1)      管理比较混乱。

2)      而且对于具体的某个开发人员,他可能更新的模块数据有限,甚至只有1个,自己的目录下不需要别的模块、别的项目。

3)      开发人员权限过大,有可能会误操作别的模块,别的项目。

建议1个项目(1个库)的一个schema建1个db路径。例:如有2个库,每个库3个用户的话,需要建6个db路径。

2.       脚本内容

对DDL操作,最好1个脚本1条操作;

对于DML操作,中间不要有commit,到整个操作完成再加commit;

 

 

不能执行匿名块

脚本中不能有像以下的内容。

IF unix > win THEN

   datadir := SUBSTR (uname, 1, unix);

ELSE

   datadir := SUBSTR (wname, 1, win);

END IF;

 

dbms_output.put_line ('datadir = ' || datadir);

 

END;

/

 

不能执行sqlplus命令

 

 

脚本合并
内容与migrate脚本标准一样

手写3个命名时间精确到秒的脚本

 

 

注意:这3个脚本未实际执行,也非migrate new新建,是纯手工新建。

 

--// pera1

-- Migration SQL that makes the change goeshere.

 

crate table pera1 (id number(30));

 

 

--//@UNDO

-- SQL to undo the change goes here.

 

drop table pera1

 

 

合并后的内容:

------------------------------------------------------------------------

-- MyBatis Migrations - script

------------------------------------------------------------------------

-- 20131010135226_pera2.sql

-- pera2

-- Migration SQL that makes the change goeshere.

 

crate table pera2 (id number(30));

 

 

 

 

INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010135226, '2013-10-10 14:43:01', 'pera2');

 

-- 20131010143333_pera3.sql

-- pera3

-- Migration SQL that makes the change goeshere.

 

crate table pera3 (id number(30));

 

 

 

 

INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010143333, '2013-10-10 14:43:01', 'pera3');

 

------------------------------------------------------------------------

-- MyBatis Migrations SUCCESS

-- Total time: 0s

-- Finished at: Thu Oct 10 14:43:01 CST2013

-- FinalMemory: 2M/483M

 

可见脚本能够正常合并。

 

合并后的脚本可以在另一台机器的bootstrap.sql执行

内容与migrate脚本标准不一样

内容类似于下面这样:

 

 

create table pera11 (id number(30));

 

 

D:\PERA\migration\db>migrate statu

------------------------------------------------------------------------

-- MyBatis Migrations - statu

------------------------------------------------------------------------

ID             Applied At          Description

================================================================================

20131009084544    ...pending...    create changelog

20131009084545    ...pending...    first migration

20131010151000    ...pending...    pera11

20131010152000    ...pending...    pera12

20131010153000    ...pending...    pera13

 

------------------------------------------------------------------------

-- MyBatis Migrations SUCCESS

-- Total time: 0s

-- Finished at: Thu Oct 10 15:40:49 CST2013

-- Final Memory: 24M/483M

------------------------------------------------------------------------

 

 

D:\PERA\migration\db>migrate script20131009084545 20131010153000 > release2.sql

 

 

内容:

 

------------------------------------------------------------------------

-- MyBatis Migrations - script

------------------------------------------------------------------------

-- 20131010151000_pera11.sql

 

create table pera11 (id number(30));

 

 

 

INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010151000, '2013-10-10 15:41:44', 'pera11');

 

-- 20131010152000_pera12.sql

 

create table pera12 (id number(30));

 

 

 

INSERT INTO CHANGELOG (ID, APPLIED_AT, DESCRIPTION)VALUES (20131010152000, '2013-10-10 15:41:44', 'pera12');

 

-- 20131010153000_pera13.sql

 

create table pera11 (id number(30));

 

 

 

INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010153000, '2013-10-10 15:41:44', 'pera13');

 

------------------------------------------------------------------------

-- MyBatis Migrations SUCCESS

-- Total time: 0s

-- Finished at: Thu Oct 10 15:41:44 CST2013

-- Final Memory: 2M/483M

------------------------------------------------------------------------

建议:

 

关于version

不建议DBA使用version迁移开发库到指定版本,该命令更多的是用在数据库回滚,如果开发人员写的回滚命令有问题,则会出错。如果如生成较早的脚本,建议使用script去发布生成。

开发人员回滚操作。

也不建议开发人员使用version回滚,如果version中间有别人的更新,有可能一起回滚了。

开发人员可以使用up回滚当前操作,但是一定要检查回滚命令的正确性。

如果脚本up时出错,但是脚本中在出错的SQL前有正确的SQL应用到了库,此时migrations认为该脚本没有执行,状态为pending,无法使用migratedown回滚,只能通过在数据库直接写SQL的方法回退。

 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是MyBatis的增强工具,提供了很多实用的功能,其中包括对关联查询的支持。MyBatis-Plus的关联查询主要通过Wrapper对象来实现,下面就来详细介绍一下MyBatis-Plus的关联查询的使用方法。 1. 基本概念 在使用MyBatis-Plus的关联查询之前,我们需要先了解一些基本概念。 1.1. 实体类 实体类是指与数据库表对应的Java类。在使用MyBatis-Plus的关联查询时,我们需要定义实体类,并在实体类中定义与其他表关联的属性。 1.2. Wrapper对象 Wrapper对象是MyBatis-Plus中用于构建SQL语句的查询条件对象,可以通过Wrapper对象实现关联查询。 1.3. QueryWrapper QueryWrapper是Wrapper的一种实现,用于构建查询条件。它可以通过链式调用的方式来构建查询条件,支持多种查询方式,包括等于、不等于、大于、小于、模糊查询等。 1.4. LambdaQueryWrapper LambdaQueryWrapper是QueryWrapper的一种升级版,它支持使用Lambda表达式来构建查询条件,使用起来更加简单、方便。 2. 关联查询的使用方法 了解了基本概念之后,下面就来介绍一下MyBatis-Plus的关联查询的使用方法。 2.1. 一对一关联查询 一对一关联查询是指两张表之间的关联关系是一对一的,例如用户表和身份证表之间的关联关系。 在MyBatis-Plus中,一对一关联查询可以通过selectJoin方法来实现。例如,查询用户表和身份证表中的所有数据,可以使用如下代码: ``` List<User> userList = userMapper.selectJoin(null); ``` 其中,userMapper是MyBatis的Mapper接口,User是用户表对应的实体类。在selectJoin方法中传入的参数为null,表示查询所有数据。 如果需要在查询时指定查询条件,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,例如: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("user_name", "张三"); List<User> userList = userMapper.selectJoin(wrapper); ``` 上述代码中,使用QueryWrapper构建了一个查询条件,查询条件为用户名为“张三”,然后调用selectJoin方法来查询数据。 2.2. 一对多关联查询 一对多关联查询是指两张表之间的关联关系是一对多的,例如用户表和订单表之间的关联关系。 在MyBatis-Plus中,一对多关联查询可以通过selectList方法来实现。例如,查询用户表和订单表中的所有数据,可以使用如下代码: ``` List<User> userList = userMapper.selectList(null); for (User user : userList) { List<Order> orderList = user.getOrderList(); // 处理订单数据 } ``` 在查询用户表数据时,通过getUserList方法获取到了用户对应的订单数据,然后可以对订单数据进行处理。 如果需要在查询时指定查询条件,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,例如: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("user_name", "张三"); List<User> userList = userMapper.selectList(wrapper); ``` 上述代码中,使用QueryWrapper构建了一个查询条件,查询条件为用户名为“张三”,然后调用selectList方法来查询数据。 2.3. 多对多关联查询 多对多关联查询是指两张表之间的关联关系是多对多的,例如学生表和课程表之间的关联关系。 在MyBatis-Plus中,多对多关联查询需要通过中间表来实现。假设学生表和课程表之间的关联关系是通过中间表“学生课程表”来实现的,那么学生表和课程表中的所有数据可以使用如下代码: ``` List<Student> studentList = studentMapper.selectList(null); for (Student student : studentList) { List<Course> courseList = student.getCourseList(); // 处理课程数据 } ``` 在查询学生表数据时,通过getCourseList方法获取到了学生所选的课程数据,然后可以对课程数据进行处理。 如果需要在查询时指定查询条件,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,例如: ``` QueryWrapper<Student> wrapper = new QueryWrapper<>(); wrapper.eq("student_name", "张三"); List<Student> studentList = studentMapper.selectList(wrapper); ``` 上述代码中,使用QueryWrapper构建了一个查询条件,查询条件为学生名为“张三”,然后调用selectList方法来查询数据。 3. 总结 MyBatis-Plus的关联查询主要通过Wrapper对象来实现,支持一对一、一对多和多对多关联查询。在使用关联查询时,需要定义实体类,并在实体类中定义与其他表关联的属性。同时,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,在查询时指定查询条件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值