-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robot
committed
Oct 17, 2023
1 parent
db95b25
commit 214a5ac
Showing
8 changed files
with
112 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
easy-enum-for-spring/src/main/java/com/robot/dict/spring/DictMybatisConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
easy-enum-for-spring/src/main/java/com/robot/dict/spring/mybatis/DictTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
easy-enum-for-spring/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters