文章目录 Mybatis操作Oracle中的Clob和Blob字段 1. Oracle中创建测试的表结构:Byte_Array_Test 2. Mybatis配置MapperXml,用Map<String, Object>接受Clob和Blob的数据,如下图: 3. 代码中获取Clob和Blob的值 3-1 接收Blob和Clob数据代码: 3-2 Blob实例 3-3 Clob实例 3-4 扩展: Blob转byte[],Clob转string,不考虑对象内置方法的话,亦可用如下自定义方法 4. 将Blob生成的字节数组byte[],写入到文件,如:test.jpg
Mybatis操作Oracle中的Clob和Blob字段
说明:CLOB和BLOB的区别,这两个被统称为LOB,即Large Object(大对象类型)最本质的区别:CLOB的C,可以理解为Char,保存的是字符大对象BLOB的B,即Binary,保存的是二进制大对象CLOB应该转换成StringBLOB应该转换成byte [ ]
1. Oracle中创建测试的表结构:Byte_Array_Test
2. Mybatis配置MapperXml,用Map<String, Object>接受Clob和Blob的数据,如下图:
3. 代码中获取Clob和Blob的值
3-1 接收Blob和Clob数据代码:
Blob blobVal = ( Blob ) map. get ( "BLOB" ) ;
Clob clobVal = ( Clob ) map. get ( "CLOB" ) ;
3-2 Blob实例
@RequestMapping ( "getArr/{id}" )
public String getArr ( @PathVariable String id) throws SQLException { Map < String , Object > map = byteArrayTestService. find ( id) ; Blob blob = ( Blob ) map. get ( "BLOB" ) ; byte [ ] bytes1 = blob. getBytes ( 1 , ( int ) blob. length ( ) ) ; return "success" ;
}
3-3 Clob实例
@RequestMapping ( "getArr/{id}" )
public String getArr ( @PathVariable String id) throws SQLException { Map < String , Object > map = byteArrayTestService. find ( id) ; Clob clob = ( Clob ) map. get ( "CLOB" ) ; String clobStr1 = clob. getSubString ( 1 , ( int ) clob. length ( ) ) ; return "success" ;
}
3-4 扩展: Blob转byte[],Clob转string,不考虑对象内置方法的话,亦可用如下自定义方法
private byte [ ] blobToBytes ( Blob blob) { BufferedInputStream is = null ; try { is = new BufferedInputStream ( blob. getBinaryStream ( ) ) ; byte [ ] bytes = new byte [ ( int ) blob. length ( ) ] ; int len = bytes. length; int offset = 0 ; int read = 0 ; while ( offset < len && ( read = is. read ( bytes, offset, len - offset) ) >= 0 ) { offset += read; } return bytes; } catch ( Exception e) { return null ; } finally { try { is. close ( ) ; is = null ; } catch ( IOException e) { return null ; } }
}
public static String clobToString ( Clob clob) { try { Reader inStreamDoc = clob. getCharacterStream ( ) ; char [ ] tempDoc = new char [ ( int ) clob. length ( ) ] ; inStreamDoc. read ( tempDoc) ; inStreamDoc. close ( ) ; return new String ( tempDoc) ; } catch ( IOException e) { e. printStackTrace ( ) ; } catch ( SQLException es) { es. printStackTrace ( ) ; } return null ;
}
4. 将Blob生成的字节数组byte[],写入到文件,如:test.jpg
public class FileUtil { public static boolean save2File ( String fname, byte [ ] msg) { OutputStream fos = null ; try { File file = new File ( fname) ; File parent = file. getParentFile ( ) ; boolean bool; if ( ( ! parent. exists ( ) ) && ( ! parent. mkdirs ( ) ) ) { return false ; } fos = new FileOutputStream ( file) ; fos. write ( msg) ; fos. flush ( ) ; return true ; } catch ( FileNotFoundException e) { return false ; } catch ( IOException e) { File parent; return false ; } finally { if ( fos != null ) { try { fos. close ( ) ; } catch ( IOException e) { } } } } public static void main ( String [ ] args) { String msgStr = "我是java爱好者,测试数据" ; byte [ ] bytes = msgStr. getBytes ( ) ; String filename = "D:\\test\\test.txt" ; boolean flag = FileUtil . save2File ( filename, bytes) ; System . out. println ( "flag = " + flag ) ; }
}