mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
update docs
This commit is contained in:
parent
ae8e02de66
commit
0a4050d904
@ -51,6 +51,7 @@ export default defineConfig({
|
||||
{ text: '数据脱敏', link: '/zh/mask' },
|
||||
{ text: 'SQL 审计', link: '/zh/audit' },
|
||||
{ text: '多数据源', link: '/zh/multi-datasource' },
|
||||
{ text: '事务管理', link: '/zh/tx' },
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@ -18,7 +18,59 @@ mybatis-flex:
|
||||
|
||||
在以上配置中,`ds1` 和 `ds2` 是由用户自定义的,我们可以理解为数据源的名称,或者数据源的 `key`,这个在动态切换数据库中非常有用。
|
||||
|
||||
## 通过注解指定数据源
|
||||
在无 Spring 框架的场景下,代码如下:
|
||||
|
||||
```java
|
||||
DataSource dataSource1 = new HikariDataSource();
|
||||
dataSource1.setJdbcUrl(....)
|
||||
|
||||
## 编码动态切换数据源
|
||||
DataSource dataSource2 = new HikariDataSource();
|
||||
dataSource2.setJdbcUrl(....)
|
||||
|
||||
DataSource dataSource3 = new HikariDataSource();
|
||||
dataSource3.setJdbcUrl(....)
|
||||
|
||||
MybatisFlexBootstrap.getInstance()
|
||||
.setDataSource("ds1", dataSource1)
|
||||
.addDataSource("ds2", dataSource2)
|
||||
.addDataSource("ds3", dataSource3)
|
||||
.start();
|
||||
```
|
||||
## 开始使用
|
||||
|
||||
默认使用第一个配置的数据源:
|
||||
|
||||
```java
|
||||
List<Row> rows = Db.selectAll("tb_account");
|
||||
System.out.println(rows);
|
||||
```
|
||||
|
||||
通过编码的方式,切换到数据源 `ds2`:
|
||||
|
||||
```java
|
||||
try{
|
||||
DataSourceKey.use("ds2")
|
||||
List<Row> rows = Db.selectAll("tb_account");
|
||||
System.out.println(rows);
|
||||
}finally{
|
||||
DataSourceKey.clear();
|
||||
}
|
||||
```
|
||||
|
||||
或者
|
||||
|
||||
```java
|
||||
List<Row> rows = DataSourceKey.use("ds2"
|
||||
, () -> Db.selectAll("tb_account"));
|
||||
```
|
||||
|
||||
## 数据源切换(设置)
|
||||
|
||||
MyBatis-Flex 提供了 3 种方式来配置数据源:
|
||||
- 1、编码,使用` DataSourceKey.use` 方法。
|
||||
- 2、`@UseDataSource("dataSourceName")` 在 Mapper 方法上,添加注解,用于指定使用哪个数据源。
|
||||
- 3、`@Table(dataSource="dataSourceName")` 在 Entity 类上添加注解,该 Entity 的增删改查请求默认使用该数据源。
|
||||
|
||||
::: tip 数据源配置的优先级
|
||||
`DataSourceKey.use()` > `@UseDataSource()` > `@Table(dataSource="...")`
|
||||
:::
|
||||
|
||||
45
docs/zh/tx.md
Normal file
45
docs/zh/tx.md
Normal file
@ -0,0 +1,45 @@
|
||||
# 事务管理
|
||||
|
||||
MyBatis-Flex 提供了一个名为 `Db.tx()` 的方法<Badge type="tip" text="^1.0.6" />,用于进行事务管理,以下是示例代码:
|
||||
|
||||
```java
|
||||
Db.tx(new Supplier<Boolean>() {
|
||||
@Override
|
||||
public Boolean get() {
|
||||
|
||||
//进行事务操作
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
若 `tx()` 方法抛出异常,或者返回 false,或者返回 null,则回滚事务。只有正常返回 true 的时候,正常进行事务提交。
|
||||
|
||||
|
||||
## 嵌套事务
|
||||
|
||||
示例代码:
|
||||
|
||||
```java
|
||||
Db.tx(() -> {
|
||||
|
||||
//进行事务操作
|
||||
|
||||
boolean success = Db.tx(() -> {
|
||||
//另一个事务的操作
|
||||
return true;
|
||||
});
|
||||
|
||||
if(success)...
|
||||
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
支持无限极嵌套;
|
||||
|
||||
## 特征
|
||||
|
||||
- 1、支持嵌套事务
|
||||
- 2、支持多数据源(注意:在多数据源的情况下,所有数据源的请求(Connection)会执行相同的 commit 或者 rollback,但并非原子操作。)
|
||||
@ -195,6 +195,11 @@ public class MybatisFlexBootstrap {
|
||||
return this;
|
||||
}
|
||||
|
||||
public MybatisFlexBootstrap setDataSource(String dataSourceKey, DataSource dataSource) {
|
||||
this.dataSource = new FlexDataSource(dataSourceKey, dataSource);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MybatisFlexBootstrap addDataSource(String dataSourceKey, DataSource dataSource) {
|
||||
if (this.dataSource == null) {
|
||||
this.dataSource = new FlexDataSource(dataSourceKey, dataSource);
|
||||
|
||||
@ -48,11 +48,14 @@ public class MultiDataSourceTester {
|
||||
.start();
|
||||
|
||||
//默认查询 db1
|
||||
List<Row> rows = Db.selectAll("tb_account");
|
||||
System.out.println(rows);
|
||||
List<Row> rows1 = Db.selectAll("tb_account");
|
||||
System.out.println(rows1);
|
||||
|
||||
System.out.println("------");
|
||||
|
||||
List<Row> rows = DataSourceKey.use("ds2"
|
||||
, () -> Db.selectAll("tb_account"));
|
||||
|
||||
|
||||
//查询数据源 ds2
|
||||
DataSourceKey.use("ds2");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user