这几天仔细看了一下ibatis的文档,发现2.2后,ibatis的改变还是挺大的。对于自定义类型支持的也不错,这样对于blob和clob数据的处理也就简单多了。
不过在spring 中已经提供了很好的实现,所以这又省去了很多的功夫,接下来看看ibatis是如何支持clob和blob的。
ibatis提供了TypeHandler接口,用于处理数据类型,基本的实现类为BaseTypeHandler
在spring 中,提供了AbstractLobTypeHandler作为基础类,并且提供了相应的模版方法,所有的工作由LobHandler处理。
BlobByteArrayTypeHandler 主要用于处理blob类型数据,使用byte[]来映射相应的blob
ClobStringTypeHandler 用于处理clob类型数据,使用字符串来映射Clob
有一点需要注意的是,AbstractLobTypeHandler中实现了事务支持,需要用来释放相应的资源,所以一定需要在事务环境中进行。
下面是一个简单的例子:
public class Food {
private String content;
private String id;
private byte[] image;
private String name;
...
}
xml如下:说明一下,在resultMap中可以通过typeHandler来指定具体的handler.在inline变量中,可以通过handler来定义相应的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>
简单的测试:
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());
分享到:
相关推荐
ibatis 读取oracle clob类型
NULL 博文链接:https://huibin.iteye.com/blog/804070
NULL 博文链接:https://bijian1013.iteye.com/blog/2233121
springmvc ibatis 整合, 解决BLOB,CLOB等大字段的问题,内置查询缓存 和解决SpringMVC 返回JSON下载,乱码等问题 内部并没有实际的项目,只是一个搭建好的环境,方便较少大家时间, 并提供了一个DEMO ,紧为不了解...
ibatis学习 ibatis总结 ibatis ibatis ibatis
Ibatis.net学习和实例Ibatis.net学习和实例Ibatis.net学习和实例Ibatis.net学习和实例
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 ...
公司的大部分项目都开始使用IBatis作为O/R Mapping了,但是在使用的过程中也发现了很多不方便和存在...想了很多办法,都没能很好的避免这个问题,无意间在javaeye上看到了《使ibatis支持hibernate式的物理分页》这篇文章,
但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比,iBATIS独辟蹊径“iBATIS不是试图从根本上掩盖SQL,实际上,它是在拥抱SQL。” <br>但别犯愁:SQL本身具备...
ibatis 开发指南 和 iBATIS-SqlMaps两本图书
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
其中包含iBatis精讲PDF iBatis实例 iBatis开发指南 iBatis分页 spring+iBatis处理1对多数据表实例等等若干帮助文档。希望可以帮助大家学习iBatis...
iBatis和Spring整合 iBatis和Spring整合
ibatis demo,ibatis例子,ibatis示例
iBATIS 级联iBATIS 级联iBATIS 级联
ibatis的配置文件和映射文件,以及ibatis的应用
ibatis的原码 ibatis源码 ibatis源码 ibatis源码
iBatis和Hibernate的区别
书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。《iBATIS实战》的最后给出了一个设计优雅、层次清晰的示例程序JGameStore,该...