不同数据库对blob字段的处理代码演示

数据库
本文主要介绍了不同的数据库或程序开发语言对BLOB字段的处理过程详细代码,通过代码让我们来了解它们的处理过程吧,希望能对读者有所帮助。

spring、Ibatis、mysql和java处理blob字段的方法是不同的,本文给出了处理过程的详细代码,现在一一开始介绍。

1)spring配置文件:

 

  1. <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/> 
  2.  
  3. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
  4.  
  5. <property name="configLocation" value="classpath:conf/sqlMapConfig.xml"></property> 
  6.  
  7. <property name="dataSource" ref="dataSource2"></property> 
  8.  
  9. <property name="lobHandler" ref="lobHandler"></property> 
  10.  
  11. </bean> 

 

2)Ibatis配置文件:

 

  1. sqlMapConfig.xml:  
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?> 
  4.  
  5. <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  
  6.  
  7. "http://www.ibatis.com/dtd/sql-map-config-2.dtd"> 
  8.  
  9. <sqlMapConfig>   
  10.  
  11. <properties resource="conf/serverity.properties" />   
  12.  
  13. <settings  useStatementNamespaces="true" cacheModelsEnabled="false" maxRequests="256" 
  14.  
  15. maxSessions="64" maxTransactions="16"/> 
  16.  
  17. <typeHandler  jdbcType="BLOB" javaType="[B"callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"/> 
  18.  
  19. <sqlMap resource="conf/sqlmap/monitorSqlMap.xml" /> 
  20.  
  21. </sqlMapConfig> 
  22.  
  23. monitorSqlMap.xml:  
  24.  
  25. <?xml version="1.0" encoding="UTF-8"?> 
  26.  
  27. <!DOCTYPE sqlMap  
  28.  
  29. PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"  
  30.  
  31. "http://www.ibatis.com/dtd/sql-map-2.dtd"> 
  32.  
  33. <sqlMap namespace="favMonitor"> 
  34.  
  35. <select id="queryVoBytes" parameterClass="java.util.Map" resultClass="[B"> 
  36.  
  37. select voListBytes from t_favMonitor where userId=#userId# and reqName=#reqName#  
  38.  
  39. </select> 
  40.  
  41. </sqlMap> 

 

3) mysql数据库:

 

  1. table:  t_favMonitor(userid int, reqName varchar, voListBytes blob) 

 

4) java存取:

 

  1. private static class ByteObjectUtil{  
  2.  
  3. static byte[] convertObj2ByteArray(Object obj) throws IOException{  
  4.  
  5. ByteArrayOutputStream baos=null;  
  6.  
  7. ObjectOutputStream oos=null;  
  8.  
  9. try {  
  10.  
  11. baos=new ByteArrayOutputStream();  
  12.  
  13. oos=new ObjectOutputStream(baos);  
  14.  
  15. oos.writeObject(obj);  
  16.  
  17. return baos.toByteArray();  
  18.  
  19. } catch (IOException e) {  
  20.  
  21. throw new IOException(e);  
  22.  
  23. }finally{  
  24.  
  25. baos.close();  
  26.  
  27. oos.close();  
  28.  
  29. }  
  30.  
  31. }  
  32.  
  33. static Object readObjFromByteArray(byte[] ob) throws IllegalStateException, IOException, ClassNotFoundException{  
  34.  
  35. if(ob==null||ob.length==0)  
  36.  
  37. throw new IllegalStateException("parameter byte[] ob is empty!");  
  38.  
  39. ByteArrayInputStream bais=new ByteArrayInputStream(ob);  
  40.  
  41. ObjectInputStream ois=null;  
  42.  
  43. try {  
  44.  
  45. ois=new ObjectInputStream(bais);  
  46.  
  47. return ois.readObject();  
  48.  
  49. } catch (IOException e) {  
  50.  
  51. throw new IOException(e);  
  52.  
  53. }finally{  
  54.  
  55. bais.close();  
  56.  
  57. ois.close();  
  58.  
  59. }  
  60.  
  61. }  
  62.  

 

关于处理blob字段的方法就介绍到这里,如果您想了解更多数据库方面的知识,可以到这里看一看:http://database.51cto.com/,谢谢各位的支持。

【编辑推荐】

  1. 共享database独立Schema构建SAAS平台
  2. ASP通过Oracle Object for OLE对Oracle查询
  3. Oracle跟踪文件分析工具TKPROF使用简介
  4. 如何配置Oracle 10g oem中的主机身份证明
  5. PLSQL Developer8连接Oracle 10g X64版报错的解决
责任编辑:赵鹏 来源: 博客园
相关推荐

2010-04-23 14:32:01

Oracle数据库

2010-04-29 10:56:46

Oracle数据库

2010-04-20 10:12:42

OraclePL

2017-10-23 16:06:41

数据库MySQL复制中断

2011-07-11 14:36:10

BinlogMysql

2010-05-21 15:33:54

MySQL text

2010-05-12 18:41:34

MySQL数据库

2010-05-28 14:51:47

MySQL数据库

2010-04-14 15:58:17

Oracle程序开发

2009-08-25 16:01:32

C#.NET连接数据库

2011-07-12 16:41:14

mysql处理异常

2019-10-24 08:01:45

MySQL迁移数据库

2011-08-18 18:34:00

Oracle数据库创建自增字段

2017-05-25 10:23:13

数据a表b表

2010-04-19 09:26:04

Oracle数据库

2010-06-04 09:33:28

连接MySQL数据库

2010-06-09 17:36:45

MySQL数据库同步

2010-06-04 10:40:55

AJAX MySQL

2010-06-12 09:53:19

2010-06-12 17:55:23

MySQL数据库同步
点赞
收藏

51CTO技术栈公众号