mybatisplus 顶级 Service扩展:
1.IBaseService 重写了常用的方法,使用前不用再校验入参,以OrThrow结尾的方法数据不存在会抛异常,返回的数据不用再判断是否为空。
2.IService 全部继承于IBaseService, 几乎所有查询都可以用该接口:IPage query(Wrapper queryWrapper, BaseQueryParam queryParam)。
3.在mybatisplus代码生成策略构造时增加
.superServiceClass(“基础包路径” + “自定义的接口IBaseService”)
.superServiceImplClass(“基础包路径” + “自自定义接口的实现类IBaseServiceImpl”)。
- IBaseService接口
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IBaseService<T> extends IService<T> {
/**
* 通用的查询:可分页/不分页/只查询总数/分页查询但不进行count查询
*
* @param queryWrapper 查询条件
* @param queryParam 查询参数:
* <p>1:currentPage或pageSize为null或者为0不分页查询列表,执行list(Wrapper<T> queryWrapper),返回IPage<p/>
* <p>2:currentPage或pageSize小于0,查询总记录条数,执行count(Wrapper<T> queryWrapper),返回IPage.total<p/>
* <p>3:searchCount是否进行count查询</p>
* <p>4:currentPage和pageSize大于0,分页查询,执行page(E page, Wrapper<T> queryWrapper),返回IPage</p>
* @return
*/
IPage<T> query(Wrapper<T> queryWrapper, BasePageParam queryParam);
/**
* 根据 ID 查询
*
* @param id 主键id
* @return 无数据抛异常
*/
T getByIdOrThrow(Serializable id);
T getByIdOrThrow(Serializable id, String errorMsgTemplate, Object... params);
/**
* 根据 ID 查询,复制Bean对象属性
*
* @param id 主键id
* @param clazz copy的class
* @param ignoreProperties 忽略的属性
* @return 无数据抛异常
*/
T getByIdOrThrow(Serializable id, Class<T> clazz, String... ignoreProperties);
/**
* 根据 ID 查询
*
* @param id 主键id
* @param clazz 被构造的类
* @return 无数据返回构造后的对象
*/
T getByIdOrNewInstance(Serializable id, Class<T> clazz);
/**
* 查询一条记录 LIMIT 1
*
* @param queryWrapper
* @return
*/
default T getAny(LambdaQueryWrapper<T> queryWrapper) {
return getOne(queryWrapper.last("LIMIT 1"));
}
/**
* 查询一条记录
*
* @param queryWrapper
* @return
*/
default T getAnyOrThrow(LambdaQueryWrapper<T> queryWrapper) {
return Assert.notNull(getAny(queryWrapper), Result.CodeEnum.NOT_DATA.getDesc());
}
/**
* 查询(根据ID 批量查询)
*
* @param ids 主键di集合
* @return 无数据抛异常
*/
List<T> listByIdsOrThrow(Collection<? extends Serializable> ids);
}
- 实现IBaseService接口
@Slf4j
public class IBaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IBaseService<T> {
@Override
public IPage<T> query(Wrapper<T> queryWrapper, BasePageParam queryParam) {
if (queryParam.getCurrentPage() == null || queryParam.getPageSize() == null || queryParam.getCurrentPage() == 0 || queryParam.getPageSize() == 0) {
List<T> list = list(queryWrapper);
return new Page<T>().setRecords(list).setTotal(list.size());
} else if (queryParam.getCurrentPage() < 0 || queryParam.getPageSize() < 0) {
return new Page<T>().setTotal(count(queryWrapper));
}
return page(Page.of(queryParam.getCurrentPage(), queryParam.getPageSize(), (queryParam.getSearchCount() == null || queryParam.getSearchCount())), queryWrapper);
}
@Override
public T getById(Serializable id) {
return super.getById(Assert.notNull(id, Result.CodeEnum.PARAM_ERROR.getDesc()));
}
@Override
public T getByIdOrThrow(Serializable id) {
return Assert.notNull(getById(id), Result.CodeEnum.NOT_DATA.getDesc());
}
@Override
public T getByIdOrThrow(Serializable id, String errorMsgTemplate, Object... params) {
return Assert.notNull(getById(id), errorMsgTemplate, params);
}
@Override
public T getByIdOrThrow(Serializable id, Class<T> clazz, String... ignoreProperties) {
return BeanUtil.copyProperties(getByIdOrThrow(id), clazz, ignoreProperties);
}
@Override
public T getByIdOrNewInstance(Serializable id, Class<T> clazz) {
T entity = getById(id);
return entity != null ? entity : ReflectUtil.newInstanceIfPossible(clazz);
}
@Override
public List<T> listByIdsOrThrow(Collection<? extends Serializable> ids) {
return Assert.notEmpty(listByIds(Assert.notEmpty(ids)), Result.CodeEnum.NOT_DATA.getDesc());
}
@Override
public boolean saveBatch(Collection<T> entityList) {
return !CollectionUtils.isEmpty(entityList) && super.saveBatch(entityList);
}
@Override
public boolean saveOrUpdateBatch(Collection<T> entityList) {
return !CollectionUtils.isEmpty(entityList) && super.saveOrUpdateBatch(entityList);
}
@Override
public boolean updateBatchById(Collection<T> entityList) {
return !CollectionUtils.isEmpty(entityList) && super.updateBatchById(entityList);
}
@Override
public boolean saveOrUpdate(T entity) {
BeanUtil.trimStrFields(entity);
return super.saveOrUpdate(entity);
}
@Override
public boolean removeById(Serializable id) {
return super.removeById(Assert.notNull(id, Result.CodeEnum.PARAM_ERROR.getDesc()));
}
@Override
public boolean removeByIds(Collection<?> list) {
return super.removeByIds(Assert.notEmpty(list, Result.CodeEnum.PARAM_ERROR.getDesc()));
}
}
- 基础分页查询参数
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class BasePageParam implements Serializable {
private static final long serialVersionUID = -1503907011703083515L;
/**
* 查询页码,当前第几页,0时不分页,小于0只查询总数
*/
@ApiModelProperty("查询页码,当前第几页,0时不分页,小于0只查询总数")
private Integer currentPage;
/**
* 每页显示x条,0时不分页,小于0只查询总数
*/
@ApiModelProperty("每页显示x条,0时不分页,小于0只查询总数")
private Integer pageSize;
/**
* 是否进行 count 查询
*/
@ApiModelProperty("是否进行 count 查询")
private Boolean searchCount = true;
/**
* 创建开始日期
*/
@ApiModelProperty("创建日期")
private LocalDate createBeginDate;
/**
* 创建结束日期
*/
@ApiModelProperty("创建结束日期")
private LocalDate createEndDate;
/**
* 创建开始日期
*
* @return (createBeginDate)yyyy-MM-dd 00:00:00,000
*/
public LocalDateTime getCreateBeginOfDay() {
return LocalDateTimeUtil.beginOfDay(this.createBeginDate);
}
/**
* 创建结束日期
*
* @return (createEndDate)yyyy-MM-dd 23:59:59
*/
public LocalDateTime getCreateEndOfDay() {
return LocalDateTimeUtil.endOfDay(this.createEndDate);
}
}