日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
iBATIS操作Blob與Clob淺析

這幾天仔細(xì)看了一下iBATIS的文檔,發(fā)現(xiàn)2.2后,iBATIS的改變還是挺大的。對于自定義類型支持的也不錯,這樣對于blob和Clob數(shù)據(jù)的處理也就簡單多了。
 
不過在spring 中已經(jīng)提供了很好的實現(xiàn),所以這又省去了很多的功夫,接下來看看iBATIS是如何支持Clob和blob的。

創(chuàng)新互聯(lián)是專業(yè)的尼河口網(wǎng)站建設(shè)公司,尼河口接單;提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行尼河口網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!

iBATIS提供了TypeHandler接口,用于處理數(shù)據(jù)類型,基本的實現(xiàn)類為BaseTypeHandler

在spring 中,提供了AbstractLobTypeHandler作為基礎(chǔ)類,并且提供了相應(yīng)的模版方法,所有的工作由LobHandler處理。

BlobByteArrayTypeHandler 主要用于處理blob類型數(shù)據(jù),使用byte[]來映射相應(yīng)的Blob

ClobStringTypeHandler 用于處理Clob類型數(shù)據(jù),使用字符串來映射Clob

有一點需要注意的是,AbstractLobTypeHandler中實現(xiàn)了事務(wù)支持,需要用來釋放相應(yīng)的資源,所以一定需要在事務(wù)環(huán)境中進(jìn)行。

