mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
commit
7532b2e31c
@ -1,6 +1,6 @@
|
|||||||
# Active Record <Badge type="tip" text="v1.5.3" />
|
# Active Record <Badge type="tip" text="v1.5.3" />
|
||||||
|
|
||||||
[Active Record 模式](http://www.martinfowler.com/eaaCatalog/activeRecord.html)出自 Martin Fowler
|
[Active Record](http://www.martinfowler.com/eaaCatalog/activeRecord.html) 模式出自 Martin Fowler
|
||||||
写的《[企业应用架构模式](https://book.douban.com/subject/4826290/)》书中。在 Active Record
|
写的《[企业应用架构模式](https://book.douban.com/subject/4826290/)》书中。在 Active Record
|
||||||
模式中,对象中既有持久存储的数据,也有针对数据的操作。Active Record 模式把数据存取逻辑作为对象的一部分,处理对象的用户知道如何把数据写入数据库,还知道如何从数据库中读出数据。
|
模式中,对象中既有持久存储的数据,也有针对数据的操作。Active Record 模式把数据存取逻辑作为对象的一部分,处理对象的用户知道如何把数据写入数据库,还知道如何从数据库中读出数据。
|
||||||
|
|
||||||
@ -34,6 +34,7 @@ public class Account extends Model<Account> {
|
|||||||
private String userName;
|
private String userName;
|
||||||
private Integer age;
|
private Integer age;
|
||||||
private Date birthday;
|
private Date birthday;
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -48,6 +49,7 @@ public class AccountController {
|
|||||||
public boolean save(@RequestBody Account account) {
|
public boolean save(@RequestBody Account account) {
|
||||||
return account.save();
|
return account.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -127,6 +129,17 @@ Account.create()
|
|||||||
.list();
|
.list();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 查询单列数据
|
||||||
|
|
||||||
|
`Model` 提供了 `obj` 方法来查询单列数据:
|
||||||
|
|
||||||
|
```java
|
||||||
|
Account.create()
|
||||||
|
.select(Account::getUserName) // 只查询 user_name 列数据
|
||||||
|
.where(Account::getAge).ge(18)
|
||||||
|
.objList();
|
||||||
|
```
|
||||||
|
|
||||||
### 查询分页数据
|
### 查询分页数据
|
||||||
|
|
||||||
`Model` 提供了 `page` 方法来查询分页数据:
|
`Model` 提供了 `page` 方法来查询分页数据:
|
||||||
@ -162,7 +175,7 @@ User.create()
|
|||||||
.withRelations() // 使用 Relations Query 的方式进行关联查询。
|
.withRelations() // 使用 Relations Query 的方式进行关联查询。
|
||||||
.maxDepth(3) // 设置父子关系查询中,默认的递归查询深度。
|
.maxDepth(3) // 设置父子关系查询中,默认的递归查询深度。
|
||||||
.ignoreRelations("orderList") // 忽略查询部分 Relations 注解标记的属性。
|
.ignoreRelations("orderList") // 忽略查询部分 Relations 注解标记的属性。
|
||||||
.extraCondition("id",100) // 添加额外的 Relations 查询条件。
|
.extraConditionParam("id", 100) // 添加额外的 Relations 查询条件。
|
||||||
.one();
|
.one();
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -174,16 +187,16 @@ User.create()
|
|||||||
User.create()
|
User.create()
|
||||||
.where(USER.USER_ID.eq(1))
|
.where(USER.USER_ID.eq(1))
|
||||||
.withFields() // 使用 Fields Query 的方式进行关联查询。
|
.withFields() // 使用 Fields Query 的方式进行关联查询。
|
||||||
.fieldMapping(User::getRoleList,user-> // 设置属性对应的 QueryWrapper 查询。
|
.fieldMapping(User::getRoleList, user -> // 设置属性对应的 QueryWrapper 查询。
|
||||||
QueryWrapper.create()
|
QueryWrapper.create()
|
||||||
.select()
|
.select()
|
||||||
.from(ROLE)
|
.from(ROLE)
|
||||||
.where(ROLE.ROLE_ID.in(
|
.where(ROLE.ROLE_ID.in(
|
||||||
QueryWrapper.create()
|
QueryWrapper.create()
|
||||||
.select(USER_ROLE.ROLE_ID)
|
.select(USER_ROLE.ROLE_ID)
|
||||||
.from(USER_ROLE)
|
.from(USER_ROLE)
|
||||||
.where(USER_ROLE.USER_ID.eq(user.getUserId()))
|
.where(USER_ROLE.USER_ID.eq(user.getUserId()))
|
||||||
)))
|
)))
|
||||||
.one();
|
.one();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -20,11 +20,12 @@
|
|||||||
- 2、是否主动添加了 `mybatis-spring-boot-starter` 的依赖,导致版本不匹配。使用 SpringBoot 的情况下,应该引用 `mybatis-flex-spring-boot-starter`
|
- 2、是否主动添加了 `mybatis-spring-boot-starter` 的依赖,导致版本不匹配。使用 SpringBoot 的情况下,应该引用 `mybatis-flex-spring-boot-starter`
|
||||||
就可以了,不需要再添加其他 MyBatis 依赖。
|
就可以了,不需要再添加其他 MyBatis 依赖。
|
||||||
- 3、是否与 `mybatis-plus-boot-starter` 共用,使 MyBatis 被优先初始化,而导致 MyBatis-Flex 没有被加载。
|
- 3、是否与 `mybatis-plus-boot-starter` 共用,使 MyBatis 被优先初始化,而导致 MyBatis-Flex 没有被加载。
|
||||||
- 4、是否添加了 `pagehelper-spring-boot-starter` 依赖,导致传递了 `mybatis-plus-boot-starter` 依赖。如还想继续使用 pagehelper 插件,点击 [这里](#与-pagehelper-集成出现错误) 查看解决方案。
|
- 4、是否添加了 `pagehelper-spring-boot-starter` 依赖,导致传递了 `mybatis-spring-boot-starter` 依赖。如还想继续使用 pagehelper 插件,点击 [这里](#与-pagehelper-集成出现错误) 查看解决方案。
|
||||||
|
|
||||||
## 示例中的 AccountMapper 和 "ACCOUNT" 在哪里,报错了。
|
## 示例中的 AccountMapper 和 "ACCOUNT" 在哪里,报错了。
|
||||||
|
|
||||||
MyBatis-Flex 使用了 APT 技术,这两个类是自动生成的。
|
MyBatis-Flex 使用了 APT 技术,这两个类是自动生成的,如果已经生成但是导入不了,点击 [这里](./others/apt.md#开发工具无法导入生成的代码) 查看解决方案。
|
||||||
|
|
||||||
参考:[MyBatis-Flex APT 配置 - MyBatis-Flex 官方网站](./others/apt.md)
|
参考:[MyBatis-Flex APT 配置 - MyBatis-Flex 官方网站](./others/apt.md)
|
||||||
|
|
||||||
## 阿里镜像找不到依赖?
|
## 阿里镜像找不到依赖?
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user