mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: 文档优化,枚举注解获取优化
This commit is contained in:
parent
625f166542
commit
75c4f74861
@ -43,7 +43,7 @@ public enum TypeEnum {
|
||||
```
|
||||
```java
|
||||
public interface BaseEnum {
|
||||
@EnumValue
|
||||
@EnumValue // 当注解写在枚举基类接口里
|
||||
Integer getCode();
|
||||
String getDesc();
|
||||
}
|
||||
@ -74,8 +74,10 @@ public enum EnableEnum implements BaseEnum {
|
||||
**注意事项**
|
||||
|
||||
- 1、@EnumValue 注解标识的属性,要求必须是 public 修饰,或者有 get 方法。
|
||||
- 2、@EnumValue 注解标识支持在 get 方法使用注解,也支持在基类接口上的get方法上使用注解。
|
||||
- 3、枚举注解优先级。 优先取字段上的注解。如果字段没有注解,则会找方法上的注解,如果枚举类当前方法没有注解,则会去找父类或者父接口的方法寻找存在注解的方法。
|
||||
|
||||
- 2、当配置了 @EnumValue 时,QueryWrapper 构建时,传入枚举,自动使用该值进行 SQL 参数拼接。例如:
|
||||
- 4、当配置了 @EnumValue 时,QueryWrapper 构建时,传入枚举,自动使用该值进行 SQL 参数拼接。例如:
|
||||
|
||||
```java
|
||||
QueryWrapper query = QueryWrapper.create();
|
||||
@ -89,6 +91,5 @@ query.select().from(ACCOUNT)
|
||||
select * from tb_account
|
||||
where type_enum = 1
|
||||
```
|
||||
- 3、支持在当前方法使用注解,也支持在基类接口上使用注解。优先取字段上的注解。如果字段没有注解,则会找方法上的注解,如果枚举类当前方法没有注解,则会去找父类或者父接口的方法寻找存在注解的方法。
|
||||
|
||||
|
||||
|
||||
@ -36,11 +36,20 @@ public class CompositeEnumTypeHandler<E extends Enum<E>> implements TypeHandler<
|
||||
private final TypeHandler<E> delegate;
|
||||
|
||||
public CompositeEnumTypeHandler(Class<E> enumClass) {
|
||||
boolean isNotFind = false;
|
||||
List<Field> enumDbValueFields = ClassUtil.getAllFields(enumClass, f -> f.getAnnotation(EnumValue.class) != null);
|
||||
List<Method> enumDbValueMethods = ClassUtil.getAllMethods(enumClass, m -> m.getAnnotation(EnumValue.class) != null);
|
||||
List<Method> enumDbInterfaceMethodList = Arrays.stream(enumClass.getInterfaces())
|
||||
.flatMap(inter -> ClassUtil.getAllMethods(inter, m -> m.getAnnotation(EnumValue.class) != null).stream()).collect(Collectors.toList());
|
||||
if (enumDbValueFields.isEmpty() && enumDbValueMethods.isEmpty() && enumDbInterfaceMethodList.isEmpty()) {
|
||||
if (enumDbValueFields.isEmpty()) {
|
||||
List<Method> enumDbValueMethods = ClassUtil.getAllMethods(enumClass, m -> m.getAnnotation(EnumValue.class) != null);
|
||||
if (enumDbValueMethods.isEmpty()) {
|
||||
List<Method> enumDbInterfaceMethodList = Arrays.stream(enumClass.getInterfaces())
|
||||
.flatMap(inter -> ClassUtil.getAllMethods(inter, m -> m.getAnnotation(EnumValue.class) != null).stream())
|
||||
.collect(Collectors.toList());
|
||||
if (enumDbInterfaceMethodList.isEmpty()) {
|
||||
isNotFind = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isNotFind) {
|
||||
delegate = new EnumTypeHandler<>(enumClass);
|
||||
} else {
|
||||
delegate = new FlexEnumTypeHandler<>(enumClass);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user