mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
83d3ed4ef8
@ -10,7 +10,7 @@ MyBatis-Flex 提供了一个名为 `IService` 的接口,及其默认实现类
|
||||
接口:
|
||||
|
||||
```java
|
||||
public interface IAccountService extends IService{
|
||||
public interface IAccountService extends IService<Account> {
|
||||
//你的自定义方法
|
||||
List<Account> customMethod();
|
||||
}
|
||||
@ -20,14 +20,15 @@ public interface IAccountService extends IService{
|
||||
|
||||
```java
|
||||
@Component
|
||||
public class AccountServiceImpl implements IAccountService
|
||||
extends ServiceImpl<AccountMapper, Account>{
|
||||
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
|
||||
implements IAccountService {
|
||||
|
||||
@Override
|
||||
public List<Account> customMethod() {
|
||||
// 返回 id >= 100 的数据
|
||||
return list(ACCOUNT.ID.ge(100));
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -45,7 +46,8 @@ public class AccountServiceImpl implements IAccountService
|
||||
|
||||
`IService` 的接口提供了 remove、removeById、removeByIds、removeByMap 方法,用于删除数据:
|
||||
|
||||
- **remove(query)**:根据 `QueryCondition` 或 `QueryWrapper` 构建的条件来删除数据。
|
||||
- **remove(query)**:根据 `QueryWrapper` 构建的条件来删除数据。
|
||||
- **remove(condition)**:根据 `QueryCondition` 构建的条件来删除数据。
|
||||
- **removeById(id)**:根据主键删除数据,复合主键需要传入一个数组。
|
||||
- **removeByIds(ids)**:根据主键的集合,批量删除多条数据。
|
||||
- **removeByMap(map)**:根据 `Map<字段名,值>` 组成的条件删除数据,字段名和值的关系为相等的关系;同时,防止 "不小心" 全表删除数据,Map 的值不允许为 null 或者空数据。
|
||||
@ -53,30 +55,40 @@ public class AccountServiceImpl implements IAccountService
|
||||
|
||||
## 更新数据
|
||||
|
||||
`IService` 的接口提供了 update、updateById、updateByMap 方法,用于更新数据:
|
||||
`IService` 的接口提供了 update、updateById、updateBatch 方法,用于更新数据:
|
||||
|
||||
- **update(entity, query)**:根据 `QueryCondition` 或 `QueryWrapper` 构建的条件更新数据,实体类可以没有主键(如果有也会被忽略),实体类的 null 属性,会自动被忽略。
|
||||
- **updateById(entity)**:根据主键更新数据,要求主键值不能为空,否则会抛出异常;同时,数据为 null 的字段不会更新到数据库。
|
||||
- **updateByMap(entity, map)**:根据 `Map<字段名,值>` 组成的条件更新数据,实体类可以没有主键(如果有也会被忽略),实体类的 null 属性,会自动被忽略。
|
||||
- **updateById(entity, ignoreNulls)**:根据主键更新数据,要求主键值不能为空,否则会抛出异常;同时,设置是否将数据为 null 的字段更新到数据库。
|
||||
- **update(entity, map)**:根据 `Map<字段名,值>` 组成的条件更新数据,实体类可以没有主键(如果有也会被忽略),实体类的 null 属性,会自动被忽略。
|
||||
- **update(entity, query)**:根据 `QueryWrapper` 构建的条件更新数据,实体类可以没有主键(如果有也会被忽略),实体类的 null 属性,会自动被忽略。
|
||||
- **update(entity, condition)**:根据 `QueryCondition` 构建的条件更新数据,实体类可以没有主键(如果有也会被忽略),实体类的 null 属性,会自动被忽略。
|
||||
- **updateBatch(entities)**:批量保存多条数据,要求主键值不能为空,否则会抛出异常;同时,数据为 null 的字段不会更新到数据库。
|
||||
- **updateBatch(entities, size)**:批量保存多条数据,按指定数量切分,要求主键值不能为空,否则会抛出异常;同时,数据为 null 的字段不会更新到数据库。。
|
||||
|
||||
|
||||
## 查询数据
|
||||
|
||||
### 查询一条数据
|
||||
|
||||
`IService` 的接口提供了 getById、getByIdOpt、getOne、getOneOpt 方法,用于查询一条数据:
|
||||
`IService` 的接口提供了 getById、getByIdOpt、getOne、getOneOpt、getOneAs、getOneAsOpt 方法,用于查询一条数据:
|
||||
|
||||
- **getById(id)**:根据主键查询数据。
|
||||
- **getByIdOpt(id)**:根据主键查询数据,并封装为 `Optional` 返回。
|
||||
- **getOne(query)**: 根据 `QueryCondition` 或 `QueryWrapper` 构建的条件查询一条数据。
|
||||
- **getOneOpt(query)**: 根据 `QueryCondition` 或 `QueryWrapper` 构建的条件查询一条数据,并封装为 `Optional` 返回。
|
||||
- **getOne(query)**: 根据 `QueryWrapper` 构建的条件查询一条数据。
|
||||
- **getOne(condition)**: 根据 `QueryCondition` 构建的条件查询一条数据。
|
||||
- **getOneOpt(query)**: 根据`QueryWrapper` 构建的条件查询一条数据,并封装为 `Optional` 返回。
|
||||
- **getOneOpt(condition)**: 根据 `QueryCondition` 构建的条件查询一条数据,并封装为 `Optional` 返回。
|
||||
- **getOneAs(query, asType)**: 根据 `QueryWrapper` 构建的条件查询一条数据,并通过 asType 进行接收。
|
||||
- **getOneAsOpt(query, asType)**: 根据`QueryWrapper` 构建的条件查询一条数据,并通过 asType 进行接收,封装为 `Optional` 返回。
|
||||
|
||||
### 查询多条数据
|
||||
|
||||
`IService` 的接口提供了 list、listByIds、listByMap 方法,用于查询多条数据:
|
||||
`IService` 的接口提供了 list、listAs、listByIds、listByMap 方法,用于查询多条数据:
|
||||
|
||||
- **list()**:查询所有数据。
|
||||
- **list(query)**:根据 `QueryCondition` 或 `QueryWrapper` 构建的条件查询多条数据。
|
||||
- **list(condition)**:根据 `QueryCondition` 构建的条件查询多条数据。
|
||||
- **listAs(query, asType)**:根据 `QueryWrapper` 构建的条件查询多条数据,并通过 asType 进行接收。
|
||||
- **listByIds(ids)**:根据主键的集合查询多条数据。
|
||||
- **listByMap(map)**:根据 `Map<字段名,值>` 组成的条件查询多条数据。
|
||||
|
||||
@ -85,16 +97,21 @@ public class AccountServiceImpl implements IAccountService
|
||||
`IService` 的接口提供了 exists、count 方法,用于查询数据数量;
|
||||
|
||||
- **count()**:查询所有数据数量。
|
||||
- **count(query)**:根据 `QueryCondition` 或 `QueryWrapper` 构建的条件查询数据数量。
|
||||
- **exist(query)**:根据 `QueryCondition` 或 `QueryWrapper` 构建的条件判断数据是否存在。
|
||||
- **count(query)**:根据 `QueryWrapper` 构建的条件查询数据数量。
|
||||
- **count(condition)**:根据 `QueryCondition` 构建的条件查询数据数量。
|
||||
- **exist(query)**:根据 `QueryWrapper` 构建的条件判断数据是否存在。
|
||||
- **exist(condition)**:根据 `QueryCondition` 构建的条件判断数据是否存在。
|
||||
|
||||
### 分页查询数据
|
||||
|
||||
`IService` 的接口提供了 page 方法,用于分页查询数据:
|
||||
`IService` 的接口提供了 page、pageAs 方法,用于分页查询数据:
|
||||
|
||||
- **page(page)**:分页查询所有数据。
|
||||
- **page(page, query)**:根据 `QueryCondition` 或 `QueryWrapper` 构建的条件分页查询数据。
|
||||
- **page(page, query)**:根据 `QueryWrapper` 构建的条件分页查询数据。
|
||||
- **page(page, condition)**:根据 `QueryCondition` 构建的条件分页查询数据。
|
||||
- **pageAs(page, query, asType)**:根据 `QueryWrapper` 构建的条件分页查询数据,并通过 asType 进行接收。
|
||||
|
||||
## 其他方法
|
||||
|
||||
- **getMapper()**:获取对应的 `BaseMapper` 接口。
|
||||
- **query()**:获取默认的 `QueryWrapper` 类。
|
||||
@ -166,9 +166,9 @@ public class AccountServiceImpl extends CacheableServiceImpl<MyAccountMapper, Ac
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
|
||||
public Page<Account> page(Page<Account> page, QueryWrapper query) {
|
||||
return super.page(page, query);
|
||||
@Cacheable(key = "#root.methodName + ':' + #page.getPageSize() + ':' + #page.getPageNumber() + ':' + #query.toSQL()")
|
||||
public <R> Page<R> pageAs(Page<R> page, QueryWrapper query, Class<R> asType) {
|
||||
return super.pageAs(page, query, asType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -115,9 +115,9 @@ public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.bu
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
|
||||
public Page<#(entityClassName)> page(Page<#(entityClassName)> page, QueryWrapper query) {
|
||||
return super.page(page, query);
|
||||
@Cacheable(key = "#root.methodName + ':' + #page.getPageSize() + ':' + #page.getPageNumber() + ':' + #query.toSQL()")
|
||||
public <R> Page<R> pageAs(Page<R> page, QueryWrapper query, Class<R> asType) {
|
||||
return super.pageAs(page, query, asType);
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
@ -282,7 +282,7 @@ public interface IService<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>根据查询条件查询一条数据,并通过 asType 进行接收
|
||||
* <p>根据查询条件查询一条数据,并通过 asType 进行接收。
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param asType 接收的数据类型
|
||||
@ -355,7 +355,7 @@ public interface IService<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>根据查询条件查询数据集合,并通过 asType 进行接收
|
||||
* <p>根据查询条件查询数据集合,并通过 asType 进行接收。
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param asType 接收的数据类型
|
||||
@ -456,7 +456,7 @@ public interface IService<T> {
|
||||
* @return 分页对象
|
||||
*/
|
||||
default Page<T> page(Page<T> page, QueryWrapper query) {
|
||||
return getMapper().paginate(page, query);
|
||||
return pageAs(page, query, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -470,6 +470,18 @@ public interface IService<T> {
|
||||
return page(page, query().where(condition));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>根据查询条件分页查询数据,并通过 asType 进行接收。
|
||||
*
|
||||
* @param page 分页对象
|
||||
* @param query 查询条件
|
||||
* @param asType 接收的数据类型
|
||||
* @return 分页对象
|
||||
*/
|
||||
default <R> Page<R> pageAs(Page<R> page, QueryWrapper query, Class<R> asType) {
|
||||
return getMapper().paginateAs(page, query, asType);
|
||||
}
|
||||
|
||||
default QueryWrapper query() {
|
||||
return QueryWrapper.create();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user