fix(通知公告): string与blob类型互转报错

This commit is contained in:
gx_ma
2025-04-24 09:34:26 +08:00
parent 8c9031acef
commit bfd2977ff4
2 changed files with 59 additions and 4 deletions

View File

@@ -0,0 +1,55 @@
package com.fastbee.system.handle;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import static cn.hutool.core.io.file.FileWrapper.DEFAULT_CHARSET;
public class BlobToStringHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, String parameter, JdbcType jdbcType) throws SQLException {
ByteArrayInputStream inputStream;
byte[] bytes;
bytes = parameter.getBytes(DEFAULT_CHARSET);
// 把String转化成byte流
inputStream = new ByteArrayInputStream(bytes);
preparedStatement.setBinaryStream(i, inputStream, bytes.length);
}
@Override
public String getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
Blob blob = resultSet.getBlob(columnName);
return getBlobToString(blob);
}
@Override
public String getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
Blob blob = resultSet.getBlob(columnIndex);
return getBlobToString(blob);
}
@Override
public String getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
Blob blob = callableStatement.getBlob(columnIndex);
return getBlobToString(blob);
}
private String getBlobToString(Blob blob) throws SQLException {
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
if (null != returnValue) {
// 把byte转化成string
result = new String(returnValue, DEFAULT_CHARSET);
}
return result;
}
}

View File

@@ -8,7 +8,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="noticeId" column="notice_id" />
<result property="noticeTitle" column="notice_title" />
<result property="noticeType" column="notice_type" />
<result property="noticeContent" column="notice_content" />
<result property="noticeContent" column="notice_content" typeHandler="com.fastbee.system.handle.BlobToStringHandler" />
<result property="status" column="status" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
@@ -18,7 +18,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap>
<sql id="selectNoticeVo">
select notice_id, notice_title, notice_type, cast(notice_content as char) as notice_content, status, create_by, create_time, update_by, update_time, remark
select notice_id, notice_title, notice_type, notice_content, status, create_by, create_time, update_by, update_time, remark
from sys_notice
</sql>
@@ -54,7 +54,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
)values(
<if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle}, </if>
<if test="noticeType != null and noticeType != ''">#{noticeType}, </if>
<if test="noticeContent != null and noticeContent != ''">#{noticeContent}, </if>
<if test="noticeContent != null and noticeContent != ''">#{noticeContent,typeHandler=com.fastbee.system.handle.BlobToStringHandler}, </if>
<if test="status != null and status != ''">#{status}, </if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
@@ -67,7 +67,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<set>
<if test="noticeTitle != null and noticeTitle != ''">notice_title = #{noticeTitle}, </if>
<if test="noticeType != null and noticeType != ''">notice_type = #{noticeType}, </if>
<if test="noticeContent != null">notice_content = #{noticeContent}, </if>
<if test="noticeContent != null">notice_content = #{noticeContent,typeHandler=com.fastbee.system.handle.BlobToStringHandler}, </if>
<if test="status != null and status != ''">status = #{status}, </if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = CURRENT_TIMESTAMP