mybatis读取oralce数据库字段是clob类型两种方法

一、用流的方法将clob转为String类型 (要记的将流关闭)
@Override
    public Map queryreviewbaseerrorList(Map map) {
        Map mapData = new HashMap<>();
        List> list=new ArrayList>();
        int reviewcode=Integer.parseInt(map.get("reviewcode").toString());
        list = DataReviewMapper.queryreviewbaseerrorList(map);
        for (Map obj : list) {  
            Clob sc = (Clob) obj.get("TOPOGEOMETRY");
            if(sc!=null){
            String TOPOGEOMETRY = null;
            try {
                TOPOGEOMETRY = ClobToString(sc);
            } catch (SQLException | IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            obj.remove("TOPOGEOMETRY");
            obj.put("TOPOGEOMETRY", TOPOGEOMETRY); 
            }
        }  
        PageInfo pInfo = new PageInfo(list);
        mapData.put("code", 0);
        mapData.put("msg", "");
        mapData.put("data", list);
        mapData.put("count", pInfo.getTotal());
        return mapData;
    }
    public String ClobToString(Clob sc) throws SQLException, IOException {  
        String reString = "";  
        Reader is = sc.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;  
    }
二、使用ClobProxyImpl类来解决(比较好)
    Map map = clubGroMapper.queryClueGroupDetail(map);
    ClobProxyImpl disOrgNoProxy = (ClobProxyImpl)map.remove("distributeOrgNo");//此字段在oracle中为clob类型
    String distributeOrgNo = null;
    if(disOrgNoProxy != null){
        Clob rawClob = disOrgNoProxy .getRawClob();
        distributeOrgNo = rawClob.getSubString(1,(int)rawClob.length());
    }
    map.put("distributeOrgNo",distributeOrgNo);


 

你可能感兴趣的:(Mybatis)