mybatis 自定义转换规则_mybatis类型转换器 - 自定义全局转换enum

在数据模型、接口参数等场景部分属性参数为一些常量值,比如性别:男、女。若是定义成int或String类型,于是类型本身的范围太宽,要求使用者需要了解底层的业务方可知如何传值,那整体来看增加沟通成本,对接效率也低。面对此等业务,使用枚举简便许多。枚举enum像个固定常量类,也像一个数组。

public enum Sex {

Male(1,"男"),

FeMale(2,"女"),;private int index;

private String description;Sex(int index, String description){

this.index = index;

this.description = description;

}public int getIndex() {

return index;

}public String getDescription() {

return description;

}

}

定义枚举,存储到数据库为int。这里就需要使用上mybatis的类型处理器功能。mybatis定义了接口TypeHandler用于数据转换。

public interface TypeHandler {

void setParameter(PreparedStatement var1, int var2, T var3, JdbcType var4) throws SQLException;T getResult(ResultSet var1, String var2) throws SQLException;T getResult(ResultSet var1, int var2) throws SQLException;T getResult(CallableStatement var1, int var2) throws SQLException;

}

mybatis内置了许多类型处理器:IntegerTypeHandler、BigDecimalTypeHandler、EnumTypeHandler、EnumOrdinalTypeHandler等

EnumTypeHandler 数据库字符串类型,用以存储枚举的名称(Male, FeMale);

EnumOrdinalTypeHandler 数据库为数值类型,存储枚举的序数值

从 3.4.5 开始,MyBatis 默认支持 JSR-310(日期和时间 API):LocalDateTypeHandler、LocalDateTimeTypeHandler等。

你可以重写或自定义实现所需的类型转换器,实现org.apache.ibatis.type.TypeHandler 接口 或者继承一个很便利的类 org.apache.ibatis.type.BaseTypeHandler。

mybatis默认的枚举实现类,不能把index存入数据库中,因此需自定义实现器。

在项目定义的枚举不止一个,若是单个枚举转换,代码成本太高,维护扩展也困难。那有没有办法写一个通用的转换器呢?

可以的。

一、为了区分那些需要转换的枚举,提取些共同属性,定义成接口,需转皆实现此接口即可。

public interface BaseEnum{int getIndex();String getDescription();}

public enum Sex implements BaseEnum{

Male(1,"男"),

FeMale(2,"女"),;private int index;

private String description;Sex(int index, String description){

this.index = index;

this.description = description;

}public int getIndex() {

return index;

}public String getDescription() {

return description;

}

}

二、自定义类型转换器AutoGenericEnumTypeHandler,继承BaseTypeHandler。因为是通用类型,所以使用到java 泛型的参数

public class AutoGenericEnumTypeHandler extends BaseTypeHandler {

private Class enumType;

private E[] enums;

public AutoGenericEnumTypeHandler(){}

public AutoGenericEnumTypeHandler(Class type){

if (type == null) {

throw new IllegalArgumentException("Type argument cannot be null");

}

this.enumType = type;

this.enums = type.getEnumConstants();

if (this.enums == null) {

throw new IllegalArgumentException(type.getName() + " does not represent an enum type.");

}

}

private E loadEnum(int index) {

for (E e : enums) {

if (e.getIndex() == index) {

return e;

}

}

throw new IllegalArgumentException(enumType.getName() + " unknown enumerated type index:" + index);

}

@Override

public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {

ps.setInt(i, parameter.getIndex());

}

@Override

public E getNullableResult(ResultSet rs, String columnName) throws SQLException {

if(rs.getObject(columnName) == null){

return null;

}

int index = rs.getInt(columnName);

return loadEnum(index);

}

@Override

public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {

if(rs.getObject(columnIndex) == null){

return null;

}

int index = rs.getInt(columnIndex);

return loadEnum(index);

}

@Override

public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {

if(cs.getObject(columnIndex) == null){

return null;

}

int index = cs.getInt(columnIndex);

return loadEnum(index);

}

}

三、配置使用。3.4.5版本后提供配置默认枚举转换器即,不用侵入写代码(侵入写法源代码下载路径:https://gitee.com/zss_376987715/mybatis-dao)

spring boot的配置:mybatis.configuration.default-enum-type-handler=com.XXX.typehandler.AutoGenericEnumTypeHandler

spring项目通过mybatis-config.xml配置文件:

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

到此已经实现转换。

如果正在参照(https://www.cnblogs.com/song27/p/10977241.html)学习多数据源。需要对代码有点改动。

在创建SqlSessionFactory时,添加MybatisProperties mybatisProperties参数,重新加载configuration。不然spring boot的application.properties配置不会生效。因为未加载进去。

@Bean

public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource, MybatisProperties mybatisProperties) throws Exception {

SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(dataSource);

sessionFactory.setConfiguration(mybatisProperties.getConfiguration());

return sessionFactory.getObject();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值