springboot + mybatis 处理枚举值

目录

一、TypeHandler

二、自定义枚举值处理器

三、配置

四、原理


一、TypeHandler

针对各种类型的处理器

已经内置很多的处理器(TypeHandlerRegistry 初始化时就已经加载了

注意:对于枚举值,有默认的处理器,EnumTypeHandler,EnumOrdinalTypeHandler

二、自定义枚举值处理器

自定义的好处,就是按照自己的需要处理,比如:可以依据code自定义处理过程

public class StatusEnumTypeHandler<E extends Enum<E> & StatusEnum> extends BaseTypeHandler<E> {
    private Class<E> type;

    public StatusEnumTypeHandler(Class<E> type){
        this.type = type;
        if (type == null){
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        if (type.getEnumConstants() == null) {
            throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
        }
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException {
        //****这里可以自定义取值,而不是默认的名称或者序号*****//
        preparedStatement.setInt(i, e.getCode());
    }

    @Override
    public E getNullableResult(ResultSet resultSet, String s) throws SQLException {
        int value = resultSet.getInt(s);
        return this.getEnumInstance(value);
    }

    @Override
    public E getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int value = resultSet.getInt(i);
        return this.getEnumInstance(value);
    }

    @Override
    public E getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int value = callableStatement.getInt(i);
        return this.getEnumInstance(value);
    }

    private E getEnumInstance(int enumValue) {
        for (E e : type.getEnumConstants()) {
            if (e.getCode() == enumValue) {
                return e;
            }
        }
        return null;
    }
}

三、配置

1、配置文件mybatisConfig.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeHandlers>
        <typeHandler handler="com.dao.typehandler.StatusEnumTypeHandler"
                 javaType="com.enums.XXX"/>
    </typeHandlers>
    <mappers>
    </mappers>
</configuration>

2、mybatis generator配置

配置之后,会自动生成枚举字段

<table tableName="house" domainObjectName="HouseDao">
     <generatedKey column="id" sqlStatement="MySql" identity="true"/>
     <columnOverride column="status" jdbcType="SMALLINT" javaType="com.enums.XXX"/>
</table>

3、application.properties 配置

#下划线自动转驼峰式
#这里需要注意,mybatis.configuration不能和mybatis.config-location同时配置,只能配置其一
#mybatis.configuration.mapUnderscoreToCamelCase=true
#mapper映射文件所在的位置
mybatis.mapper-locations=classpath*:mapper/*.xml
#注意:不能是classpath*:mybatisConfig.xml,否则会找不到文件
mybatis.config-location=classpath:mybatisConfig.xml

四、原理

mybatis是由 MybatisAutoConfiguration类来生成SqlSessionFactory bean,在生成bean期间,会判断mybatis.configuration和mybatis.config-location是否同时存在(SqlSessionFactoryBean.afterPropertiesSet)。

SqlSessionFactoryBean会来解析配置文件并加入到typeHandlerRegistry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值