init
This commit is contained in:
11
src/main/java/com/newyetai/mybatis/handle/BaseEnum.java
Normal file
11
src/main/java/com/newyetai/mybatis/handle/BaseEnum.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.newyetai.mybatis.handle;
|
||||
|
||||
public interface BaseEnum<T> {
|
||||
|
||||
/**
|
||||
* 获取枚举的值
|
||||
*
|
||||
* @return 枚举的值
|
||||
*/
|
||||
T getValue();
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.newyetai.mybatis.handle;
|
||||
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @description: 通用枚举转换
|
||||
* @author: chenyun.huang
|
||||
* @create: 2020-04-14 20:00
|
||||
**/
|
||||
@MappedJdbcTypes(value = JdbcType.VARCHAR, includeNullJdbcType = true)
|
||||
@MappedTypes({BaseEnum.class})
|
||||
public class BaseEnumTypeHandler<E extends Enum<?> & BaseEnum> extends BaseTypeHandler<E> {
|
||||
|
||||
/**
|
||||
* 枚举的class
|
||||
*/
|
||||
private Class<E> type;
|
||||
/**
|
||||
* 枚举的每个子类枚
|
||||
*/
|
||||
private E[] enums;
|
||||
|
||||
/**
|
||||
* 一定要有默认的构造函数,不然抛出 not found method 异常
|
||||
*/
|
||||
public BaseEnumTypeHandler() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现
|
||||
*
|
||||
* @param type 配置文件中设置的转换类
|
||||
*/
|
||||
public BaseEnumTypeHandler(Class<E> type) {
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("Type argument cannot be null");
|
||||
}
|
||||
this.type = type;
|
||||
this.enums = type.getEnumConstants();
|
||||
if (this.enums == null) {
|
||||
throw new IllegalArgumentException(type.getSimpleName()
|
||||
+ " does not represent an enum type.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, E parameter,
|
||||
JdbcType jdbcType) throws SQLException {
|
||||
/*
|
||||
* BaseTypeHandler已经帮我们做了parameter的null判断
|
||||
* 数据库存储的是枚举的值,所以我们这里使用 value , 如果需要存储 name,可以自定义修改
|
||||
*/
|
||||
if (jdbcType == null) {
|
||||
Object value = parameter.getValue();
|
||||
ps.setString(i, Objects.toString(value));
|
||||
} else {
|
||||
ps.setObject(i, parameter.getValue(), jdbcType.TYPE_CODE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getNullableResult(ResultSet rs, String columnName)
|
||||
throws SQLException {
|
||||
return locateEnumStatus(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getNullableResult(ResultSet rs, int columnIndex)
|
||||
throws SQLException {
|
||||
return locateEnumStatus(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getNullableResult(CallableStatement cs, int columnIndex)
|
||||
throws SQLException {
|
||||
return locateEnumStatus(cs.getString(columnIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* 枚举类型转换,由于构造函数获取了枚举的子类 enums,让遍历更加高效快捷,
|
||||
* <p>
|
||||
* 我将取出来的值 全部转换成字符串 进行比较,
|
||||
*
|
||||
* @param value 数据库中存储的自定义value属性
|
||||
* @return value 对应的枚举类
|
||||
*/
|
||||
private E locateEnumStatus(String value) {
|
||||
return Arrays.stream(enums)
|
||||
.filter(e -> Objects.toString(e.getValue()).equals(value))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user