mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
docs: update docs
This commit is contained in:
parent
d24b723a1b
commit
68f1a7ef99
@ -170,3 +170,28 @@ update tb_account
|
|||||||
set user_name = "michael", age = (select ... from ... )
|
set user_name = "michael", age = (select ... from ... )
|
||||||
where id = 100
|
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-示例)。
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
在 MyBatis-Flex 中,内置了 `QueryChain.java` 和 `UpdateChain.java` 用于对数据进行链式查询操作和链式数据操作(修改和删除)。
|
在 MyBatis-Flex 中,内置了 `QueryChain.java` 和 `UpdateChain.java` 用于对数据进行链式查询操作和链式数据操作(修改和删除)。
|
||||||
|
|
||||||
|
- **QueryChain**:链式查询
|
||||||
|
- **UpdateChain**:链式更新
|
||||||
|
|
||||||
|
|
||||||
## QueryChain 示例
|
## QueryChain 示例
|
||||||
|
|
||||||
@ -39,9 +42,10 @@ List<Article> articles = QueryChain.of(mapper)
|
|||||||
## UpdateChain 示例
|
## UpdateChain 示例
|
||||||
|
|
||||||
假设我们要更新 `Account` 的 `userName` 为 "`张三`",更新年龄在之前的基础上加 1,更新代码如下:
|
假设我们要更新 `Account` 的 `userName` 为 "`张三`",更新年龄在之前的基础上加 1,更新代码如下:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateChain() {
|
public void testUpdateChain1() {
|
||||||
UpdateChain.of(Account.class)
|
UpdateChain.of(Account.class)
|
||||||
.set(Account::getUserName, "张三")
|
.set(Account::getUserName, "张三")
|
||||||
.setRaw(Account::getAge, "age + 1")
|
.setRaw(Account::getAge, "age + 1")
|
||||||
@ -56,6 +60,33 @@ UPDATE `tb_account` SET `user_name` = '张三' , `age` = age + 1
|
|||||||
WHERE `id` = 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 的方法
|
## QueryChain 的方法
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import com.mybatisflex.core.MybatisFlexBootstrap;
|
|||||||
import com.mybatisflex.core.audit.AuditManager;
|
import com.mybatisflex.core.audit.AuditManager;
|
||||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||||
import com.mybatisflex.core.audit.MessageCollector;
|
import com.mybatisflex.core.audit.MessageCollector;
|
||||||
|
import com.mybatisflex.core.query.QueryChain;
|
||||||
import com.mybatisflex.core.update.UpdateChain;
|
import com.mybatisflex.core.update.UpdateChain;
|
||||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
@ -13,6 +14,8 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||||
|
|
||||||
public class UpdateChainTest {
|
public class UpdateChainTest {
|
||||||
|
|
||||||
static AccountMapper accountMapper;
|
static AccountMapper accountMapper;
|
||||||
@ -53,4 +56,17 @@ public class UpdateChainTest {
|
|||||||
Account account = accountMapper.selectOneById(1);
|
Account account = accountMapper.selectOneById(1);
|
||||||
System.out.println(account);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user