/*
* Copyright (c) 2015-2020, www.dibo.ltd ([email protected]).
* <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 [email protected]
* @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(entityClass);
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);
}
@Transactional(rollbackFor = Exception.class)
@Override
public boolean createEntity(T entity) {
if(entity == null){
warning("createEntity", "参数entity为null");
return false;
}
return save(entity);
}
@Transactional(rollbackFor = Exception.class)
@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 fieldKey
* @param fieldVal
*/
protected void afterDelete(String fieldKey, Object fieldVal) {
}
@Override
@Transactional(rollbackFor = Exception.class)
public <RE, R> boolean createEntityAndRelatedEntities(T entity, List<RE> relatedEntities, ISetter<RE, R> relatedEntitySetter) {
boolean success = createEntity(entity);
if(!success){
log.warn("新建Entity失败: {}", entity.toString());
return false;
}
if(V.isEmpty(relatedEntities)){
return true;
}
Class relatedEntityClass = relatedEntities.get(0).getClass();
// 获取主键
Object pkValue = getPrimaryKeyValue(entity);
String relatedEntitySetterFld = BeanUtils.convertToFieldName(relatedEntitySetter);
// 填充关联关系
relatedEntities.forEach(relatedEntity-> BeanUtils.setProperty(relatedEntity, relatedEntitySetterFld, pkValue));
// 获取关联对象对应的Service
BaseService relatedEntityService = ContextHolder.getBaseServiceByEntity(relatedEntityClass);
if(relatedEntityService != null){
return relatedEntityService.createEntities(relatedEntities);
}
else{
// 查找mapper
BaseMapper mapper = ContextHolder.getBaseMapperByEntity(relatedEntityClass);
// 新增
没有合适的资源?快使用搜索试试~ 我知道了~
Diboot轻代码开发平台 v3.2.0.zip
共871个文件
java:550个
vue:117个
ts:116个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 60 浏览量
2024-03-18
21:20:59
上传
评论
收藏 2.09MB ZIP 举报
温馨提示
Diboot轻代码开发平台 v3.2.0.zip
资源推荐
资源详情
资源评论
收起资源包目录
Diboot轻代码开发平台 v3.2.0.zip (871个子文件)
.eslintrc.cjs 527B
.env.development 66B
.env.development 34B
mybatis-3-mapper.dtd 7KB
.eslintignore 103B
spring.factories 104B
spring.factories 101B
spring.factories 98B
spring.factories 98B
spring.factories 96B
spring.factories 96B
spring.factories 95B
.gitignore 302B
.gitignore 302B
.gitignore 196B
说明.htm 4KB
index.html 17KB
index.html 394B
favicon.ico 10KB
favicon.ico 4KB
BaseServiceImpl.java 42KB
BeanUtils.java 41KB
BaseServiceTest.java 27KB
V.java 27KB
BaseBinder.java 22KB
IamPluginInitializer.java 21KB
ConditionManager.java 18KB
ResultAssembler.java 18KB
QueryCondition.java 18KB
ReadExcelListener.java 18KB
BaseController.java 17KB
BeanUtilsTest.java 17KB
QueryBuilder.java 16KB
ExcelHelper.java 16KB
TestJoinQuery.java 15KB
IamUserServiceImpl.java 15KB
D.java 14KB
DictionaryServiceExtImpl.java 14KB
HttpHelper.java 13KB
MiddleTable.java 13KB
ParserCache.java 12KB
IamResourceServiceImpl.java 12KB
IamRoleResourceServiceImpl.java 12KB
BaseService.java 12KB
BaseCrudRestController.java 12KB
S.java 12KB
QuartzSchedulerServiceImpl.java 11KB
BindingCacheManager.java 11KB
IamUserRoleServiceImpl.java 11KB
FieldBinder.java 11KB
ConditionParser.java 11KB
SqlFileInitializer.java 10KB
EntityBinder.java 10KB
SimplePrincipalCollection.java 10KB
ContextHolder.java 10KB
WrapperHelper.java 10KB
FieldListBinder.java 10KB
JoinsBinder.java 9KB
CoreAutoConfig.java 9KB
RelationsBinder.java 9KB
IamSecurityUtils.java 9KB
EntityListBinder.java 9KB
IamAutoConfig.java 9KB
AnnotationRestApiHolder.java 9KB
BaseTranslator.java 8KB
BaseExcelFileController.java 8KB
DynamicSqlProvider.java 8KB
SpringMvcConfig.java 8KB
IamAuthorizingRealm.java 8KB
JoinConditionManager.java 8KB
JsonResult.java 8KB
LogAspect.java 8KB
BindAnnotationGroup.java 8KB
IamTenantServiceImpl.java 8KB
TestFieldListBinder.java 8KB
BaseAuthServiceImpl.java 8KB
FileHelper.java 7KB
TokenUtils.java 7KB
TestEntityListBinder.java 7KB
DynamicMemoryCacheManager.java 7KB
Message.java 7KB
ParallelBindingManager.java 7KB
IamOrgServiceImpl.java 7KB
PropInfo.java 6KB
SqlExecutor.java 6KB
ApiPermissionExtractor.java 6KB
AliyunOssFileStorageServiceImpl.java 6KB
Cons.java 6KB
IamAccountServiceImpl.java 6KB
TestFieldBinder.java 6KB
ExcelBindAnnoHandler.java 6KB
ZipHelper.java 6KB
WxMpMemberAuthServiceImpl.java 6KB
JSON.java 6KB
CountBinder.java 6KB
UserOrgDataAccessScopeManager.java 6KB
HttpHelper.java 6KB
MessageServiceImpl.java 6KB
TenantPluginInitializer.java 6KB
OAuth2SSOServiceImpl.java 6KB
共 871 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
芝麻粒儿
- 粉丝: 6w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功