/*
* Copyright (c) 2015-2020, www.dibo.ltd (service@dibo.ltd).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.diboot.core.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.diboot.core.binding.Binder;
import com.diboot.core.binding.cache.BindingCacheManager;
import com.diboot.core.binding.helper.ServiceAdaptor;
import com.diboot.core.binding.helper.WrapperHelper;
import com.diboot.core.binding.parser.EntityInfoCache;
import com.diboot.core.binding.parser.ParserCache;
import com.diboot.core.binding.parser.PropInfo;
import com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper;
import com.diboot.core.config.BaseConfig;
import com.diboot.core.config.Cons;
import com.diboot.core.dto.SortParamDTO;
import com.diboot.core.entity.BaseTreeEntity;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.exception.InvalidUsageException;
import com.diboot.core.mapper.BaseCrudMapper;
import com.diboot.core.service.BaseService;
import com.diboot.core.util.*;
import com.diboot.core.vo.LabelValue;
import com.diboot.core.vo.Pagination;
import com.diboot.core.vo.Status;
import org.apache.ibatis.reflection.property.PropertyNamer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.lang.Nullable;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* CRUD通用接口实现类
* @author mazc@dibo.ltd
* @param <M> mapper类
* @param <T> entity类
* @version 2.0
* @date 2019/01/01
*/
@SuppressWarnings({"unchecked", "rawtypes", "JavaDoc"})
public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {
private static final Logger log = LoggerFactory.getLogger(BaseServiceImpl.class);
/**
* 获取当前的Mapper对象
* @return
*/
@Override
public M getMapper(){
return baseMapper;
}
@Override
public QueryChainWrapper<T> query() {
return ChainWrappers.queryChain(this.getBaseMapper());
}
@Override
public LambdaQueryChainWrapper<T> lambdaQuery() {
return ChainWrappers.lambdaQueryChain(this.getBaseMapper());
}
@Override
public UpdateChainWrapper<T> update() {
return ChainWrappers.updateChain(this.getBaseMapper());
}
@Override
public LambdaUpdateChainWrapper<T> lambdaUpdate() {
return ChainWrappers.lambdaUpdateChain(this.getBaseMapper());
}
@Override
public T getEntity(Serializable id){
return super.getById(id);
}
@Override
public <FT> FT getValueOfField(Serializable idVal, SFunction<T, FT> getterFn) {
PropInfo propInfo = BindingCacheManager.getPropInfoByClass(getEntityClass());
String fetchCol = propInfo.getColumnByField(BeanUtils.convertSFunctionToFieldName(getterFn));
QueryWrapper<T> queryWrapper = new QueryWrapper<T>()
.select(propInfo.getIdColumn(), fetchCol)
.eq(propInfo.getIdColumn(), idVal);
T entity = getSingleEntity(queryWrapper);
if(entity == null){
return null;
}
return getterFn.apply(entity);
}
@Override
public <FT> FT getValueOfField(SFunction<T, ?> queryFieldFn, Serializable queryFieldVal, SFunction<T, FT> getterFn) {
return getValueOfField(Wrappers.<T>lambdaQuery().eq(queryFieldFn, queryFieldVal), getterFn);
}
@Override
public <FT> FT getValueOfField(LambdaQueryWrapper<T> queryWrapper, SFunction<T, FT> getterFn) {
T entity = getSingleEntity(queryWrapper.select(getterFn));
if(entity == null){
return null;
}
return getterFn.apply(entity);
}
@Override
public <FT> List<FT> getValuesOfField(String fieldKey, Object fieldVal, SFunction<T, FT> getterFn) {
QueryWrapper<T> queryWrapper = buildQueryWrapperByFieldValue(fieldKey, fieldVal);
PropInfo propInfo = BindingCacheManager.getPropInfoByClass(getEntityClass());
String fetchCol = propInfo.getColumnByField(BeanUtils.convertSFunctionToFieldName(getterFn));
queryWrapper.select(fetchCol);
return getValuesOfField(queryWrapper, getterFn);
}
/**
* 基于给定的字段和值,构建 QueryWrapper
* @param fieldKey
* @param fieldVal
* @return
*/
protected QueryWrapper<T> buildQueryWrapperByFieldValue(String fieldKey, Object fieldVal){
PropInfo propInfo = BindingCacheManager.getPropInfoByClass(getEntityClass());
String conditionCol = propInfo.getColumnByField(fieldKey);
QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
if((fieldVal instanceof Collection)){
queryWrapper.in(conditionCol, (Collection<?>) fieldVal);
}
else if(fieldVal.getClass().isArray()){
queryWrapper.in(conditionCol, (Object[]) fieldVal);
}
else {
queryWrapper.eq(conditionCol, fieldVal);
}
return queryWrapper;
}
@Transactional(rollbackFor = Exception.class)
@Override
public boolean createEntity(T entity) {
if(entity == null){
warning("createEntity", "参数entity为null");
return false;
}
return save(entity);
}
@Override
public boolean save(T entity) {
this.beforeCreate(entity);
boolean success = super.save(entity);
if(success) {
this.afterCreate(entity);
}
return success;
}
/**
* 用于创建之前的自动填充等场景调用
*/
protected void beforeCreate(T entity) {
if(entity instanceof BaseTreeEntity) {
fillTreeNodeParentPath(entity);
}
}
/**
* 创建数据的后拦截
* @param entity
*/
protected void afterCreate(T entity) {
}
/**
* 批量创建数据的前拦截
* @param entityList
*/
protected void beforeBatchCreate(Collection<T> entityList) {
}
/**
* 批量创建数据的后拦截
* @param entityList
*/
protected void afterBatchCreate(Collection<T> entityList) {
}
/**
* 更新数据的后拦截
* @param entity
*/
protected void afterUpdate(T entity) {
}
/**
* 删除数据的前拦截,值可能为单值或集合
* @param fieldKey
* @param fieldVal
*/
protected void beforeDelete(String fieldKey, Object fieldVal) {
}
/**
* 删除数据的前拦截,值可能为单值或集合
* @param entityIds
*/
protected void beforeDelete(Object entityIds) {
String
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
为开发人员打造的低代码开发平台。Mybatis-plus关联查询,关联无SQL,性能高10倍,前后端代码本地可视化生成,flowable工作流,spring cloud微服务,强大的无代码模型表单页面流程设计能力等全方位赋能!
资源推荐
资源详情
资源评论
收起资源包目录
为开发人员打造的低代码开发平台 Mybatis-plus关联查询,关联无SQL,性能高10倍,前后端代码本地可视化生成,flowable工作流,spring cloud微服务,强大的无代码模型表单页 (1038个子文件)
.eslintrc.cjs 525B
.env.development 148B
.env.development 93B
mybatis-3-mapper.dtd 7KB
.eslintignore 103B
aiconfigurator.factories 80B
.gitignore 302B
.gitignore 302B
.gitignore 208B
.gitignore 196B
index.html 17KB
index.html 666B
index.html 394B
favicon.ico 10KB
favicon.ico 10KB
org.springframework.boot.autoconfigure.AutoConfiguration.imports 41B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 38B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 35B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 35B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 33B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 33B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 32B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 31B
BaseServiceImpl.java 48KB
IamPluginInitializer.java 44KB
BeanUtils.java 42KB
BaseServiceTest.java 30KB
V.java 28KB
BaseBinder.java 22KB
ReadExcelListener.java 20KB
QueryCondition.java 19KB
ConditionManager.java 19KB
ResultAssembler.java 18KB
ExcelHelper.java 18KB
BeanUtilsTest.java 18KB
BaseController.java 17KB
QueryBuilder.java 17KB
DictionaryServiceExtImpl.java 17KB
D.java 16KB
IamUserServiceImpl.java 16KB
TestJoinQuery.java 15KB
BaseService.java 15KB
MiddleTable.java 13KB
HttpHelper.java 13KB
IamRoleResourceServiceImpl.java 13KB
ParserCache.java 12KB
IamResourceServiceImpl.java 12KB
S.java 12KB
BaseCrudRestController.java 12KB
QuartzSchedulerServiceImpl.java 12KB
CoreAutoConfig.java 11KB
BindingCacheManager.java 11KB
ShiroHttpServletRequest.java 11KB
IamUserRoleServiceImpl.java 11KB
FieldBinder.java 11KB
SqlFileInitializer.java 11KB
EntityBinder.java 10KB
SimplePrincipalCollection.java 10KB
ContextHolder.java 10KB
WrapperHelper.java 10KB
IamAutoConfig.java 10KB
IamSecurityUtils.java 10KB
FieldListBinder.java 10KB
I18nConfigServiceImpl.java 10KB
JoinsBinder.java 9KB
BaseTranslator.java 9KB
RelationsBinder.java 9KB
EntityListBinder.java 9KB
OAuthSSOManager.java 9KB
AnnotationRestApiHolder.java 9KB
SpringMvcConfig.java 8KB
BaseExcelFileController.java 8KB
JoinConditionManager.java 8KB
BaseAuthServiceImpl.java 8KB
IamTenantServiceImpl.java 8KB
LogAspect.java 8KB
IamAuthorizingRealm.java 8KB
JsonResult.java 8KB
FileHelper.java 8KB
TestFieldListBinder.java 8KB
DynamicSqlProvider.java 8KB
TokenUtils.java 8KB
Message.java 8KB
BindAnnotationGroup.java 7KB
TestEntityListBinder.java 7KB
DataAccessControlHandler.java 7KB
PropInfo.java 7KB
IamOrgServiceImpl.java 7KB
DynamicMemoryCacheManager.java 7KB
SqlExecutor.java 7KB
ParallelBindingManager.java 7KB
UserOrgDataAccessScopeManager.java 6KB
AliyunOssFileStorageServiceImpl.java 6KB
Cons.java 6KB
MessageServiceImpl.java 6KB
OracleTranslator.java 6KB
ExcelBindAnnoHandler.java 6KB
IamAccountServiceImpl.java 6KB
ApiPermissionExtractor.java 6KB
TenantPluginInitializer.java 6KB
共 1038 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 7454
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功