mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
update docs
This commit is contained in:
parent
6c157a3602
commit
5fb9f4ca71
@ -71,15 +71,22 @@ delete from tb_account where id >= 100;
|
||||
在很多场景下,我们希望只更新**部分字段**,而更新的字段中,一些为 null,一些非 null。此时需要用到 `UpdateEntity` 工具类,以下是示例代码:
|
||||
|
||||
```java
|
||||
Account account = UpdateEntity.of(Account.class);
|
||||
account.setId(100);
|
||||
Account account = UpdateEntity.of(Account.class, 100);
|
||||
//Account account = UpdateEntity.of(Account.class);
|
||||
//account.setId(100);
|
||||
|
||||
account.setUserName(null);
|
||||
account.setAge(10);
|
||||
|
||||
accountMapper.update(account);
|
||||
```
|
||||
|
||||
以上的示例中,会把 id (主键)为 100 这条数据中的 user_name 字段更新为 null,age 字段更新为 10,其他字段不会被更新。也就是说,通过 UpdateEntity 创建的对象,只会更新调用了 setter 方法的字段,若不调用 setter 方法,不管这个对象里的属性的值是什么,都不会更新到数据库。
|
||||
|
||||
|
||||
|
||||
以上的示例中,会把 id (主键)为 100 这条数据中的 user_name 字段更新为 null,age 字段更新为 10,其他字段不会被更新。
|
||||
|
||||
也就是说,通过 UpdateEntity 创建的对象,只会更新调用了 setter 方法的字段,若不调用 setter 方法,不管这个对象里的属性的值是什么,都不会更新到数据库。
|
||||
|
||||
其执行的 sql 内容如下:
|
||||
|
||||
@ -88,3 +95,16 @@ update tb_account
|
||||
set user_name = ?, age = ? where id = ?
|
||||
#参数: null,10,100
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
```java
|
||||
Account account = UpdateEntity.of(Account.class, 100);
|
||||
```
|
||||
等同于:
|
||||
|
||||
```java
|
||||
Account account = UpdateEntity.of(Account.class);
|
||||
account.setId(100);
|
||||
```
|
||||
|
||||
|
||||
@ -64,7 +64,7 @@ SpringBoot v3.x 添加 hikariCP 的内容如下:
|
||||
|
||||
原因是在数据源的配置中,未添加 `type` 字段的配置:
|
||||
|
||||
```yaml 3
|
||||
```yaml:line-numbers 3
|
||||
spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
@ -75,8 +75,8 @@ spring:
|
||||
第 3 行中的 `type` 字段不能为空,这个并非是 MyBaits-Flex 的问题,而是 Spring 没有内置对 Druid 数据源类型
|
||||
的主动发现机制。若使用 `hikariCP` 数据源,则可以不配置 `type` 内容。
|
||||
|
||||
> 若使用多数据源,或者把数据源配置到 `mybatis-flex.datasource` 下,使用 mybatis-flex 的数据源发现机制,
|
||||
> 使用 druid 也可以不用配置 type,更多文档参考:[多数据源章节](./core/multi-datasource.md)。
|
||||
> 若把数据源配置到 `mybatis-flex.datasource` 下,使用 mybatis-flex 的数据源发现机制,
|
||||
> 使用 druid 则可以不用配置 type,更多文档参考:[多数据源章节](./core/multi-datasource.md)。
|
||||
|
||||
## 与 PageHelper 集成出现错误
|
||||
|
||||
|
||||
@ -50,15 +50,14 @@ ext {
|
||||
}
|
||||
dependencies {
|
||||
implementation("com.mybatis-flex:mybatis-flex-core:${mybatis_flex_version}")
|
||||
|
||||
// 启用APT
|
||||
annotationProcessor("com.mybatis-flex:mybatis-flex-annotation:${mybatis_flex_version}")
|
||||
annotationProcessor("com.mybatis-flex:mybatis-flex-processor:${mybatis_flex_version}")
|
||||
}
|
||||
```
|
||||
|
||||
**第 3 步:编写实体类和 Mapper**
|
||||
|
||||
> 这部分可以使用 Mybatis-Flex 的代码生成器来生成实体类哟,功能非常强大的。详情进入:[代码生成器章节](../others/codegen.md) 了解。
|
||||
|
||||
```java
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
@ -82,7 +81,10 @@ public interface AccountMapper extends BaseMapper<Account> {
|
||||
}
|
||||
```
|
||||
|
||||
**第4步:编译项目自动生成辅助类**
|
||||
> 这部分也可以使用 Mybatis-Flex 的代码生成器来生,功能非常强大的。详情进入:[代码生成器章节](../others/codegen.md) 了解。
|
||||
|
||||
|
||||
**第4步:编译项目,自动生成查询辅助类**
|
||||
|
||||
Mybatis-Flex 使用了 APT(Annotation Processing Tool)技术,在项目编译的时,会自动生成辅助操作类。
|
||||
|
||||
@ -90,7 +92,6 @@ Mybatis-Flex 使用了 APT(Annotation Processing Tool)技术,在项目编
|
||||
- Gradle 编译: `gradlew classes`
|
||||
|
||||
|
||||
|
||||
更多信息请参考:[APT 设置章节](../others/apt.md)
|
||||
|
||||
|
||||
@ -135,11 +136,14 @@ public class HelloWorld {
|
||||
```
|
||||
|
||||
> 以上的示例中, `ACCOUNT` 为 Mybatis-Flex 通过 APT 自动生成,无需手动编码。更多查看 [APT 文档](../others/apt.md)。
|
||||
>
|
||||
>若觉得 APT 使用不习惯,
|
||||
> 也可以使用代码生成器来生成。点击 [代码生成器文档](../others/codegen.md) 了解。
|
||||
|
||||
|
||||
## 更多示例
|
||||
|
||||
- 示例 1:[Mybatis-Flex 原生(非 Spring)](https://gitee.com/mybatis-flex/mybatis-flex/tree/main/mybatis-flex-test/mybatis-flex-native-test)
|
||||
- 示例 2:[Mybatis-Flex with Spring](https://gitee.com/mybatis-flex/mybatis-flex/tree/main/mybatis-flex-test/mybatis-flex-spring-test)
|
||||
- 示例 3:[Mybatis-Flex with Spring boot](https://gitee.com/mybatis-flex/mybatis-flex/tree/main/mybatis-flex-test/mybatis-flex-spring-boot-test)
|
||||
- 示例 1:[Mybatis-Flex 原生(非 Spring)](https://gitee.com/mybatis-flex/mybatis-flex-samples)
|
||||
- 示例 2:[Mybatis-Flex with Spring](https://gitee.com/mybatis-flex/mybatis-flex-samples)
|
||||
- 示例 3:[Mybatis-Flex with Spring boot](https://gitee.com/mybatis-flex/mybatis-flex-samples)
|
||||
- 示例 4:[Db + Row](https://gitee.com/mybatis-flex/mybatis-flex/blob/main/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java)
|
||||
Loading…
x
Reference in New Issue
Block a user