Commit daab2c78 by s_guodong

blok类型数据查询

parent 463bdd09
package com.brilliance.mda.support.mybatis;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.*;
/**
* @Description
* @Author s_guodong
* @Date 2023/8/24
*/
@MappedJdbcTypes(JdbcType.BLOB)
@MappedTypes(String.class)
@Component
public class StringBlobTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
// 声明一个输入流对象
ByteArrayInputStream bis = null;
String param = null;
if (parameter != null) {
//这里强转是因为知道了是String类型,可以看具体情况而定
param = parameter;
}
try {
// 把字符串转为字节流
bis = new ByteArrayInputStream(param.getBytes("utf-8"));
} catch (Exception e) {
throw new RuntimeException("Blob Encoding Error!");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
throw new RuntimeException("Blob Encoding Error!");
}
}
}
ps.setBinaryStream(i, bis, param.length());
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
Blob blob = rs.getBlob(columnName);
String returnValue = null;
if (null != blob) {
returnValue = new String(blob.getBytes(1L, (int) blob.length()));
}
return returnValue;
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Blob blob = rs.getBlob(columnIndex);
String returnValue = null;
if (null != blob) {
returnValue = new String(blob.getBytes(1L, (int) blob.length()));
}
return returnValue;
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Blob blob = cs.getBlob(columnIndex);
String returnValue = null;
if (null != blob) {
returnValue = new String(blob.getBytes(1L, (int) blob.length()));
}
return returnValue;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment