mybatis blob乱码问题

解决办法:

对于注解形式的sql:

@Select("select * from sys_user where id = #{id}")
@Results({@Result(column = "info", property = "info", typeHandler = CustomizeBlobToStringHandler.class)})
User selectCustomAnnotation(@Param("id") Long id);

对于xml形式的sql

<resultMap id="BaseResultMap" type="com.samubo.plus.quickstart.pojo.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="age" property="age"/>
<result column="info" property="info" javaType="java.sql.Blob" typeHandler="com.samubo.plus.quickstart.handler.CustomizeBlobToStringHandler"/>

</resultMap>
<select id="selectCustomXml" resultType="com.samubo.plus.quickstart.pojo.User" >
    select * from sys_user where id = #{id}
</select>
@Data
@TableName(value = "sys_user", autoResultMap=true)
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    @TableField(typeHandler = CustomizeBlobToStringHandler.class, jdbcType = JdbcType.BLOB)
    private String info;

CustomizeBlobToStringHandler:

public class CustomizeBlobToStringHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
            throws SQLException {
        byte[] bytes = parameter.getBytes(StandardCharsets.UTF_8);
        ps.setBlob(i, new ByteArrayInputStream(bytes), bytes.length);

    }

    @Override
    public String getNullableResult(ResultSet resultSet, String columnName) throws SQLException {

        return getConvertResult(resultSet.getBlob(columnName));
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {

        return getConvertResult(resultSet.getBlob(columnIndex));
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {

        return getConvertResult(callableStatement.getBlob(columnIndex));
    }

    private String getConvertResult(Blob blob) throws SQLException {
        if (blob == null) {
            return null;
        }
        return new String(blob.getBytes(1, (int) blob.length()), StandardCharsets.UTF_8);
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个开源的持久层框架,它可以与关系数据库进行交互。在MyBatis中,可以使用BLOB(Binary Large Object)来存储和获取大量的二进制数据。 当我们需要将二进制数据存储到数据库中时,可以使用MyBatisBLOB类型来声明相应的属性。在数据库表中,我们可以使用BLOB、BINARY或VARBINARY类型来存储二进制数据。在MyBatis的映射文件中,我们可以使用BLOB类型来和数据库对应的字段进行映射。 在存储二进制数据之前,我们需要将其转换成字节数组。可以使用InputStream或File对象来获取二进制数据,然后将其转换成字节数组,再使用MyBatisBLOB对象进行存储。 在映射文件的insert语句中,我们可以使用#{blobProperty,jdbcType=BLOB}来指定BLOB类型的属性,并通过#{blobProperty}来引用该属性。当执行插入操作时,MyBatis会将字节数组转换为二进制数据,并存储到数据库中的BLOB字段中。 在查询二进制数据时,可以使用select语句查询到对应的BLOB字段。在映射文件中,我们可以使用#{blobProperty,jdbcType=BLOB}来接收查询结果,并通过#{blobProperty}来引用该属性。当执行查询操作时,MyBatis会将数据库中的BLOB字段转换为字节数组,并赋值给相应的属性。 总而言之,使用MyBatis存取BLOB数据需要将其转换成字节数组进行存储,然后在查询时将字节数组转换为对应的二进制数据。使用BLOB类型来声明属性,可以方便地进行数据库交互,实现对二进制数据的存储和获取
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值