Search in sources :

Example 6 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project diboot by dibo-software.

the class ContextHelper method getIServiceByEntity.

/**
 * 根据Entity获取对应的IService实现
 * @param entity
 * @return
 */
public static IService getIServiceByEntity(Class entity) {
    EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity);
    IService iService = entityInfoCache != null ? entityInfoCache.getService() : null;
    if (iService == null) {
        log.info("未能识别到Entity: " + entity.getName() + " 的IService实现!");
    }
    return iService;
}
Also used : EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) IService(com.baomidou.mybatisplus.extension.service.IService)

Example 7 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project diboot by dibo-software.

the class ContextHelper method getBaseServiceByEntity.

/**
 * 根据Entity获取对应的BaseService实现
 * @param entity
 * @return
 */
public static BaseService getBaseServiceByEntity(Class entity) {
    EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity);
    IService iService = entityInfoCache != null ? entityInfoCache.getService() : null;
    if (iService == null) {
        log.info("未能识别到Entity: " + entity.getName() + " 的Service实现!");
    }
    if (iService instanceof BaseService) {
        return (BaseService) iService;
    }
    return null;
}
Also used : EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) IService(com.baomidou.mybatisplus.extension.service.IService) BaseService(com.diboot.core.service.BaseService)

Example 8 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project diboot by dibo-software.

the class JoinsBinder method executeJoinQuery.

/**
 * 关联查询(分页)
 * @param queryWrapper 调用QueryBuilder.to*QueryWrapper得到的实例
 * @param entityClazz 返回结果entity/vo类
 * @param pagination 分页
 * @return
 * @throws Exception
 */
private static <DTO, E> List<E> executeJoinQuery(QueryWrapper<DTO> queryWrapper, Class<E> entityClazz, Pagination pagination, boolean limit1) {
    // 非动态查询,走BaseService
    if (queryWrapper instanceof DynamicJoinQueryWrapper == false) {
        IService iService = ContextHelper.getIServiceByEntity(entityClazz);
        if (iService != null) {
            return ServiceAdaptor.queryList(iService, (QueryWrapper) queryWrapper, pagination, entityClazz);
        } else {
            throw new InvalidUsageException("单表查询对象无BaseService/IService实现: " + entityClazz.getSimpleName());
        }
    }
    long begin = System.currentTimeMillis();
    // 转换为queryWrapper
    DynamicJoinQueryWrapper dynamicJoinWrapper = (DynamicJoinQueryWrapper) queryWrapper;
    dynamicJoinWrapper.setMainEntityClass(entityClazz);
    List<Map<String, Object>> mapList = null;
    if (pagination == null) {
        if (limit1) {
            Page page = new Page<>(1, 1);
            page.setSearchCount(false);
            IPage<Map<String, Object>> pageResult = getDynamicQueryMapper().queryForListWithPage(page, dynamicJoinWrapper);
            mapList = pageResult.getRecords();
        } else {
            mapList = getDynamicQueryMapper().queryForList(dynamicJoinWrapper);
        }
    } else {
        // 格式化orderBy
        formatOrderBy(dynamicJoinWrapper, entityClazz, pagination);
        IPage<Map<String, Object>> pageResult = getDynamicQueryMapper().queryForListWithPage(pagination.toPage(), dynamicJoinWrapper);
        pagination.setTotalCount(pageResult.getTotal());
        mapList = pageResult.getRecords();
    }
    long ms = (System.currentTimeMillis() - begin);
    if (ms > 5000) {
        log.warn("{} 动态Join查询执行耗时 {} ms,建议优化", dynamicJoinWrapper.getDtoClass().getSimpleName(), ms);
    }
    if (V.isEmpty(mapList)) {
        return Collections.emptyList();
    }
    if (mapList.size() > BaseConfig.getBatchSize()) {
        log.warn("{} 动态Join查询记录数过大( {} 条), 建议优化", dynamicJoinWrapper.getDtoClass().getSimpleName(), mapList.size());
    }
    // 转换查询结果
    List<E> entityList = new ArrayList<>();
    for (Map<String, Object> colValueMap : mapList) {
        Map<String, Object> fieldValueMap = new HashMap<>();
        // 格式化map
        for (Map.Entry<String, Object> entry : colValueMap.entrySet()) {
            String fieldName = S.toLowerCaseCamel(entry.getKey());
            // 如果是布尔类型,检查entity中的定义是Boolean/boolean
            if (entry.getValue() instanceof Boolean && S.startsWithIgnoreCase(entry.getKey(), "is_")) {
                // 检查有is前缀的Boolean类型
                Field boolType = BeanUtils.extractField(entityClazz, fieldName);
                if (boolType == null) {
                    // 检查无is前缀的boolean类型
                    String tempFieldName = S.toLowerCaseCamel(S.substringAfter(entry.getKey(), "_"));
                    boolType = BeanUtils.extractField(entityClazz, tempFieldName);
                    if (boolType != null) {
                        fieldName = tempFieldName;
                    }
                }
            }
            fieldValueMap.put(fieldName, entry.getValue());
        }
        // 绑定map到entity
        try {
            E entityInst = entityClazz.newInstance();
            BeanUtils.bindProperties(entityInst, fieldValueMap);
            if (ENABLE_DATA_PROTECT) {
                ParserCache.getFieldEncryptorMap(entityClazz).forEach((k, v) -> {
                    String value = BeanUtils.getStringProperty(entityInst, k);
                    BeanUtils.setProperty(entityInst, k, value == null ? null : v.decrypt(value));
                });
            }
            entityList.add(entityInst);
        } catch (Exception e) {
            log.warn("new实例并绑定属性值异常", e);
        }
    }
    return entityList;
}
Also used : Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) DynamicJoinQueryWrapper(com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) Field(java.lang.reflect.Field) IService(com.baomidou.mybatisplus.extension.service.IService) InvalidUsageException(com.diboot.core.exception.InvalidUsageException)

