Search in sources :

Example 1 with IService

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

the class BindingCacheManager method initEntityInfoCache.

/**
 * 初始化
 */
private static void initEntityInfoCache() {
    StaticMemoryCacheManager cacheManager = getCacheManager();
    if (cacheManager.isUninitializedCache(CACHE_NAME_CLASS_ENTITY) == false) {
        return;
    }
    // 初始化有service的entity缓存
    Map<String, IService> serviceMap = ContextHelper.getApplicationContext().getBeansOfType(IService.class);
    Set<String> uniqueEntitySet = new HashSet<>();
    if (V.notEmpty(serviceMap)) {
        for (Map.Entry<String, IService> entry : serviceMap.entrySet()) {
            Class entityClass = BeanUtils.getGenericityClass(entry.getValue(), 1);
            if (entityClass != null) {
                IService entityIService = entry.getValue();
                if (uniqueEntitySet.contains(entityClass.getName())) {
                    if (entityIService.getClass().getAnnotation(Primary.class) != null) {
                        EntityInfoCache entityInfoCache = cacheManager.getCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), EntityInfoCache.class);
                        if (entityInfoCache != null) {
                            entityInfoCache.setService(entityIService);
                        }
                    } else {
                        log.warn("Entity: {} 存在多个service实现类,可能导致调用实例与预期不一致!", entityClass.getName());
                    }
                } else {
                    EntityInfoCache entityInfoCache = new EntityInfoCache(entityClass, entityIService);
                    cacheManager.putCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), entityInfoCache);
                    cacheManager.putCacheObj(CACHE_NAME_TABLE_ENTITY, entityInfoCache.getTableName(), entityInfoCache);
                    cacheManager.putCacheObj(CACHE_NAME_ENTITYNAME_CLASS, entityClass.getSimpleName(), entityClass);
                    uniqueEntitySet.add(entityClass.getName());
                }
            }
        }
    } else {
        log.debug("未获取到任何有效@Service.");
    }
    // 初始化没有service的table-mapper缓存
    SqlSessionFactory sqlSessionFactory = ContextHelper.getBean(SqlSessionFactory.class);
    Collection<Class<?>> mappers = sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers();
    if (V.notEmpty(mappers)) {
        for (Class<?> mapperClass : mappers) {
            Type[] types = mapperClass.getGenericInterfaces();
            try {
                if (types != null && types.length > 0 && types[0] != null) {
                    ParameterizedType genericType = (ParameterizedType) types[0];
                    Type[] superTypes = genericType.getActualTypeArguments();
                    if (superTypes != null && superTypes.length > 0 && superTypes[0] != null) {
                        String entityClassName = superTypes[0].getTypeName();
                        if (!uniqueEntitySet.contains(entityClassName) && entityClassName.length() > 1) {
                            Class<?> entityClass = Class.forName(entityClassName);
                            BaseMapper mapper = (BaseMapper) ContextHelper.getBean(mapperClass);
                            EntityInfoCache entityInfoCache = new EntityInfoCache(entityClass, null);
                            entityInfoCache.setBaseMapper(mapper);
                            cacheManager.putCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), entityInfoCache);
                            cacheManager.putCacheObj(CACHE_NAME_TABLE_ENTITY, entityInfoCache.getTableName(), entityInfoCache);
                            cacheManager.putCacheObj(CACHE_NAME_ENTITYNAME_CLASS, entityClass.getSimpleName(), entityClass);
                            uniqueEntitySet.add(entityClass.getName());
                        }
                    }
                }
            } catch (Exception e) {
                log.warn("解析mapper异常", e);
            }
        }
    }
    uniqueEntitySet = null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Primary(org.springframework.context.annotation.Primary) StaticMemoryCacheManager(com.diboot.core.cache.StaticMemoryCacheManager) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) IService(com.baomidou.mybatisplus.extension.service.IService)

Example 2 with IService

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

the class ExtQueryWrapper method queryList.

/**
 * 查询一条数据
 * @param entityClazz
 * @return
 */
public List queryList(Class<E> entityClazz, Pagination pagination) {
    this.mainEntityClass = entityClazz;
    IService iService = ContextHelper.getIServiceByEntity(entityClazz);
    if (iService != null) {
        return ServiceAdaptor.queryList(iService, (QueryWrapper) this, pagination, entityClazz);
    } else {
        throw new InvalidUsageException("查询对象无BaseService/IService实现: " + entityClazz.getSimpleName());
    }
}
Also used : IService(com.baomidou.mybatisplus.extension.service.IService) InvalidUsageException(com.diboot.core.exception.InvalidUsageException)

Example 3 with IService

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

the class ExtQueryWrapper method queryList.

/**
 * 查询一条数据
 * @param entityClazz
 * @return
 */
public List<E> queryList(Class<E> entityClazz) {
    this.mainEntityClass = entityClazz;
    IService iService = ContextHelper.getIServiceByEntity(entityClazz);
    if (iService != null) {
        return ServiceAdaptor.queryList(iService, this);
    } else {
        throw new InvalidUsageException("查询对象无BaseService/IService实现: " + entityClazz.getSimpleName());
    }
}
Also used : IService(com.baomidou.mybatisplus.extension.service.IService) InvalidUsageException(com.diboot.core.exception.InvalidUsageException)

Example 4 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project jeecg-boot by jeecgboot.

the class ImportExcelUtil method importDateSave.

public static List<String> importDateSave(List<?> 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 5 with IService

use of com.baomidou.mybatisplus.extension.service.IService in project kykms 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)

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