doc: update docs

This commit is contained in:
开源海哥 2023-08-02 19:22:40 +08:00
parent 63a849c458
commit f6e618d3ec
4 changed files with 66 additions and 33 deletions

View File

@ -145,6 +145,16 @@ QueryChain.of(accountMapper)
.list();
```
以上代码执行的 SQL 如下:
```sql
select tb_account.*
, max(tb_account.age) as maxAge
, avg(tb_account.age) as avgAge
where tb_account.id >= 100
```
## 多表映射
假设我们定义了一个 `BootVo.java`,其中包含了图书的基本信息,也包含了图书归属的用户信息,例如:

View File

@ -39,12 +39,33 @@ Db.executeBatch(accounts.size(), 1000, AccountMapper.class, (mapper, index) -> {
mapper.insert(account);
});
```
或者
```java
List<Account> accounts = ....
Db.executeBatch(accounts, 1000, AccountMapper.class, (mapper, account) -> {
mapper.insert(account);
});
```
`Db.executeBatch` 是通过 JDBC 的 `Statement.executeBatch()` 进行批量执行;这个在大批量数据执行的时候,效率要比 `BaseMapper.insertBatch` 高出许多;
::: tip 提示
我看到有一些同学担心 `BaseMapper.insertBatch` 被误用,在 `IService` 中通过使用 `Db.executeBatch` 重写了 Service 的 `insertBatch` 方法,这也是没问题的。但还是需要明白,
`BaseMapper.insertBatch``Db.executeBatch` 的底层实现差异,以及不同的使用场景。
:::
IService 很多批量操作的方法,也都是通过 `Db.executeBatch` 进行封装的,大家也可以通过其扩展出自己的 "批量操作" 方法来。比如这是一个批量忽略 `null` 的插入示例:
```java
public boolean saveBatchSelective(Collection<Account> entities) {
int[] result = Db.executeBatch(entities, 1000,
AccountMapper.class, BaseMapper::insertSelective);
return SqlUtil.toBool(result);
}
```
## `Db.updateBatch` 方法
@ -52,7 +73,8 @@ Db.executeBatch(accounts.size(), 1000, AccountMapper.class, (mapper, index) -> {
```java
List<Account> accounts = ....
String sql = "insert into tb_account(user_name,age,birthday) values (?,?,?)";
String sql = "insert into tb_account(user_name, age, birthday) " +
"values (?, ?, ?)";
Db.updateBatch(sql, new BatchArgsSetter() {
@Override
public int getBatchSize() {

View File

@ -1,6 +1,6 @@
# 链式操作
在 MyBatis-Flex 中,内置了 `QueryChain.java` `UpdateChain.java` 用于对数据进行链式查询操作和链式数据操作(修改和删除)。
在 MyBatis-Flex 中,内置了 `QueryChain.java` `UpdateChain.java` 以及 `DbChain.java` 用于对数据进行链式查询操作和链式操作(修改和删除)。
- **QueryChain**:链式查询
- **UpdateChain**:链式更新

View File

@ -91,12 +91,12 @@ WHERE account_id IN (1, 2, 3, 4, 5)
**注意事项 1**
在以上的 `@RelationOneToOne` 注解配置中,若 `IDCard.java` 是一个没有 `@Table` 注解修饰的实体类,
在以上的 `@RelationOneToOne` 注解中,若 `IDCard.java` 是 VO、DTO 等,而不是一个带有 `@Table` 注解的 Entity 类,
则需要在 `@RelationOneToOne` 配置上 `targetTable` 用于指定查询的表名。
假设 `IDCard.java` 没有 `@Table` 注解修饰(比如 vo 或 dto 等),配置如下
```java 9
例如
```java 10
public class Account implements Serializable {
@Id(keyType = KeyType.Auto)
@ -104,6 +104,7 @@ public class Account implements Serializable {
private String userName;
// 假设 IDCard 类是 vo 或者 dto需要配置 targetTable
@RelationOneToOne(selfField = "id", targetField = "accountId"
, targetTable = "tb_idcard")
private IDCard idCard;
@ -115,7 +116,7 @@ public class Account implements Serializable {
**注意事项 2**
`Account.java``IDCard.java` 示例中,若他们的关联关系是通过 **中间表** 的方式进行关联,则需要添加
`joinTable` `joinSelfColumn` `joinTargetColumn` 配置,如下所示:
`joinTable` `joinSelfColumn` `joinTargetColumn` 配置,如下所示:
```java 9,10,11
public class Account implements Serializable {