public enum ComputerState {
OPEN(10), //开启
CLOSE(11), //关闭
OFF_LINE(12), //离线
FAULT(200), //故障
UNKNOWN(255); //未知
private int code;
ComputerState(int code) { this.code = code; }
}
<typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.example.entity.enums.ComputerState"/> </typeHandlers>
public interface BaseCodeEnum {
int getCode();
}
public enum ComputerState implements BaseCodeEnum{
OPEN(10), //开启
CLOSE(11), //关闭
OFF_LINE(12), //离线
FAULT(200), //故障
UNKNOWN(255); //未知
private int code;
ComputerState(int code) { this.code = code; }
@Override
public int getCode() { return this.code; }
}
public class CodeEnumUtil {
public static <E extends Enum<?> & BaseCodeEnum> E codeOf(Class<E> enumClass, int code) {
E[] enumConstants = enumClass.getEnumConstants();
for (E e : enumConstants) {
if (e.getCode() == code)
return e;
}
return null;
}
}
public class CodeEnumTypeHandler<E extends Enum<?> & BaseCodeEnum> extends BaseTypeHandler<BaseCodeEnum> {
private Class<E> type;
public CodeEnumTypeHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, BaseCodeEnum parameter, JdbcType jdbcType)
throws SQLException {
ps.setInt(i, parameter.getCode());
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
int i = rs.getInt(columnName);
if (rs.wasNull()) {
return null;
} else {
try {
return CodeEnumUtil.codeOf(type, i);
} catch (Exception ex) {
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.",
ex);
}
}
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int i = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
try {
return CodeEnumUtil.codeOf(type, i);
} catch (Exception ex) {
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.",
ex);
}
}
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int i = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
try {
return CodeEnumUtil.codeOf(type, i);
} catch (Exception ex) {
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.",
ex);
}
}
}
}
<typeHandlers> <typeHandler handler="com.example.typeHandler.CodeEnumTypeHandler" javaType="com.example.entity.enums.ComputerState"/> </typeHandlers>
@Configuration
@ConfigurationProperties(prefix = "mybatis")
public class MyBatisConfig {
private String configLocation;
private String mapperLocations;
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, ResourcesUtil resourcesUtil) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
// 设置配置文件地址
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setConfigLocation(resolver.getResource(configLocation));
factory.setMapperLocations(resolver.getResources(mapperLocations));
SqlSessionFactory sqlSessionFactory = factory.getObject();
// ----------- 动态加载实现BaseCodeEnum接口的枚举,使用CodeEnumTypeHandler转换器
// 取得类型转换注册器
TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
// 扫描所有实体类
List<String> classNames = resourcesUtil.list("com/example", "/**/entity");
for (String className : classNames) {
// 处理路径成为类名
className = className.replace('/', '.').replaceAll("\\.class", "");
// 取得Class
Class<?> aClass = Class.forName(className, false, getClass().getClassLoader());
// 判断是否实现了BaseCodeEnum接口
if (aClass.isEnum() && BaseCodeEnum.class.isAssignableFrom(aClass)) {
// 注册
typeHandlerRegistry.register(className, "com.example.typeHandler.CodeEnumTypeHandler");
}
}
// --------------- end
return sqlSessionFactory;
}
public String getConfigLocation() {
return configLocation;
}
public void setConfigLocation(String configLocation) {
this.configLocation = configLocation;
}
public String getMapperLocations() {
return mapperLocations;
}
public void setMapperLocations(String mapperLocations) {
this.mapperLocations = mapperLocations;
}
}
@Component
public class ResourcesUtil {
private final ResourcePatternResolver resourceResolver;
public ResourcesUtil() {
this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
}
/**
* 返回路径下所有class
*
* @param rootPath 根路径
* @param locationPattern 位置表达式
* @return
* @throws IOException
*/
public List<String> list(String rootPath, String locationPattern) throws IOException {
Resource[] resources = resourceResolver.getResources("classpath*:" + rootPath + locationPattern + "/**/*.class");
List<String> resourcePaths = new ArrayList<>();
for (Resource resource : resources) {
resourcePaths.add(preserveSubpackageName(resource.getURI(), rootPath));
}
return resourcePaths;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有