iBATIS中怎么操作Blob與Clob,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)鐘山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎ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)單的例子:
public class Food { private String content; private String id; private byte[] image; private String name; ... }
xml如下:說(shuō)明一下,在resultMap中可以通過(guò)typeHandler來(lái)指定具體的handler.在inline變量中,可以通過(guò)handler來(lái)定義相應(yīng)的typeHandler
﹤sqlMap namespace="Food"﹥ ﹤typeAlias alias="Food" type="org.esoft.hdb.bo.Food"/﹥ ﹤resultMap id="foodResult" class="Food"﹥ ﹤result property="id" column="C_ID"/﹥ ﹤result property="name" column="C_NAME"/﹥ ﹤result property="content" column="C_content" typeHandler="org.springframework.orm.ibatis.support.ClobStringTypeHandler"/﹥ ﹤result property="image" column="C_image" typeHandler="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"/﹥ ﹤/resultMap﹥ ﹤sql id="foodFragment"﹥select C_ID,C_NAME,C_CONTENT,C_IMAGE from T_FOOD﹤/sql﹥ ﹤select id="getAll" resultMap="foodResult"﹥ ﹤include refid="foodFragment"/﹥ ﹤/select﹥ ﹤select id="selectById" parameterClass="string" resultMap="foodResult"﹥ ﹤include refid="foodFragment"/﹥ where C_ID=#id#﹤/select﹥ ﹤insert id="insert" parameterClass="Food"﹥ insert into T_FOOD ( C_ID, C_NAME,C_CONTENT, C_IMAGE) values ( #id#, #name#,#content,handler=org.springframework.orm.ibatis.support.ClobStringTypeHandler#, #image,handler=org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler#) ﹤/insert﹥ ﹤update id="update" parameterClass="Food"﹥ update T_FOOD set C_NAME = #name#, C_CONTENT = #content,handler=org.springframework.orm.ibatis.support.ClobStringTypeHandler#, C_IMAGE = #image,handler=org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler# where C_ID = #id# ﹤/update﹥ ﹤delete id="deleteById" parameterClass="string"﹥ delete from T_FOOD where C_ID = #id# ﹤/delete﹥ ﹤/sqlMap﹥ public interface FoodService { void save(Food food); Food get(String id); /** * @param food */ void update(Food food); } public class FoodServiceImpl implements FoodService { private FoodDAO foodDAO; private DaoCreator creator; public void setCreator(DaoCreator creator) { this.creator = creator; } protected FoodDAO getFoodDAO() { if (foodDAO == null) { foodDAO = (FoodDAO) creator.createDao(FoodDAO.class, Food.class); } return foodDAO; } public Food get(String id) { return getFoodDAO().get(id); } public void save(Food food) { getFoodDAO().save(food); } public void update(Food food) { getFoodDAO().update(food); } } spring xml 配置: 。。。 ﹤bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/﹥ ﹤bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"﹥ ﹤property name="dataSource" ref="dataSource"/﹥ ﹤/bean﹥ ﹤bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥ ﹤property name="dataSource" ref="dataSource"/﹥ ﹤property name="configLocation"﹥ ﹤value﹥SqlMapConfig.xml﹤/value﹥ ﹤/property﹥ ﹤property name="lobHandler" ref="lobHandler"/﹥ ﹤/bean﹥ ﹤bean id="daoCreate" class="org.esoft.hdb.ibatis.IbatisDaoCreator"﹥ ﹤property name="sqlMapClient" ref="sqlMapClient"/﹥ ﹤/bean﹥ ﹤bean id="foodService" class="org.esoft.hdb.service.FoodServiceImpl"﹥ ﹤property name="creator" ref="daoCreate"/﹥ ﹤/bean﹥ ﹤aop:config﹥ ﹤aop:pointcut id="foodServiceMethods" expression="execution(* org.esoft.hdb.service.FoodService.*(..))"/﹥ ﹤aop:advisor advice-ref="txAdvice" pointcut-ref="foodServiceMethods"/﹥ ﹤/aop:config﹥ ﹤tx:advice id="txAdvice" transaction-manager="transactionManager"﹥ ﹤tx:attributes﹥ ﹤tx:method name="*" propagation="REQUIRED"/﹥ ﹤/tx:attributes﹥ ﹤/tx:advice﹥
簡(jiǎn)單的測(cè)試:
save : Food food = new Food(); food.setPk("1"); food.setName("food1"); BufferedInputStream in = new BufferedInputStream(getClass() .getResourceAsStream("/1.gif")); byte[] b = FileCopyUtils.copyToByteArray(in); food.setImage(b); in = new BufferedInputStream(getClass().getResourceAsStream( "/hibernate.cfg.xml")); b = FileCopyUtils.copyToByteArray(in); food.setContent(new String(b)); foodService.save(food); update: Food food = foodService.get("1"); BufferedInputStream in = new BufferedInputStream(getClass() .getResourceAsStream("/jdbc.properties")); byte[] b = FileCopyUtils.copyToByteArray(in); food.setContent(new String(b)); foodService.update(food); food = foodService.get("1"); assertNotNull(food.getImage());
關(guān)于iBATIS中怎么操作Blob與Clob問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
本文題目:iBATIS中怎么操作Blob與Clob
分享鏈接:http://jinyejixie.com/article42/jjjhec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、企業(yè)網(wǎng)站制作、網(wǎng)站營(yíng)銷、品牌網(wǎng)站制作、軟件開(kāi)發(fā)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)