不同数据库对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./,谢谢各位的支持。

【编辑推荐】

  1. 共享database独立Schema构建SAAS平台
  2. ASP通过Oracle Object for OLE对Oracle查询
  3. Oracle跟踪文件分析工具TKPROF使用简介
  4. 如何配置Oracle 10g oem中的主机身份证明
  5. PLSQL Developer8连接Oracle 10g X64版报错的解决
THE END