下面是一個簡單的例子:

 
 
 
  1. public class Food { 
  2. private String content; 
  3. private String id; 
  4. private byte[] image; 
  5. private String name;   
  6.     ... 

xml如下:說明一下,在resultMap中可以通過typeHandler來指定具體的handler.在inline變量中,可以通過handler來定義相應(yīng)的typeHandler

 
 
 
  1. ﹤sqlMap namespace="Food"﹥ 
  2.    
  3.    ﹤typeAlias alias="Food" type="org.esoft.hdb.bo.Food"/﹥ 
  4.    ﹤resultMap id="foodResult" class="Food"﹥ 
  5.   ﹤result property="id" column="C_ID"/﹥ 
  6.   ﹤result property="name" column="C_NAME"/﹥ 
  7.   ﹤result property="content" column="C_content" 
  8.  typeHandler="org.springframework.orm.ibatis.support.ClobStringTypeHandler"/﹥ 
  9.   ﹤result property="image" column="C_image" 
  10.  typeHandler="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"/﹥ 
  11.    ﹤/resultMap﹥ 
  12.    ﹤sql id="foodFragment"﹥select C_ID,C_NAME,C_CONTENT,C_IMAGE from T_FOOD﹤/sql﹥ 
  13.   ﹤select id="getAll" resultMap="foodResult"﹥ 
  14.   ﹤include refid="foodFragment"/﹥ 
  15.    ﹤/select﹥ 
  16.    ﹤select id="selectById" parameterClass="string" resultMap="foodResult"﹥ 
  17.   ﹤include refid="foodFragment"/﹥ where C_ID=#id#﹤/select﹥ 
  18.    
  19.    ﹤insert id="insert" parameterClass="Food"﹥ insert into T_FOOD ( C_ID, 
  20.   C_NAME,C_CONTENT, C_IMAGE) values ( #id#, 
  21.   #name#,#content,handler=org.springframework.orm.ibatis.support.ClobStringTypeHandler#, 
  22.   #image,handler=org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler#) 
  23.   ﹤/insert﹥ 
  24.    
  25.    ﹤update id="update" parameterClass="Food"﹥ update T_FOOD set C_NAME = #name#, 
  26.   C_CONTENT = 
  27.   #content,handler=org.springframework.orm.ibatis.support.ClobStringTypeHandler#, 
  28.   C_IMAGE = 
  29.   #image,handler=org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler# 
  30.   where C_ID = #id# ﹤/update﹥ 
  31.    
  32.    ﹤delete id="deleteById" parameterClass="string"﹥ delete from T_FOOD where C_ID = #id# 
  33.   ﹤/delete﹥ 
  34.    
  35. ﹤/sqlMap﹥ 
  36. public interface FoodService { 
  37.    
  38. void save(Food food); 
  39. Food get(String id); 
  40. /** 
  41. * @param food 
  42. */ 
  43. void update(Food food); 
  44. public class FoodServiceImpl implements FoodService { 
  45. private FoodDAO foodDAO; 
  46. private DaoCreator creator; 
  47. public void setCreator(DaoCreator creator) { 
  48.     this.creator = creator; 
  49. protected FoodDAO getFoodDAO() { 
  50.     if (foodDAO == null) { 
  51.    foodDAO = (FoodDAO) creator.createDao(FoodDAO.class, Food.class); 
  52.     } 
  53.     return foodDAO; 
  54. public Food get(String id) { 
  55.     return getFoodDAO().get(id); 
  56. public void save(Food food) { 
  57.     getFoodDAO().save(food); 
  58. public void update(Food food) { 
  59.     getFoodDAO().update(food); 
  60. spring xml 配置:
  61.  
  62. 。。。 
  63.  ﹤bean id="lobHandler" 
  64.   class="org.springframework.jdbc.support.lob.DefaultLobHandler"/﹥ 
  65.    
  66.    ﹤bean id="transactionManager" 
  67.   class="org.springframework.jdbc.datasource.DataSourceTransactionManager"﹥ 
  68.   ﹤property name="dataSource" ref="dataSource"/﹥ 
  69.    ﹤/bean﹥ 
  70.    
  71.    ﹤bean id="sqlMapClient" 
  72.   class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥ 
  73.   ﹤property name="dataSource" ref="dataSource"/﹥ 
  74.   ﹤property name="configLocation"﹥ 
  75.  ﹤value﹥SqlMapConfig.xml﹤/value﹥ 
  76.   ﹤/property﹥ 
  77.   ﹤property name="lobHandler" ref="lobHandler"/﹥ 
  78.    ﹤/bean﹥ 
  79.    
  80.    ﹤bean id="daoCreate" class="org.esoft.hdb.ibatis.IbatisDaoCreator"﹥ 
  81.   ﹤property name="sqlMapClient" ref="sqlMapClient"/﹥ 
  82.    ﹤/bean﹥ 
  83.    
  84.    ﹤bean id="foodService" class="org.esoft.hdb.service.FoodServiceImpl"﹥ 
  85.   ﹤property name="creator" ref="daoCreate"/﹥ 
  86.    ﹤/bean﹥ 
  87.    
  88.    
  89.    ﹤aop:config﹥ 
  90.   ﹤aop:pointcut id="foodServiceMethods" 
  91.  expression="execution(* org.esoft.hdb.service.FoodService.*(..))"/﹥ 
  92.   ﹤aop:advisor advice-ref="txAdvice" pointcut-ref="foodServiceMethods"/﹥ 
  93.    ﹤/aop:config﹥ 
  94.    ﹤tx:advice id="txAdvice" transaction-manager="transactionManager"﹥ 
  95.   ﹤tx:attributes﹥ 
  96.  ﹤tx:method name="*" propagation="REQUIRED"/﹥ 
  97.   ﹤/tx:attributes﹥ 
  98.    ﹤/tx:advice﹥ 

簡單的測試:

 
 
 
  1. save : 
  2.     Food food = new Food(); 
  3.     food.setPk("1"); 
  4.     food.setName("food1"); 
  5.     BufferedInputStream in = new BufferedInputStream(getClass() 
  6.   .getResourceAsStream("/1.gif")); 
  7.     byte[] b = FileCopyUtils.copyToByteArray(in); 
  8.     food.setImage(b); 
  9.   in = new BufferedInputStream(getClass().getResourceAsStream( 
  10.   "/hibernate.cfg.xml")); 
  11.     b = FileCopyUtils.copyToByteArray(in); 
  12.     food.setContent(new String(b)); 
  13.     foodService.save(food); 
  14. update: 
  15. Food food = foodService.get("1"); 
  16.     BufferedInputStream in = new BufferedInputStream(getClass() 
  17.   .getResourceAsStream("/jdbc.properties")); 
  18.     byte[] b = FileCopyUtils.copyToByteArray(in); 
  19.     food.setContent(new String(b)); 
  20.     foodService.update(food); 
  21.     food = foodService.get("1"); 
  22.     assertNotNull(food.getImage());

iBATIS操作Blob與Clob的情況就像你介紹到這里,希望對你有所幫助。


網(wǎng)站標(biāo)題:iBATIS操作Blob與Clob淺析
文章網(wǎng)址:http://m.5511xx.com/article/cdgiego.html