docs: update docs

This commit is contained in:
开源海哥 2023-07-25 18:19:23 +08:00
parent d24b723a1b
commit 68f1a7ef99
3 changed files with 73 additions and 1 deletions

View File

@ -170,3 +170,28 @@ update tb_account
set user_name = "michael", age = (select ... from ... )
where id = 100
```
## UpdateChain
UpdateChain 是一个对 `UpdateEntity``UpdateWrapper` 等进行封装的一个工具类,方便用户用于进行链式操作。
假设我们要更新 `Account``userName` 为 "`张三`",更新年龄在之前的基础上加 1更新代码如下
```java
@Test
public void testUpdateChain() {
UpdateChain.of(Account.class)
.set(Account::getUserName, "张三")
.setRaw(Account::getAge, "age + 1")
.where(Account::getId).eq(1)
.update();
}
```
以上方法调用时MyBatis-Flex 内部执行的 SQL 如下:
```sql
UPDATE `tb_account` SET `user_name` = '张三' , `age` = age + 1
WHERE `id` = 1
```
更多关于 **链式操作**,请点击这个 [这里](./chain.html#updatechain-示例)。

View File

@ -2,6 +2,9 @@
在 MyBatis-Flex 中,内置了 `QueryChain.java``UpdateChain.java` 用于对数据进行链式查询操作和链式数据操作(修改和删除)。
- **QueryChain**:链式查询
- **UpdateChain**:链式更新
## QueryChain 示例
@ -39,9 +42,10 @@ List<Article> articles = QueryChain.of(mapper)
## UpdateChain 示例
假设我们要更新 `Account``userName` 为 "`张三`",更新年龄在之前的基础上加 1更新代码如下
```java
@Test
public void testUpdateChain() {
public void testUpdateChain1() {
UpdateChain.of(Account.class)
.set(Account::getUserName, "张三")
.setRaw(Account::getAge, "age + 1")
@ -56,6 +60,33 @@ UPDATE `tb_account` SET `user_name` = '张三' , `age` = age + 1
WHERE `id` = 1
```
**另一个示例:**
```java
@Test
public void testUpdateChain2() {
//更新数据
UpdateChain.of(Account.class)
.set(Account::getAge, ACCOUNT.AGE.add(1))
.where(Account::getId).ge(100)
.and(Account::getAge).eq(18)
.update();
//查询所有数据并打印
QueryChain.of(accountMapper)
.list()
.forEach(System.out::println);
}
```
通过 `UpdateChain` 进行 `update()`,其执行的 SQL 如下:
```sql
UPDATE `tb_account` SET `age` = `age` + 1
WHERE `id` >= 100 AND `age` = 18
```
## QueryChain 的方法

View File

@ -4,6 +4,7 @@ import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.junit.BeforeClass;
@ -13,6 +14,8 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
public class UpdateChainTest {
static AccountMapper accountMapper;
@ -53,4 +56,17 @@ public class UpdateChainTest {
Account account = accountMapper.selectOneById(1);
System.out.println(account);
}
@Test
public void testUpdateChain1() {
UpdateChain.of(Account.class)
.set(Account::getAge, ACCOUNT.AGE.add(1))
.where(Account::getId).ge(100)
.and(Account::getAge).eq(18)
.update();
QueryChain.of(accountMapper)
.list()
.forEach(System.out::println);
}
}