手记

记一次mybatis typeHandler的应用

1,数据库数据类型为 VARCHAR 默认【】

2,PO中数据类型的定义
@ColumnType(typeHandler = DialStatusEnumSetTypeHandler.class)
private Set redialCondition;
3,DialStatusEnumSetTypeHandler具体实现

public class DialStatusEnumSetTypeHandler extends BaseTypeHandler<Set<DialStatusEnum>> {

    @Override
    public Set<DialStatusEnum> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String val = rs.getString(columnName);
        return JsonUtils.string2Object(val, new TypeReference<Set<DialStatusEnum>>() {
        });
    }

    @Override
    public Set<DialStatusEnum> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String val = rs.getString(columnIndex);
        return JsonUtils.string2Object(val, new TypeReference<Set<DialStatusEnum>>() {
        });
    }

    @Override
    public Set<DialStatusEnum> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String val = cs.getString(columnIndex);
        return JsonUtils.string2Object(val, new TypeReference<Set<DialStatusEnum>>() {
        });
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Set<DialStatusEnum> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JsonUtils.object2String(parameter));
    }
}

4,Mapper.xml文件

<result column="redial_condition" jdbcType="VARCHAR" property="redialCondition"
                typeHandler="com.yiwise.core.model.enums.handler.DialStatusEnumSetTypeHandler"/>
0人推荐
随时随地看视频
慕课网APP