Ibatis-spring-mysql 處理blob欄位

來源:互聯網
上載者:User

1)spring設定檔:

 <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
 
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:conf/sqlMapConfig.xml"></property>
  <property name="dataSource" ref="dataSource2"></property>
  <property name="lobHandler" ref="lobHandler"></property>
 </bean>

2) Ibatis設定檔:

sqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig> 
 <properties resource="conf/serverity.properties" /> 
 <settings  useStatementNamespaces="true" cacheModelsEnabled="false" maxRequests="256" maxSessions="64" maxTransactions="16"/>
 <typeHandler  jdbcType="BLOB" javaType="[B" callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"/>
 <sqlMap resource="conf/sqlmap/monitorSqlMap.xml" />
</sqlMapConfig>

monitorSqlMap.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="favMonitor">
 <select id="queryVoBytes" parameterClass="java.util.Map" resultClass="[B">
  select voListBytes from t_favMonitor where userId=#userId# and reqName=#reqName#
 </select>
</sqlMap>

3) mysql資料庫:

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

4) java存取:

private static class ByteObjectUtil{
  
  static byte[] convertObj2ByteArray(Object obj) throws IOException{
   ByteArrayOutputStream baos=null;
   ObjectOutputStream oos=null;
   try {
    baos=new ByteArrayOutputStream();
    oos=new ObjectOutputStream(baos);
    oos.writeObject(obj);
    return baos.toByteArray();
   } catch (IOException e) {
    throw new IOException(e);
   }finally{
    baos.close();
    oos.close();
   }
  }
  
  
  static Object readObjFromByteArray(byte[] ob) throws IllegalStateException, IOException, ClassNotFoundException{
   if(ob==null||ob.length==0)
    throw new IllegalStateException("parameter byte[] ob is empty!");
   ByteArrayInputStream bais=new ByteArrayInputStream(ob);
   ObjectInputStream ois=null;
   try {
    ois=new ObjectInputStream(bais);
    return ois.readObject();
   } catch (IOException e) {
    throw new IOException(e);
   }finally{
    bais.close();
    ois.close();
   }
  }
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.