Skip to content

Commit 981e9d5

Browse files
committed
新增自定义DDL运行器.
1 parent e708290 commit 981e9d5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/content/docs/guides/auto-ddl.md

+25
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,28 @@ ddlScript.run(new StringReader("DELETE FROM user;\n" +
4949
在这个示例中,我们定义了一个`MysqlDdl`组件,它实现了`IDdl`接口,并提供了要执行的SQL脚本文件列表。通过调用`ShardingKey.change`方法,我们可以切换到mysql的从库,并使用`ddlScript.run`方法执行特定的SQL脚本。
5050

5151
通过这种方式,MyBatis-Plus提供了一个高效且自动化的方式来管理数据库的DDL操作,极大地简化了数据库结构的管理和维护工作。
52+
53+
## 自定义运行器
54+
55+
如果集成了MyBatis-Plus的starter的话,会自动实例化一个 DdlApplicationRunner 实例来执行 DDL 脚本。
56+
57+
执行方式为自动提交事务,且忽略错误继续执行(其他脚本参数见如下)。
58+
59+
如果需要自定义控制,请自行注入一个DdlApplicationRunner实例至容器。
60+
61+
```java
62+
@Bean
63+
public DdlApplicationRunner ddlApplicationRunner(List<IDdl> ddlList) {
64+
DdlApplicationRunner ddlApplicationRunner = new DdlApplicationRunner(ddlList);
65+
// 下面属性自 3.5.11 开始 ...
66+
ddlApplicationRunner.setAutoCommit(false); //关闭自动提交
67+
ddlApplicationRunner.setDdlScriptErrorHandler(DdlScriptErrorHandler.ThrowsErrorHandler.INSTANCE); // 设置错误处理方式为抛异常
68+
ddlApplicationRunner.setScriptRunnerConsumer(scriptRunner -> {
69+
scriptRunner.setLogWriter(null); // 关闭执行日志打印 默认: System.out
70+
scriptRunner.setErrorLogWriter(null); // 关闭错误日志打印 默认:System.err
71+
scriptRunner.setStopOnError(true); // 遇到异常是否停止
72+
scriptRunner.setRemoveCRs(false); // 是否替换\r\n 为 \n 默认: false
73+
});
74+
return ddlApplicationRunner;
75+
}
76+
```

0 commit comments

Comments
 (0)