使用ibatis调用数据库中blob类型的数据
目标:
在oracle里建一个字段,存储为一个blob类型,用于存储图片或者是其它格式的流信息或大数据,数据库中间件使用ibatis,主要做图片的存储使用,服务器使用weblogic
实现的时候碰到的问题:
ibatis对blob类型的数据有一个专门的支持.需要配置相关的配置文件,框架中使用了部分Spring的配置,在读出的图片进行写入的时候碰到问题为不能正确write到页面上,后使用纯servlet进行解决。此为weblogic对文件有拦截
具体配置如下。
ApplicationContext.xml中增加以下配置
<bean id="transactionManager" abstract="true"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="proxyTargetClass"> <value>false</value> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>
?在ibatis的SqlMap中增加以下配置
<!-- 添加对blob数据类型 的支持 --><typeHandler jdbcType="BLOB" javaType="[B" callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler" />
MYDEMOPLUS.xml配置文件信息,主要是在里边加了一个别名的信息,映射一个pojo
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN""http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap namespace="ceshi"> <cacheModel id="cacheDCWJ_USER" type="LRU"><!-- 设定缓存有效期为10秒 --><flushInterval minutes="10" /><!-- 本缓存中容纳的最大数据对象数量 --><property name="size" value="100" /></cacheModel> <typeAlias alias="demoResult" type="net.htjs.zxks.pojo.CeShi" /> <!-- 查询语句 ,跟据id查询数据库中的相应数据--><select id="showTable" parameterresultresultClass = "demoResult">select vid from ceshi</select><!-- 另外一个测试用的select --><select id="select_all_id" parameterresultClass = "demoResult">select * from ceshi</select><!-- 插入一个图片进数据库 --><insert id="insert_into" parametername="code">import java.io.Serializable;public class CeShi implements Serializable{String vid;String name;//String ceshi;byte[] ceshi;public String getVid() {return vid;}public void setVid(String vid) {this.vid = vid;}public String getName() {return name;}public void setName(String name) {this.name = name;}/**public String getCeshi() {return ceshi;}public void setCeshi(String ceshi) {this.ceshi = ceshi;}**/public byte[] getCeshi() {return ceshi;}public void setCeshi(byte[] ceshi) {this.ceshi = ceshi;}}?这样配置上基本己经完成,然后就是java代码的编写,可以通过
out = response.getOutputStream();?
??? ???????? out.write(text.getCeshi());
??? ???????? out.flush();
??? ???????? out.close();
这种方法在页面上读出图片信息