diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/handle/BlobToStringHandler.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/handle/BlobToStringHandler.java new file mode 100644 index 00000000..6b7d609f --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/handle/BlobToStringHandler.java @@ -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 { + + @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; + } +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysNoticeMapper.xml b/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysNoticeMapper.xml index 53cd4741..111d5968 100644 --- a/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysNoticeMapper.xml +++ b/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysNoticeMapper.xml @@ -8,7 +8,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + @@ -18,7 +18,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - 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 @@ -54,7 +54,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" )values( #{noticeTitle}, #{noticeType}, - #{noticeContent}, + #{noticeContent,typeHandler=com.fastbee.system.handle.BlobToStringHandler}, #{status}, #{remark}, #{createBy}, @@ -67,7 +67,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" notice_title = #{noticeTitle}, notice_type = #{noticeType}, - notice_content = #{noticeContent}, + notice_content = #{noticeContent,typeHandler=com.fastbee.system.handle.BlobToStringHandler}, status = #{status}, update_by = #{updateBy}, update_time = CURRENT_TIMESTAMP