集成小霸王注解

This commit is contained in:
wangyu 2020-04-09 10:48:48 +08:00
parent 0cbf0bbe24
commit 031288761e
6 changed files with 95 additions and 3 deletions

View File

@ -0,0 +1,29 @@
package com.flyfish.framework.annotations;
import com.flyfish.framework.repository.impl.DefaultRepositoryFactoryBean;
import com.flyfish.framework.repository.impl.DefaultRepositoryImpl;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import java.lang.annotation.*;
@EnableMongoRepositories(
repositoryFactoryBeanClass = DefaultRepositoryFactoryBean.class,
repositoryBaseClass = DefaultRepositoryImpl.class
)
@EnableMongoAuditing
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface EnableMongoRepo {
/**
* 扫描的基本路径
*
* @return 结果
*/
@AliasFor(annotation = EnableMongoRepositories.class)
String[] basePackages() default {};
}

View File

@ -5,12 +5,14 @@ import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.domain.base.Qo;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.ClassUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.BiFunction;
@ -105,7 +107,7 @@ public final class CriteriaBuilder<T extends Domain> {
Object value = propertyDescriptor.getReadMethod().invoke(qo);
// 值非空予以过滤
if (!ObjectUtils.isEmpty(value)) {
String field = keyMapper.getOrDefault(key, key);
String field = determineField(key, propertyDescriptor);
return functionMap.getOrDefault(propertyDescriptor.getName(), Criteria::is).apply(
Criteria.where(field), fixValue(field, value));
}
@ -149,6 +151,14 @@ public final class CriteriaBuilder<T extends Domain> {
}
}
private String determineField(String key, PropertyDescriptor propertyDescriptor) {
String field = keyMapper.getOrDefault(key, key);
if (ClassUtils.isAssignable(propertyDescriptor.getPropertyType(), Collection.class)) {
field = field.replace(".$id", "");
}
return field;
}
/**
* 整合最后的结果
*

View File

@ -0,0 +1,47 @@
package com.flyfish.framework.configuration.boot;
import com.flyfish.framework.annotations.EnableMongoRepo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.AliasFor;
import org.springframework.scheduling.annotation.EnableAsync;
import java.lang.annotation.*;
/**
* 飞鱼便捷入口一个注解所有工作
* All in One 小霸王注解注意指定扫描路径
*
* @author wangyu
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableMongoRepo
@SpringBootApplication
@EnableAsync(proxyTargetClass = true)
@EnableAspectJAutoProxy
public @interface FlyfishApplication {
/**
* repo扫描的基础包
*
* @return 结果
*/
@AliasFor(
annotation = EnableMongoRepo.class,
attribute = "basePackages"
)
String[] repositoryBasePackages() default {};
/**
* 扫描组件的路径
*
* @return 结果
*/
@AliasFor(
annotation = SpringBootApplication.class
)
String[] scanBasePackages() default "com.flyfish";
}

View File

@ -24,7 +24,7 @@ import java.util.List;
* @author wangyu
* @create 2017-06-15 8:48
*/
public abstract class BaseController<T extends Domain, Q extends Qo<T>> {
public abstract class BaseController<T extends Domain, Q extends Qo<T>> implements SafeController {
@Autowired
protected BaseService<T> service;

View File

@ -0,0 +1,4 @@
package com.flyfish.framework.controller;
public interface SafeController {
}

View File

@ -2,6 +2,7 @@ package com.flyfish.framework.handler;
import com.flyfish.framework.bean.BaseResponse;
import com.flyfish.framework.bean.Result;
import com.flyfish.framework.controller.SafeController;
import com.flyfish.framework.exception.BaseException;
import com.flyfish.framework.exception.auth.ClientTokenException;
import com.flyfish.framework.exception.auth.UserInvalidException;
@ -22,8 +23,9 @@ import java.util.stream.Collectors;
/**
* Created by wangyu on 2017/9/8.
* 只允许implement了SafeController的class
*/
@RestControllerAdvice("com.flyfish")
@RestControllerAdvice(basePackageClasses = SafeController.class)
@ResponseBody
public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);