diff --git a/README.md b/README.md index 338bb41..dae054e 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ ## 功能 - [x] 简化枚举类定义(省略属性定义&get方法,提供大量实用的枚举转换工具) -- [x] Spring开发支持(支持实体类中使用枚举属性、支持Get类型接口入参直接使用枚举、Feign调用支持枚举传输) -- [ ] MyBatis开发支持(Entity中可直接用枚举属性在sql中自动转换成数值、MyBatisPlus中Wrapper支持直接使用枚举条件) +- [x] Spring开发支持(支持Post请求DTO/VO中使用枚举属性、支持Get请求方法出入参使用枚举、Feign调用支持枚举传输) +- [x] MyBatis开发支持(Entity中可直接用枚举属性在sql中自动转换成数值、MyBatisPlus中Wrapper支持直接使用枚举条件) - [ ] Dubbo调用中使用枚举传输 ## 快速上手 @@ -153,14 +153,14 @@ switch(studentDTO.getSex()){ io.github.luo-zhan easy-enum - 1.1.0-RELEASE + 1.2.0-RELEASE - + io.github.luo-zhan easy-enum-for-spring - 1.1.0-RELEASE + 1.2.0-RELEASE ``` diff --git a/easy-enum-for-spring/pom.xml b/easy-enum-for-spring/pom.xml index fafc717..d4d935b 100644 --- a/easy-enum-for-spring/pom.xml +++ b/easy-enum-for-spring/pom.xml @@ -6,7 +6,7 @@ io.github.luo-zhan EasyEnum - 1.1.0-RELEASE + 1.2.0-RELEASE easy-enum-for-spring @@ -20,16 +20,9 @@ io.github.luo-zhan easy-enum - 1.1.0-RELEASE + 1.2.0-RELEASE - - org.projectlombok - lombok - 1.18.26 - - - org.springframework.boot spring-boot-starter-web @@ -37,6 +30,13 @@ provided + + org.mybatis + mybatis + 3.5.7 + provided + + \ No newline at end of file diff --git a/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictJacksonConfiguration.java b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictJacksonConfiguration.java index 9e48feb..f47784a 100644 --- a/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictJacksonConfiguration.java +++ b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictJacksonConfiguration.java @@ -13,6 +13,9 @@ @Configuration public class DictJacksonConfiguration { + /** + * 这种方式可以添加自定义配置,而不会覆盖原有配置 + */ @Bean("dictJsonMapperCustomizer") public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.modules(new DictModule()); diff --git a/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictMybatisConfiguration.java b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictMybatisConfiguration.java new file mode 100644 index 0000000..144c582 --- /dev/null +++ b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictMybatisConfiguration.java @@ -0,0 +1,27 @@ +package com.robot.dict.spring; + +import com.robot.dict.spring.mybatis.DictTypeHandler; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.type.TypeHandlerRegistry; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Configuration; + +/** + * Dict枚举对Mybatis的支持 + * + * @author R + */ +@Configuration +@ConditionalOnBean(SqlSessionFactory.class) +public class DictMybatisConfiguration implements ApplicationContextAware { + + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class); + TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry(); + typeHandlerRegistry.setDefaultEnumTypeHandler(DictTypeHandler.class); + } + +} diff --git a/easy-enum-for-spring/src/main/java/com/robot/dict/spring/jackson/DictDeserializer.java b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/jackson/DictDeserializer.java index 1ad30c5..cee8071 100644 --- a/easy-enum-for-spring/src/main/java/com/robot/dict/spring/jackson/DictDeserializer.java +++ b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/jackson/DictDeserializer.java @@ -6,12 +6,10 @@ import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.deser.ContextualDeserializer; import com.robot.dict.Dict; -import lombok.NoArgsConstructor; import java.io.IOException; @SuppressWarnings("all") -@NoArgsConstructor public class DictDeserializer extends JsonDeserializer implements ContextualDeserializer { private Class enumClass; diff --git a/easy-enum-for-spring/src/main/java/com/robot/dict/spring/mybatis/DictTypeHandler.java b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/mybatis/DictTypeHandler.java new file mode 100644 index 0000000..4d104c6 --- /dev/null +++ b/easy-enum-for-spring/src/main/java/com/robot/dict/spring/mybatis/DictTypeHandler.java @@ -0,0 +1,63 @@ +package com.robot.dict.spring.mybatis; + + +import com.robot.dict.Dict; +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; +import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.stream.Stream; + +/** + * 字典枚举类型处理器 + * 可支持实体类使用枚举属性,以及MybatisPlus查询条件中使用枚举值 + * + * @author R + */ +public class DictTypeHandler> extends BaseTypeHandler> { + private final Class type; + private final Class genericType; + + @SuppressWarnings("unchecked") + public DictTypeHandler(Class type) { + if (type == null) { + throw new IllegalArgumentException("Type argument cannot be null"); + } + this.type = type; + this.genericType = Stream.of(type.getGenericInterfaces()) + .map(ParameterizedTypeImpl.class::cast) + .filter(genericInterface -> genericInterface.getRawType() == Dict.class) + .findFirst() + .map(genericInterface -> (Class) genericInterface.getActualTypeArguments()[0]) + .orElseThrow(() -> new RuntimeException("orm中使用的枚举类必须实现Dict接口")); + + } + + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Dict parameter, JdbcType jdbcType) throws SQLException { + ps.setObject(i, parameter.getCode()); + } + + @Override + public Dict getNullableResult(ResultSet rs, String columnName) throws SQLException { + K code = rs.getObject(columnName, genericType); + return Dict.getByCode(type, code); + } + + @Override + public Dict getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + K code = rs.getObject(columnIndex, genericType); + return Dict.getByCode(type, code); + } + + @Override + public Dict getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + K code = cs.getObject(columnIndex, genericType); + return Dict.getByCode(type, code); + } +} diff --git a/easy-enum-for-spring/src/main/resources/META-INF/spring.factories b/easy-enum-for-spring/src/main/resources/META-INF/spring.factories index 6b12bc8..311091a 100644 --- a/easy-enum-for-spring/src/main/resources/META-INF/spring.factories +++ b/easy-enum-for-spring/src/main/resources/META-INF/spring.factories @@ -1 +1,4 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.robot.dict.spring.DictJacksonConfiguration,com.robot.dict.spring.DictSpringConvertConfiguration +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.robot.dict.spring.DictJacksonConfiguration,\ +com.robot.dict.spring.DictSpringConvertConfiguration,\ +com.robot.dict.spring.DictMybatisConfiguration diff --git a/pom.xml b/pom.xml index 113e353..1eeee57 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ io.github.luo-zhan EasyEnum - 1.1.0-RELEASE + 1.2.0-RELEASE EasyEnum easy-enum