com.baomidou.mybatisplus.toolkit.ReflectionKit Java Examples

The following examples show how to use com.baomidou.mybatisplus.toolkit.ReflectionKit. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseServiceImpl.java    From cola-cloud with MIT License 6 votes vote down vote up
/**
 * <p>
 * TableId 注解存在更新记录,否插入一条记录
 * </p>
 *
 * @param entity 实体对象
 * @return boolean
 */
@Transactional(rollbackFor = Exception.class)
@Override
public boolean insertOrUpdate(T entity) {
    if (null != entity) {
        Class<?> cls = entity.getClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
            Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
            if (StringUtils.checkValNull(idVal)) {
                return insert(entity);
            } else {
                /*
                 * 更新成功直接返回,失败执行插入逻辑
                 */
                return updateById(entity) || insert(entity);
            }
        } else {
            throw new MybatisPlusException("Error:  Can not execute. Could not find @TableId.");
        }
    }
    return false;
}
 
Example #2
Source File: BaseServiceImpl.java    From cola-cloud with MIT License 6 votes vote down vote up
@Transactional(rollbackFor = Exception.class)
@Override
public boolean insertOrUpdateAllColumn(T entity) {
    if (null != entity) {
        Class<?> cls = entity.getClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
            Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
            if (StringUtils.checkValNull(idVal)) {
                return insertAllColumn(entity);
            } else {
                /*
                 * 更新成功直接返回,失败执行插入逻辑
                 */
                return updateAllColumnById(entity) || insertAllColumn(entity);
            }
        } else {
            throw new MybatisPlusException("Error:  Can not execute. Could not find @TableId.");
        }
    }
    return false;
}
 
Example #3
Source File: CriteriaParserFactory.java    From cola-cloud with MIT License 5 votes vote down vote up
/**
 * 注册解析器
 * @param conditionParser
 */
public static void register(ConditionParser conditionParser) {
    Class<?> type = ReflectionKit.getSuperClassGenricType(conditionParser.getClass(), 0);
    if (log.isDebugEnabled()) {
        log.debug("CriteriaContext Parser {0} was registered on {1}", conditionParser, type);
    }
    repository.put(type, conditionParser);
}
 
Example #4
Source File: GenericService.java    From bird-java with MIT License 5 votes vote down vote up
@SuppressWarnings("all")
protected Class<T> getModelClass(){
    if(modelClass == null){
        synchronized (this){
            if(modelClass == null){
                modelClass = (Class<T>)ReflectionKit.getSuperClassGenricType(getClass(), 1);
            }
        }
    }
    return modelClass;
}
 
Example #5
Source File: GenericService.java    From bird-java with MIT License 5 votes vote down vote up
@SuppressWarnings("all")
protected Class<TKey> getKeyClass(){
    if(keyClass == null){
        synchronized (this){
            if(keyClass == null){
                keyClass = (Class<TKey>)ReflectionKit.getSuperClassGenricType(getClass(), 1);
            }
        }
    }
    return keyClass;
}
 
Example #6
Source File: BaseAuthInterceptor.java    From Ffast-Java with MIT License 4 votes vote down vote up
/**
 * 拦截登录&权限
 *
 * @param request
 * @param response
 * @param handler
 * @return
 * @throws Exception
 */
@Override
public boolean preHandle(HttpServletRequest request,
                         HttpServletResponse response, Object handler) throws Exception {
    logger.debug("###############进入拦截器###############");
    String path = (request.getRequestURI().replace(request.getContextPath(), ""));
    StringBuilder permissionName = new StringBuilder();
    boolean loginVerify = false;

    HandlerMethod hMethod = null;
    String beanName = null;
    if (handler instanceof HandlerMethod) {
        hMethod = ((HandlerMethod) handler);
        Object bean = hMethod.getBean();
        Logined loginedClassAnnotation = bean.getClass().getAnnotation(Logined.class);
        Logined loginedMethodAnnotation = hMethod.getMethodAnnotation(Logined.class);
        if (loginedClassAnnotation != null) {
            if (loginedClassAnnotation.notEffectSelf() &&
                    hMethod.getMethod().getDeclaringClass().getName().equals(bean.getClass().getName())) {
                loginVerify = false;
            } else {
                loginVerify = true;
            }
        }
        if (loginedMethodAnnotation != null) {
            if (loginedMethodAnnotation.notEffectSelf()) {
                loginVerify = false;
            } else {
                loginVerify = true;
            }
        }
        Permission classPermission = bean.getClass().getAnnotation(Permission.class);
        Permission methodPermission = hMethod.getMethodAnnotation(Permission.class);


        if (methodPermission != null) {
            //如果是独立的权限验证
            if (methodPermission.alone()) {
                permissionName = new StringBuilder(methodPermission.value());
            } else {
                // 如果方法权限不为空才会和类权限组合
                if (classPermission != null && StringUtils.isNotEmpty(methodPermission.value())) {
                    permissionName.append(classPermission.value());
                    permissionName.append(":");
                    permissionName.append(methodPermission.value());
                }
            }
        }
        beanName = bean.getClass().getName();
    }

    if (loginVerify) {
        Class<T> tClass = ReflectionKit.getSuperClassGenricType(this.getClass(), 0);
        T loginUser = operatorUtils.getTokenUser(request, tClass);
        request.setAttribute("loginUser", loginUser);
        if (loginUser == null) {
            //未登录
            notLogin(response);
            logger.error(beanName + " path:" + path + " [未登录]");
            return false;
        } else if (StringUtils.isNotEmpty(permissionName) &&
                !verifyPerms(loginUser.getHasRoleId(), permissionName.toString())) {
            //没有权限
            notPermission(response);
            logger.error(beanName + " loginId:" + loginUser.getUserId() +
                    " loginName:" + loginUser.getUserName() + " funcId:" + permissionName + " [没有权限]");
            return false;
        }
    }

    return true;

}
 
Example #7
Source File: BaseService.java    From Ffast-Java with MIT License 4 votes vote down vote up
@Override
protected Class<T> currentModelClass() {
    return ReflectionKit.getSuperClassGenricType(this.getClass(), 1);
}
 
Example #8
Source File: BaseServiceImpl.java    From cola-cloud with MIT License 4 votes vote down vote up
/**
 * 获取泛型类型
 * @return
 */
@SuppressWarnings("unchecked")
protected Class<T> currentModelClass() {
    return ReflectionKit.getSuperClassGenricType(getClass(), 0);
}