如何在MyBatis中优雅的使用枚举
问题
在编码过程中,经常会遇到用某个数值来表示某种状态、类型或者阶段的情况,比如有这样一个枚举:
public enum ComputerState {
OPEN(10), //开启
CLOSE(11), //关闭
OFF_LINE(12), //离线
FAULT(200), //故障
UNKNOWN(255); //未知
private int code;
ComputerState(int code) { this.code = code; }
}
通常我们希望将表示状态的数值存入数据库,即ComputerState.OPEN存入数据库取值为10。
探索
首先,我们先看看MyBatis是否能够满足我们的需求。 MyBatis内置了两个枚举转换器分别是:org.apache.ibatis.type.EnumTypeHandler和org.apache.ibatis.type.EnumOrdinalTypeHandler。
EnumTypeHandler
这是默认的枚举转换器,该转换器将枚举实例转换为实例名称的字符串,即将ComputerState.OPEN转换OPEN。
EnumOrdinalTypeHandler
顾名思义这个转换器将枚举实例的ordinal属性作为取值,即ComputerState.OPEN转换为0,ComputerState.CLOSE转换为1。 使用它的方式是在MyBatis配置文件中定义:
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.example.entity.enums.ComputerState"/>
</typeHandlers>
以上的两种转换器都不能满足我们的需求,所以看起来要自己编写一个转换器了。
方案
MyBatis提供了org.apache.ibatis.type.BaseTypeHandler类用于我们自己扩展类型转换器,上面的EnumTypeHandler和EnumOrdinalTypeHandler也都实现了这个接口。
1. 定义接口
我们需要一个接口来确定某部分枚举类的行为。如下:
public interface BaseCodeEnum {
int getCode();
}
该接口只有一个返回编码的方法,返回值将被存入数据库。
2. 改造枚举
就拿上面的ComputerState来实现BaseCodeEnum接口:
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; }
}
3. 编写一个转换工具类
现在我们能顺利的将枚举转换为某个数值了,还需要一个工具将数值转换为枚举实例。
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;
}
}
4. 自定义类型转换器
准备工作做的差不多了,是时候开始编写转换器了。 BaseTypeHandler<T> 一共需要实现4个方法:
void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType)用于定义设置参数时,该如何把Java类型的参数转换为对应的数据库类型T getNullableResult(ResultSet rs, String columnName)用于定义通过字段名称获取字段数据时,如何把数据库类型转换为对应的Java类型T getNullableResult(ResultSet rs, int columnIndex)用于定义通过字段索引获取字段数据时,如何把数据库类型转换为对应的Java类型T getNullableResult(CallableStatement cs, int columnIndex)用定义调用存储过程后,如何把数据库类型转换为对应的Java类型
我是这样实现的:
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);
}
}
}
}
5. 使用
接下来需要指定哪个类使用我们自己编写转换器进行转换,在MyBatis配置文件中配置如下:
<typeHandlers>
<typeHandler handler="com.example.typeHandler.CodeEnumTypeHandler" javaType="com.example.entity.enums.ComputerState"/>
</typeHandlers>
搞定! 经测试ComputerState.OPEN被转换为10,ComputerState.UNKNOWN被转换为255,达到了预期的效果。
6. 优化
在第5步时,我们在MyBatis中添加typeHandler用于指定哪些类使用我们自定义的转换器,一旦系统中的枚举类多了起来,MyBatis的配置文件维护起来会变得非常麻烦,也容易出错。如何解决呢? 在Spring中我们可以使用JavaConfig方式来干预SqlSessionFactory的创建过程,来完成动态的转换器指定。 思路
- 通过
sqlSessionFactory.getConfiguration().getTypeHandlerRegistry()取得类型转换器注册器 - 扫描所有实体类,找到实现了
BaseCodeEnum接口的枚举类 - 将实现了
BaseCodeEnum的类注册使用CodeEnumTypeHandler进行转换。
实现如下:
MyBatisConfig.java
@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;
}
}
ResourcesUtil.java
@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;
}
}
结束了
以上就是我对如何在MyBatis中优雅的使用枚举的探索。如果你还有更优的解决方案,请一定在评论中告知,万分感激。
转自:https://segmentfault.com/a/1190000010755321
如何在MyBatis中优雅的使用枚举的更多相关文章
- 如何在mybatis 中使用In操作
如何在mybatis 中使用In操作 假如我们想使用这样一个sql 语句,但是这样的sql语句有IN这样的操作.在我们的mybatis中有相对应的操作 SELECT * FROM product_db ...
- 如何在 Swoole 中优雅的实现 MySQL 连接池
如何在 Swoole 中优雅的实现 MySQL 连接池 一.为什么需要连接池 ? 数据库连接池指的是程序和数据库之间保持一定数量的连接不断开, 并且各个请求的连接可以相互复用, 减少重复连接数据库带来 ...
- 如何在Vue中优雅的使用防抖节流
1. 什么是防抖节流 防抖:防止重复点击触发事件 首先啥是抖? 抖就是一哆嗦!原本点一下,现在点了3下!不知道老铁脑子是不是很有画面感!哈哈哈哈哈哈 典型应用就是防止用户多次重复点击请求数据. 代码实 ...
- 如何在 Swift 中优雅地处理 JSON
阅读目录 在Swift中使用JSON的问题 开始 基础用法 枚举(Enumeration) 下标(Subscripts) 打印 调试与错误处理 后记 因为Swift对于类型有非常严格的控制,它在处 ...
- 如何在K8S中优雅的使用私有镜像库 (Docker版)
前言 在企业落地 K8S 的过程中,私有镜像库 (专用镜像库) 必不可少,特别是在 Docker Hub 开始对免费用户限流之后, 越发的体现了搭建私有镜像库的重要性. 私有镜像库不但可以加速镜像的拉 ...
- 如何在mybatis中引用java中的常量和方法
转自:http://www.68idc.cn/help/jiabenmake/qita/20140821125261.html 在mybatis的映射xml文件调用java类的方法: 1. SELEC ...
- 如何在php中优雅的地调用python程序
1.准备工作 安装有python和php环境的电脑一台. 2.书写程序. php程序如下 我们也可以将exec('python test.py') 换成 system('python test.p ...
- Spring Security:如何在Postman中优雅地测试后端API(前后端分离)
前言 在Postman中可以编写和执行自动化测试,使用 JavaScript 编写基本的 API 测试,自由编写任何用于自动化测试的测试方案. 在POSTMAN中读取Cookie值 1. 我们需要向& ...
- 学习Spring Boot:(十二)Mybatis 中自定义枚举转换器
前言 在 Spring Boot 中使用 Mybatis 中遇到了字段为枚举类型,数据库存储的是枚举的值,发现它不能自动装载. 解决 内置枚举转换器 MyBatis内置了两个枚举转换器分别是:org. ...
随机推荐
- nginx 限制并发访问及请求频率
0. 1.参考 [工作]Nginx限制IP并发连接数和请求数的研究 Module ngx_http_limit_conn_module Module ngx_http_limit_req_module ...
- Note for "Some Remarks on Writing Mathematical Proofs"
John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...
- Sql与C#中日期格式转换总结
SQL中的转换方法: 一.将string转换为datetime,主要是使用Convert方法, 方法,Convert(datetime [ ( length ) ] , expression, [st ...
- linux查看文件的后几行
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu4AAABiCAIAAACAksEXAAAZuUlEQVR4nO2dzdmzuq6G0wRV0ARNMM
- 移动端line-height问题
ios5 上:82px;下:84px; ios6 上:82px;下:84px; ios6 plus 上:124px;下:126px; mi4w 上:118px;下:130px; vivo 上:5 ...
- div 内容自动换行
如图: 1.自动换行 <div style="widht:100%;height:100%;word-wrap: break-word">111111111111111 ...
- 《第六周java学习笔记》
教材知识点总结 Java 把 String 类定义为 final 类. new 运算符首先分配内存空间并在内存空间中放入字符序列,然后计算出引用.(new String(char a[],int st ...
- java日志的相关配置文件知识
w2bc.com/article/261758”“”“
- 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position
最近用Python写了些爬虫,在爬取一个gb2312的页面时,抛出异常: UnicodeEncodeError: 'ascii' codec can't encode characters in po ...
- Scala-Unit4-Scala数组/集合
一.Scala数组 1.数组的定义: 方法一:val arr = new Array[String](3) String代表数据的元素类型.3表示数组的长度 方法二:val arr = Array[I ...