首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ibatis blob字段处置

2012-10-24 
ibatis blob字段处理发现2.2后,ibatis的改变还是挺大的。对于自定义类型支持的也不错,这样对于blob和clob数

ibatis blob字段处理
   发现2.2后,ibatis的改变还是挺大的。对于自定义类型支持的也不错,这样对于blob和clob数据的处理也就简单多了。
    不过在spring 中已经提供了很好的实现,所以这又省去了很多的功夫,接下来看看ibatis是如何支持clob和blob的。
不过在spring 中已经提供了很好的实现,所以这又省去了很多的功夫,接下来看看ibatis是如何支持clob和blob的。

    ibatis提供了TypeHandler接口,用于处理数据类型,基本的实现类为BaseTypeHandler
    在spring 中,提供了AbstractLobTypeHandler作为基础类,并且提供了相应的模版方法,所有的工作由LobHandler处理。
    BlobByteArrayTypeHandler 主要用于处理blob类型数据,使用byte[]来映射相应的blob
    ClobStringTypeHandler 用于处理clob类型数据,使用字符串来映射Clob
    有一点需要注意的是,AbstractLobTypeHandler中实现了事务支持,需要用来释放相应的资源,所以一定需要在事务环境中进行。

下面是一个简单的例子:

java 代码
public class Food {  
    private String content;  
  
    private String id;  
  
    private byte[] image;  
  
    private String name;      
         ...  
}  


xml如下:说明一下,在resultMap中可以通过typeHandler来指定具体的handler.在inline变量中,可以通过handler来定义相应的typeHandler
xml 代码
<sqlMap namespace="Food">  
      
    <typeAlias alias="Food" type="org.esoft.hdb.bo.Food"/>  
    <resultMap id="foodResult" 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" parameterresultMap="foodResult">  
        <include refid="foodFragment"/> where C_ID=#id#</select>  
      
    <insert id="insert" parameterparameterparameterref="dataSource"/>  
    </bean>  
      
    <bean id="sqlMapClient"  
        ref="dataSource"/>  
        <property name="configLocation">  
            <value>SqlMapConfig.xml</value>  
        </property>  
        <property name="lobHandler" ref="lobHandler"/>  
    </bean>  
      
    <bean id="daoCreate" ref="sqlMapClient"/>  
    </bean>  
      
    <bean id="foodService" 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>  


简单的测试:
java 代码
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());  

热点排行