• mybatis查询text类型字段时返回Nclob,导致json格式化时报错


    package cn.yuanqiao.archive.common.typeHandler;
    
    import com.alibaba.druid.proxy.jdbc.NClobProxyImpl;
    import org.apache.ibatis.type.*;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.StringReader;
    import java.sql.*;
    
    /**
     *
     *@AUTHOR:赵常飞
     *@date 2023/10/10 16:50
     */
    @MappedTypes(Clob.class)
    @MappedJdbcTypes(JdbcType.CLOB)
    @Component
    public class NClobTypeHandler extends BaseTypeHandler<String> {
        @Value("${YQ_DATABASE}")
        private Integer databaseId;
        @Override
        public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
            StringReader reader=new StringReader(parameter);
            ps.setCharacterStream(i,reader,parameter.length());
        }
    
        @Override
        public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
            String clobToString="";
            try {
                switch (databaseId) {
                    case 4://达梦数据库
                        NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(columnName);
                        Clob rawClob = aa.getRawClob();
                        clobToString = ClobToString(rawClob);
                        break;
                    default:
                        clobToString="长文本类型,无法显示";
                        break;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return clobToString;
        }
    
        @Override
        public String getNullableResult(ResultSet rs, int i) throws SQLException {
            String clobToString="";
            try {
                switch (databaseId) {
                    case 4:
                        NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(i);
                        Clob rawClob = aa.getRawClob();
                        clobToString = ClobToString(rawClob);
                        break;
                    default:
                        clobToString="长文本类型,无法显示";
                        break;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return clobToString;
        }
    
        @Override
        public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
            String clobToString="";
            try {
                switch (databaseId) {
                    case 4:
                        NClobProxyImpl aa=(NClobProxyImpl) callableStatement.getObject(i);
                        Clob rawClob = aa.getRawClob();
                        clobToString = ClobToString(rawClob);
                        break;
                    default:
                        clobToString="长文本类型,无法显示";
                        break;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return clobToString;
        }
    
    
        private String ClobToString(Clob clob) throws SQLException, IOException {
            String reString = "";
            Reader is = clob.getCharacterStream();// 得到流
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                sb.append(s);
                s = br.readLine();
            }
            reString = sb.toString();
            return reString;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    下载git步骤
    黑马Java热门面试题Redis(九)
    C++小病毒
    2022.11.11 英语背诵
    【技术积累】算法中的基本概念【一】
    gitLab上传项目代码
    测试开发——进阶篇2
    动手学深度学习(Pytorch版)代码实践 -深度学习基础-08多层感知机简洁版
    swin Transformer
    自然语言处理文本分割[Text segmentation]:PoNet算法使用多粒度Pooling结构替代attention的网络
  • 原文地址:https://blog.csdn.net/weixin_43484846/article/details/133776124