自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iBATIS操作Blob與Clob淺析

開(kāi)發(fā) 后端
iBATIS操作Blob與Clob是如何的過(guò)程呢,那么本文將向你介紹iBATIS操作Blob與Clob的情況。

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

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

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

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

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

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

下面是一個(gè)簡(jiǎn)單的例子:

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

xml如下:說(shuō)明一下,在resultMap中可以通過(guò)typeHandler來(lái)指定具體的handler.在inline變量中,可以通過(guò)handler來(lái)定義相應(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.  
  37.  
  38. public interface FoodService {   
  39.  
  40.      
  41. void save(Food food);   
  42. Food get(String id);   
  43. /**   
  44. * @param food   
  45. */   
  46. void update(Food food);   
  47. }   
  48.  
  49. public class FoodServiceImpl implements FoodService {   
  50. private FoodDAO foodDAO;   
  51.  
  52. private DaoCreator creator;   
  53.  
  54. public void setCreator(DaoCreator creator) {   
  55.     this.creator = creator;   
  56. }   
  57.  
  58. protected FoodDAO getFoodDAO() {   
  59.     if (foodDAO == null) {   
  60.    foodDAO = (FoodDAO) creator.createDao(FoodDAO.class, Food.class);   
  61.     }   
  62.     return foodDAO;   
  63. }   
  64.  
  65. public Food get(String id) {   
  66.     return getFoodDAO().get(id);   
  67. }   
  68. public void save(Food food) {   
  69.     getFoodDAO().save(food);   
  70. }   
  71. public void update(Food food) {   
  72.     getFoodDAO().update(food);   
  73. }   
  74.  
  75. }   
  76.  
  77. spring xml 配置:  
  78.    
  79. 。。。   
  80.  ﹤bean id="lobHandler"   
  81.   class="org.springframework.jdbc.support.lob.DefaultLobHandler"/﹥   
  82.      
  83.    ﹤bean id="transactionManager"   
  84.   class="org.springframework.jdbc.datasource.DataSourceTransactionManager"﹥   
  85.   ﹤property name="dataSource" ref="dataSource"/﹥   
  86.    ﹤/bean﹥   
  87.      
  88.    ﹤bean id="sqlMapClient"   
  89.   class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥   
  90.   ﹤property name="dataSource" ref="dataSource"/﹥   
  91.   ﹤property name="configLocation"﹥   
  92.  ﹤value﹥SqlMapConfig.xml﹤/value﹥   
  93.   ﹤/property﹥   
  94.   ﹤property name="lobHandler" ref="lobHandler"/﹥   
  95.    ﹤/bean﹥   
  96.      
  97.    ﹤bean id="daoCreate" class="org.esoft.hdb.ibatis.IbatisDaoCreator"﹥   
  98.   ﹤property name="sqlMapClient" ref="sqlMapClient"/﹥   
  99.    ﹤/bean﹥   
  100.      
  101.    ﹤bean id="foodService" class="org.esoft.hdb.service.FoodServiceImpl"﹥   
  102.   ﹤property name="creator" ref="daoCreate"/﹥   
  103.    ﹤/bean﹥   
  104.      
  105.      
  106.    ﹤aop:config﹥   
  107.   ﹤aop:pointcut id="foodServiceMethods"   
  108.  expression="execution(* org.esoft.hdb.service.FoodService.*(..))"/﹥   
  109.   ﹤aop:advisor advice-ref="txAdvice" pointcut-ref="foodServiceMethods"/﹥   
  110.    ﹤/aop:config﹥   
  111.    ﹤tx:advice id="txAdvice" transaction-manager="transactionManager"﹥   
  112.   ﹤tx:attributes﹥   
  113.  ﹤tx:method name="*" propagation="REQUIRED"/﹥   
  114.   ﹤/tx:attributes﹥   
  115.    ﹤/tx:advice﹥  

簡(jiǎn)單的測(cè)試:

  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的情況就像你介紹到這里,希望對(duì)你有所幫助。

【編輯推薦】

  1. 避免iBATIS N+1查詢的方法
  2. iBATIS級(jí)聯(lián)解決登錄系統(tǒng)問(wèn)題
  3. iBATIS標(biāo)簽詳解
  4. iBATIS是什么?
  5. iBATIS的優(yōu)、缺點(diǎn)及注意事項(xiàng)淺談
責(zé)任編輯:仲衡 來(lái)源: 互聯(lián)網(wǎng)轉(zhuǎn)載
相關(guān)推薦

2009-07-15 17:01:29

iBATIS操作CLO

2009-07-15 16:42:03

iBATIS讀寫CLO

2011-04-19 09:14:59

Ibatis

2009-07-21 11:12:00

iBATIS配置

2009-07-22 16:27:24

iBATIS配置類iBATIS操作類

2009-07-17 10:08:39

Hibernate與i

2009-07-17 10:32:45

iBATIS MapB

2009-07-22 10:03:11

iBATIS Resu

2009-07-16 09:14:26

iBATIS DAO

2009-07-20 18:00:16

iBATIS DAO事

2009-07-15 17:19:31

iBATIS Ecli

2009-07-15 17:58:07

iBATIS 動(dòng)態(tài)映射

2009-07-16 10:23:30

iBATIS工作原理

2009-07-22 10:42:59

iBATIS Cach

2009-07-16 13:08:09

iBATIS快速創(chuàng)建應(yīng)

2009-07-17 17:05:44

iBATIS緩存cacheModel

2009-07-22 15:21:00

iBATIS SQLM

2009-07-22 09:44:05

iBATIS Para

2009-07-22 16:02:14

iBATIS參數(shù)

2009-07-17 10:59:59

iBATIS接口
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)