Mybatis自定义类型转换器typeHandle

 自定义集合类型转换器, 将sql字符串和对象中的集合相互转换

/** 自定义类型转换器, 将sql字符串和对象中的集合相互转换
 * @author zjl
 * @date 2023/11/10
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(java.util.List.class)
public class ListToStringTypeHandle extends BaseTypeHandler<List<String>> {
    private static final ObjectMapper mapper = new ObjectMapper();

    private String toJson(List<String> params) {
        try {
            return mapper.writeValueAsString(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "[]";
    }

    private List<String> toObject(String content) {
        if (content != null && !content.isEmpty()) {
            try {
                return (List<String>) mapper.readValue(content, List.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return null;
        }
    }

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, toJson(parameter));
    }

    @Override
    public List<String> getResult(ResultSet resultSet, String columnName) throws SQLException {
        return this.toObject(resultSet.getString(columnName));
    }

    @Override
    public List<String> getResult(ResultSet resultSet, int columnName) throws SQLException {
        return this.toObject(resultSet.getString(columnName));
    }

    @Override
    public List<String> getResult(CallableStatement callableStatement, int columnName) throws SQLException {
        return this.toObject(callableStatement.getString(columnName));
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, toJson(parameter));
    }

    @Override
    public List<String> getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        return this.toObject(resultSet.getString(columnName));
    }

    @Override
    public List<String> getNullableResult(ResultSet resultSet, int columnName) throws SQLException {
        return this.toObject(resultSet.getString(columnName));
    }

    @Override
    public List<String> getNullableResult(CallableStatement callableStatement, int columnName) throws SQLException {
        return this.toObject(callableStatement.getString(columnName));
    }
}

 自定义数组类型转换器, 将sql字符串和对象中的数组相互转换

/**
 * @author zjl
 * @date 2023/11/10
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(java.lang.String[].class)
public class ArrayToStringTypeHandler extends BaseTypeHandler<String[]> {
    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, toJson(parameter));
    }

    @Override
    public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.toObject(rs.getString(columnName));
    }

    @Override
    public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.toObject(rs.getString(columnIndex));
    }

    @Override
    public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.toObject(cs.getString(columnIndex));
    }

    private String toJson(String[] params) {
        try {
            return mapper.writeValueAsString(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "[]";
    }

    private String[] toObject(String content) {
        if (content != null && !content.isEmpty()) {
            try {
                return (String[]) mapper.readValue(content, String[].class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return null;
        }
    }
}

以上两种typeHandle用法如下:

可使用注解模式

@TableField(value = "names" ,  jdbcType = JdbcType.VARCHAR ,typeHandler = ListToStringTypeHandle.class)
    private List<String> names;

或xml中(注:两种typeHandle的javaType都为java.util.List)

<resultMap id="na" type="对象的相对路径">
        <result typeHandler="ListToStringTypeHandle"
                column="names"  
                javaType="java.util.List"
                jdbcType="VARCHAR"
                property="names"/>
    </resultMap>



    <select id="selectNames" resultMap="na">
        SELECT * FROM sysName  
    </select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖中的阿龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值