Skip to content

Commit

Permalink
add 新增mybatis支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Robot committed Oct 17, 2023
1 parent db95b25 commit 214a5ac
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 18 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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调用中使用枚举传输

## 快速上手
Expand Down Expand Up @@ -153,14 +153,14 @@ switch(studentDTO.getSex()){
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</dependency>

<!-- spring开发支持,包含核心模块 -->
<!-- spring开发支持(Json、Mybatis、Converter),引入这个不需要再引入上面核心模块 -->
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum-for-spring</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</dependency>
```

Expand Down
18 changes: 9 additions & 9 deletions easy-enum-for-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.luo-zhan</groupId>
<artifactId>EasyEnum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</parent>

<artifactId>easy-enum-for-spring</artifactId>
Expand All @@ -20,23 +20,23 @@
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.15</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
<scope>provided</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
@Configuration
public class DictJacksonConfiguration {

/**
* 这种方式可以添加自定义配置,而不会覆盖原有配置
*/
@Bean("dictJsonMapperCustomizer")
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.modules(new DictModule());
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dict> implements ContextualDeserializer {
private Class<? extends Dict> enumClass;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<K, T extends Dict<K>> extends BaseTypeHandler<Dict<K>> {
private final Class<T> type;
private final Class<K> genericType;

@SuppressWarnings("unchecked")
public DictTypeHandler(Class<T> 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<K>) genericInterface.getActualTypeArguments()[0])
.orElseThrow(() -> new RuntimeException("orm中使用的枚举类必须实现Dict接口"));

}


@Override
public void setNonNullParameter(PreparedStatement ps, int i, Dict<K> parameter, JdbcType jdbcType) throws SQLException {
ps.setObject(i, parameter.getCode());
}

@Override
public Dict<K> getNullableResult(ResultSet rs, String columnName) throws SQLException {
K code = rs.getObject(columnName, genericType);
return Dict.getByCode(type, code);
}

@Override
public Dict<K> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
K code = rs.getObject(columnIndex, genericType);
return Dict.getByCode(type, code);
}

@Override
public Dict<K> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
K code = cs.getObject(columnIndex, genericType);
return Dict.getByCode(type, code);
}
}
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.luo-zhan</groupId>
<artifactId>EasyEnum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
<name>EasyEnum</name>
<modules>
<module>easy-enum</module>
Expand Down

0 comments on commit 214a5ac

Please sign in to comment.