1. 程式人生 > >Mybatis查詢oracle之clob型別

Mybatis查詢oracle之clob型別

clob是大欄位,可以儲存文件內容,mybatis查詢clob欄位需要做一些處理,才可以返回字串,可用以下方法處理:

1.public static String getClob(Object o){
  if( o == null ){
   return "";
  }
  
  oracle.sql.CLOB clob = null;
  if(o instanceof oracle.sql.CLOB){
   clob =  (CLOB) o;
  }else{
   try {
    Method method = o.getClass().getMethod("getVendorObj", new Class[]{});
    clob = (CLOB) method.invoke(o);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  
   try {
    Reader reader = clob.getCharacterStream();
    char[] chars = new char[2048];
    int i = 0;
    StringBuffer sb = new StringBuffer();
    while( (i = reader.read(chars)) != -1){
     sb.append(new String(chars).substring(0,i));
    }
    return sb.toString();
   } catch (Exception e) {
    e.printStackTrace();
   }
  return "";
 } 

2.直接在sql上處理:

msbb.bus_num = #{NUM};