mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
doc: update docs
This commit is contained in:
parent
63a849c458
commit
f6e618d3ec
@ -145,6 +145,16 @@ QueryChain.of(accountMapper)
|
|||||||
.list();
|
.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`,其中包含了图书的基本信息,也包含了图书归属的用户信息,例如:
|
假设我们定义了一个 `BootVo.java`,其中包含了图书的基本信息,也包含了图书归属的用户信息,例如:
|
||||||
|
|||||||
@ -23,7 +23,7 @@ insert into tb_account(id,nickname, .....) values
|
|||||||
(104,"miachel104", ....),
|
(104,"miachel104", ....),
|
||||||
(105,"miachel105", ....);
|
(105,"miachel105", ....);
|
||||||
```
|
```
|
||||||
这种有一个特点:在小批量数据执行插入的时候,效率是非常高;但是当数据列表过多时,其生成的 SQL 可能会非常大, 这个大的 SQL
|
这种有一个特点:在小批量数据执行插入的时候,效率是非常高;但是当数据列表过多时,其生成的 SQL 可能会非常大, 这个大的 SQL
|
||||||
在传输和执行的时候就会变得很慢了。
|
在传输和执行的时候就会变得很慢了。
|
||||||
|
|
||||||
因此,`BaseMapper.insertBatch` 方法只适用于在小批量数据插入的场景,比如 100 条数据以内。
|
因此,`BaseMapper.insertBatch` 方法只适用于在小批量数据插入的场景,比如 100 条数据以内。
|
||||||
@ -39,45 +39,67 @@ Db.executeBatch(accounts.size(), 1000, AccountMapper.class, (mapper, index) -> {
|
|||||||
mapper.insert(account);
|
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` 高出许多;
|
`Db.executeBatch` 是通过 JDBC 的 `Statement.executeBatch()` 进行批量执行;这个在大批量数据执行的时候,效率要比 `BaseMapper.insertBatch` 高出许多;
|
||||||
|
|
||||||
::: tip 提示
|
IService 很多批量操作的方法,也都是通过 `Db.executeBatch` 进行封装的,大家也可以通过其扩展出自己的 "批量操作" 方法来。比如这是一个批量忽略 `null` 的插入示例:
|
||||||
我看到有一些同学担心 `BaseMapper.insertBatch` 被误用,在 `IService` 中通过使用 `Db.executeBatch` 重写了 Service 的 `insertBatch` 方法,这也是没问题的。但还是需要明白,
|
|
||||||
`BaseMapper.insertBatch` 和 `Db.executeBatch` 的底层实现差异,以及不同的使用场景。
|
|
||||||
:::
|
|
||||||
|
|
||||||
## `Db.updateBatch` 方法
|
```java
|
||||||
|
public boolean saveBatchSelective(Collection<Account> entities) {
|
||||||
|
|
||||||
|
int[] result = Db.executeBatch(entities, 1000,
|
||||||
|
AccountMapper.class, BaseMapper::insertSelective);
|
||||||
|
|
||||||
|
return SqlUtil.toBool(result);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## `Db.updateBatch` 方法
|
||||||
|
|
||||||
这个方法的示例代码如下:
|
这个方法的示例代码如下:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
List<Account> accounts = ....
|
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() {
|
Db.updateBatch(sql, new BatchArgsSetter() {
|
||||||
@Override
|
@Override
|
||||||
public int getBatchSize() {
|
public int getBatchSize() {
|
||||||
return accounts.size();
|
return accounts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object[] getSqlArgs(int index) {
|
public Object[] getSqlArgs(int index) {
|
||||||
Account account = accounts = accounts.get(index);
|
Account account = accounts = accounts.get(index);
|
||||||
Object[] args = new Object[3];
|
Object[] args = new Object[3];
|
||||||
args[0] = account.getUserName;
|
args[0] = account.getUserName;
|
||||||
args[1] = account.getAge();
|
args[1] = account.getAge();
|
||||||
args[2] = new Date();
|
args[2] = new Date();
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
虽然这个方法叫 `updateBatch`,但一样可以执行 `insert`、`delete`、`update` 等任何 SQL; 这个方法类似 Spring 的 `jdbcTemplate.batchUpdate()` 方法。
|
虽然这个方法叫 `updateBatch`,但一样可以执行 `insert`、`delete`、`update` 等任何 SQL; 这个方法类似 Spring 的 `jdbcTemplate.batchUpdate()` 方法。
|
||||||
|
|
||||||
|
|
||||||
## `Db.updateEntitiesBatch` 方法
|
## `Db.updateEntitiesBatch` 方法
|
||||||
这个方法用于批量根据 id 更新 entity,其是对 `Db.executeBatch` 的封装,使用代码如下:
|
这个方法用于批量根据 id 更新 entity,其是对 `Db.executeBatch` 的封装,使用代码如下:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
List<Account> accounts = ....
|
List<Account> accounts = ....
|
||||||
Db.updateEntitiesBatch(accounts, 1000);
|
Db.updateEntitiesBatch(accounts, 1000);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# 链式操作
|
# 链式操作
|
||||||
|
|
||||||
在 MyBatis-Flex 中,内置了 `QueryChain.java` 和 `UpdateChain.java` 用于对数据进行链式查询操作和链式数据操作(修改和删除)。
|
在 MyBatis-Flex 中,内置了 `QueryChain.java` 、 `UpdateChain.java` 以及 `DbChain.java` 用于对数据进行链式查询操作和链式操作(修改和删除)。
|
||||||
|
|
||||||
- **QueryChain**:链式查询
|
- **QueryChain**:链式查询
|
||||||
- **UpdateChain**:链式更新
|
- **UpdateChain**:链式更新
|
||||||
@ -240,15 +240,15 @@ ArticleVo articleVo = articleService.queryChain()
|
|||||||
// 新增 Row 构建
|
// 新增 Row 构建
|
||||||
DbChain.table("tb_account")
|
DbChain.table("tb_account")
|
||||||
.set(RowKey.AUTO)
|
.set(RowKey.AUTO)
|
||||||
.set("user_name","王帅")
|
.set("user_name", "王帅")
|
||||||
.set("age",18)
|
.set("age", 18)
|
||||||
.set("birthday",new Date())
|
.set("birthday", new Date())
|
||||||
.save();
|
.save();
|
||||||
|
|
||||||
// 查询 QueryWrapper 构建
|
// 查询 QueryWrapper 构建
|
||||||
DbChain.table("tb_account")
|
DbChain.table("tb_account")
|
||||||
.select("id","user_name","age","birthday")
|
.select("id", "user_name", "age", "birthday")
|
||||||
.where("age > ?",18)
|
.where("age > ?", 18)
|
||||||
.list()
|
.list()
|
||||||
.forEach(System.out::println);
|
.forEach(System.out::println);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -91,12 +91,12 @@ WHERE account_id IN (1, 2, 3, 4, 5)
|
|||||||
|
|
||||||
**注意事项 1:**
|
**注意事项 1:**
|
||||||
|
|
||||||
在以上的 `@RelationOneToOne` 注解配置中,若 `IDCard.java` 是一个没有 `@Table` 注解修饰的实体类,
|
在以上的 `@RelationOneToOne` 注解中,若 `IDCard.java` 是 VO、DTO 等,而不是一个带有 `@Table` 注解的 Entity 类,
|
||||||
则需要在 `@RelationOneToOne` 配置上 `targetTable` 用于指定查询的表名。
|
则需要在 `@RelationOneToOne` 配置上 `targetTable` 用于指定查询的表名。
|
||||||
|
|
||||||
|
|
||||||
假设 `IDCard.java` 没有 `@Table` 注解修饰(比如 vo 或 dto 等),配置如下:
|
例如:
|
||||||
```java 9
|
```java 10
|
||||||
public class Account implements Serializable {
|
public class Account implements Serializable {
|
||||||
|
|
||||||
@Id(keyType = KeyType.Auto)
|
@Id(keyType = KeyType.Auto)
|
||||||
@ -104,6 +104,7 @@ public class Account implements Serializable {
|
|||||||
|
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
|
// 假设 IDCard 类是 vo 或者 dto,需要配置 targetTable
|
||||||
@RelationOneToOne(selfField = "id", targetField = "accountId"
|
@RelationOneToOne(selfField = "id", targetField = "accountId"
|
||||||
, targetTable = "tb_idcard")
|
, targetTable = "tb_idcard")
|
||||||
private IDCard idCard;
|
private IDCard idCard;
|
||||||
@ -115,7 +116,7 @@ public class Account implements Serializable {
|
|||||||
**注意事项 2:**
|
**注意事项 2:**
|
||||||
|
|
||||||
在 `Account.java` 和 `IDCard.java` 示例中,若他们的关联关系是通过 **中间表** 的方式进行关联,则需要添加
|
在 `Account.java` 和 `IDCard.java` 示例中,若他们的关联关系是通过 **中间表** 的方式进行关联,则需要添加
|
||||||
`joinTable` `joinSelfColumn` `joinTargetColumn` 配置,如下所示:
|
`joinTable`、 `joinSelfColumn`、 `joinTargetColumn` 配置,如下所示:
|
||||||
|
|
||||||
```java 9,10,11
|
```java 9,10,11
|
||||||
public class Account implements Serializable {
|
public class Account implements Serializable {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user