update docs

This commit is contained in:
开源海哥 2023-04-01 11:28:17 +08:00
parent 0a4050d904
commit 461300eaf7
4 changed files with 104 additions and 48 deletions

View File

@ -1,4 +1,4 @@
# Row + Db 工具的使用
# Db + Row 工具的使用
Db + Row 工具类,提供了在 Entity 实体类之外的数据库操作能力。使用 Db + Row 时,无需对数据库表进行映射, Row 是一个 HashMap 的子类,相当于一个通用的 Entity。以下为 Db + Row 的一些示例:
@ -92,36 +92,3 @@ row.set(ACCOUNT.USER_NAME,"Michael");
Db.insert("tb_account",row);
```
## Db 中的 RowMapperInvoker
在 Db.java 工具类中,内置了一个静态的 RowMapperInvoker 对象,用来真正的与 Mybatis 交互执行
SQL在一个应用中若有多个数据源没有让每个数据源持有一个 RowMapperInvoker 对象。
```java
//environmentId 为 mybatis 中的 environment 的 id
RowMapperInvoker myDb = Db.invoker("environmentId");
Row row = myDb.selectOneById("tb_account","id",1);
```
## Db 的事务管理
在 Db 的 RowMapperInvoker 中,有一个对象为 rowSessionManager我们可以通过设置 rowSessionManager
来管理自己的事务。
在 Spring 中,我们可以通过一下代码让 Spring 来管理 Db 操作事务:
```java
Db.invoker().setRowSessionManager(new SpringRowSessionManager());
// 或者可以自定义自己的 RowSessionManager 来管理事务
Db.invoker().setRowSessionManager(new RowSessionManager() {
@Override
public SqlSession getSqlSession(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
return SqlSessionUtils.getSqlSession(sqlSessionFactory, executorType, null);
}
@Override
public void releaseSqlSession(SqlSession sqlSession, SqlSessionFactory sqlSessionFactory) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
});
```

View File

@ -47,7 +47,7 @@ System.out.println(rows);
通过编码的方式,切换到数据源 `ds2`
```java
```java 2,6
try{
DataSourceKey.use("ds2")
List<Row> rows = Db.selectAll("tb_account");
@ -67,10 +67,103 @@ List<Row> rows = DataSourceKey.use("ds2"
## 数据源切换(设置)
MyBatis-Flex 提供了 3 种方式来配置数据源:
- 1、编码使用` DataSourceKey.use` 方法。
- 1、编码使用`DataSourceKey.use` 方法。
- 2、`@UseDataSource("dataSourceName")` 在 Mapper 方法上,添加注解,用于指定使用哪个数据源。
- 3、`@Table(dataSource="dataSourceName")` 在 Entity 类上添加注解,该 Entity 的增删改查请求默认使用该数据源。
`DataSourceKey.use` 示例:
```java 2,6
try{
DataSourceKey.use("ds2")
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);
}finally{
DataSourceKey.clear();
}
```
`@UseDataSource("dataSourceName")` 示例:
```java 3
interface AccountMapper extends BaseMapper{
@UseDataSource("ds2")
List<Account> myMethod();
}
```
`@Table(dataSource="dataSourceName")` 示例:
```java 1
@Table(value = "tb_account", dataSource = "ds2")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
}
```
::: tip 数据源配置的优先级
`DataSourceKey.use()` > `@UseDataSource()` > `@Table(dataSource="...")`
:::
## 更多的 Spring Yaml 配置支持
```yaml
mybatis-flex:
datasource:
ds1:
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
ds2:
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456
```
在以上配置中,`ds1``ds2` 并未指定使用哪个数据连接池MyBatis-Flex 会 **自动探测** 当前项目依赖了哪个连接池。
目前支持了 `druid``hikaricp``dbcp2``beecp` 的自动探测,如果项目中使用的不是这 4 种类型只有,需要添加 `type` 配置,如下所示:
```yaml 4,9
mybatis-flex:
datasource:
ds1:
type: com.your.datasource.type1
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
ds2:
type: com.your.datasource.type2
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456
```
同时,项目若使用到了多个数据源类型,则也需要添加 `type` 来指定当前数据源的类型。
除了 `type``url``username``password` 的配置以为MyBatis-Flex 支持该 `DataSource` 类型的所有参数配置,
例如,在 `DruidDataSource` 类中存在 `setAsyncInit` 方法,我们就可以添加 `asyncInit` 的配置,如下所示:
```yaml 8
mybatis-flex:
datasource:
ds1:
type: druid
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
asyncInit: true
ds2:
type: com.your.datasource.type2
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456
```
因此,只要该 `DataSource` 有 setter 方法,我们就可以在配置文件中进行配。与此相反的是:若该 `DataSource` 类型没有该属性,则不能使用这个配置。
::: tip 提示
在数据源的配置中,`type` 可以配置为某个 DataSource 的类名,也可以配置为别名,别名支持有:`druid`
`hikari``hikaricp``bee``beecp``dbcp``dbcp2`
:::

View File

@ -3,18 +3,15 @@
MyBatis-Flex 提供了一个名为 `Db.tx()` 的方法<Badge type="tip" text="^1.0.6" />,用于进行事务管理,以下是示例代码:
```java
Db.tx(new Supplier<Boolean>() {
@Override
public Boolean get() {
Db.tx(() -> {
//进行事务操作
//进行事务操作
return true;
}
return true;
});
```
`tx()` 方法抛出异常,或者返回 false或者返回 null则回滚事务。只有正常返回 true 的时候,正常进行事务提交。
`tx()` 方法抛出异常,或者返回 false或者返回 null则回滚事务。只有正常返回 true 的时候,进行事务提交。
## 嵌套事务
@ -31,15 +28,14 @@ Db.tx(() -> {
return true;
});
if(success)...
return true;
});
```
支持无限极嵌套
支持无限极嵌套,嵌套事务之间不会相互影响。
## 特征
- 1、支持嵌套事务
- 2、支持多数据源注意在多数据源的情况下所有数据源的请求Connection会执行相同的 commit 或者 rollback但并非原子操作。
- 2、支持多数据源注意在多数据源的情况下所有数据源的数据库请求Connection会执行相同的 commit 或者 rollback但并非原子操作。

View File

@ -8,7 +8,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Table("tb_account")
@Table(value = "tb_account",dataSource = "ds2")
public class Account {
@Id(keyType = KeyType.Auto)