Example 9 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project kms by mahonelau.

the class ImportExcelUtil method importDateSave.

public static List<String> importDateSave(List<Object> list, Class serviceClass, List<String> errorMessage, String errorFlag) {
    IService bean = (IService) SpringContextUtils.getBean(serviceClass);
    for (int i = 0; i < list.size(); i++) {
        try {
            boolean save = bean.save(list.get(i));
            if (!save) {
                throw new Exception(errorFlag);
            }
        } catch (Exception e) {
            String message = e.getMessage().toLowerCase();
            int lineNumber = i + 1;
            // 通过索引名判断出错信息
            if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_ROLE_CODE)) {
                errorMessage.add("第 " + lineNumber + " 行:角色编码已经存在,忽略导入。");
            } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_JOB_CLASS_NAME)) {
                errorMessage.add("第 " + lineNumber + " 行:任务类名已经存在,忽略导入。");
            } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_CODE)) {
                errorMessage.add("第 " + lineNumber + " 行:职务编码已经存在,忽略导入。");
            } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_DEPART_ORG_CODE)) {
                errorMessage.add("第 " + lineNumber + " 行:部门编码已经存在,忽略导入。");
            } else {
                errorMessage.add("第 " + lineNumber + " 行:未知错误,忽略导入");
                log.error(e.getMessage(), e);
            }
        }
    }
    return errorMessage;
}
Also used : IService(com.baomidou.mybatisplus.extension.service.IService) IOException(java.io.IOException)

Example 10 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project kms by mahonelau.

the class ImportExcelUtil method importDateSaveOne.

public static List<String> importDateSaveOne(Object obj, Class serviceClass, List<String> errorMessage, int i, String errorFlag) {
    IService bean = (IService) SpringContextUtils.getBean(serviceClass);
    try {
        boolean save = bean.save(obj);
        if (!save) {
            throw new Exception(errorFlag);
        }
    } catch (Exception e) {
        String message = e.getMessage().toLowerCase();
        int lineNumber = i + 1;
        // 通过索引名判断出错信息
        if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_ROLE_CODE)) {
            errorMessage.add("第 " + lineNumber + " 行:角色编码已经存在,忽略导入。");
        } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_JOB_CLASS_NAME)) {
            errorMessage.add("第 " + lineNumber + " 行:任务类名已经存在,忽略导入。");
        } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_CODE)) {
            errorMessage.add("第 " + lineNumber + " 行:职务编码已经存在,忽略导入。");
        } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_DEPART_ORG_CODE)) {
            errorMessage.add("第 " + lineNumber + " 行:部门编码已经存在,忽略导入。");
        } else {
            errorMessage.add("第 " + lineNumber + " 行:未知错误,忽略导入");
            log.error(e.getMessage(), e);
        }
    }
    return errorMessage;
}
Also used : IService(com.baomidou.mybatisplus.extension.service.IService) IOException(java.io.IOException)

Aggregations

IService (com.baomidou.mybatisplus.extension.service.IService)13 IOException (java.io.IOException)6 EntityInfoCache (com.diboot.core.binding.parser.EntityInfoCache)3 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)3 Wrapper (com.baomidou.mybatisplus.core.conditions.Wrapper)1 BaseMapper (com.baomidou.mybatisplus.core.mapper.BaseMapper)1 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 DynamicJoinQueryWrapper (com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper)1 StaticMemoryCacheManager (com.diboot.core.cache.StaticMemoryCacheManager)1 BaseService (com.diboot.core.service.BaseService)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 Primary (org.springframework.context.annotation.Primary)1