一:Java工程环境介绍
(1) 工程目录结构
(2) Oracle9i + Oracle10g的驱动 + jdk1.4
(3) 用到的jar包如下:
(4) 数据库表结构
create table IBATIS_BLOB
(
ID VARCHAR2(5) not null,
TITLE VARCHAR2(50),
CONTENT BLOB
)
二:主要代码如下
(1) IbatisBlob.java
package cn.com.victorysoft.vo;
public class IbatisBlob {
private String id;
private String title;
private byte[] content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
}
(2) ibatisblob-sqlmap.xml
select IBATISBLOB_SQ.nextval as id from dual
insert into IBATIS_BLOB(id,content,title) values(#id#,#content:BLOB#,#title#)
update IBATIS_BLOB set title = #title# ,content= #content:BLOB#
where id = #id#
(3) SqlMapConfig.xml
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
说明:红色部分为处理BLOB 类型的TypeHandler 实现类。
(4) beans.xml
http://www.springframework.org/dtd/spring-beans-2.0.dtd">
说明:Spring对LOB的支持是在事务的环境中,所以操作LOB的DAO必须受事务的管理。如果不放在事务的环境中,会报如下异常:
Caused by: java.lang.IllegalStateException: Spring transaction synchronization needs to be active for setting values
(5) 测试类代码 MainTest.java
package cn.com.victorysoft.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.FileCopyUtils;
import cn.com.victorysoft.dao.IBatisBlobDAO;
import cn.com.victorysoft.vo.IbatisBlob;
public class MainTest {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
IBatisBlobDAO batisBlobDAO = (IBatisBlobDAO)ctx.getBean("batisBlobDAO");
//****************插入图片开始*************
IbatisBlob batisBlob = new IbatisBlob();
batisBlob.setTitle("mytitle");
File file = new File("d:/dd.jpg");
FileInputStream fis = new FileInputStream(file);
byte[] b = FileCopyUtils.copyToByteArray(fis);
batisBlob.setContent(b);
batisBlobDAO.insertIbaisBlob(batisBlob);
fis.close();
//****************插入图片结束*************
//****************更新图片开始*************
batisBlob.setId("1");
batisBlob.setTitle("gengxin");
File updateFile = new File("d:/dd.jpg");
FileInputStream updateFis = new FileInputStream(updateFile);
byte[] ub = FileCopyUtils.copyToByteArray(updateFis);
batisBlob.setContent(ub);
batisBlobDAO.updateIbaisBlob(batisBlob);
//****************更新图片结束*************
//***************查询图片并输出************
batisBlob.setId("1");
batisBlob = batisBlobDAO.selectIbaisBlobById(batisBlob);
byte[] bb = batisBlob.getContent();
File outFile = new File("d:/cwb.jpg");
FileOutputStream sout = new FileOutputStream(outFile);
sout.write(bb);
sout.flush();
sout.close();
//***************查询图片并结束************
}
}
说明:本类对BLOB字段的插入、修改、查询做了测试,无任何问题,均可以正确执行。
其中FileCopyUtils为Spring提供的工具类,它提供了许多一步式的静态操作方法,能够将文件内容拷贝到一个目标 byte[]、String 甚至一个输出流或输出文件中。使用起来十分方便。
in iBATIS TypeHandlers that delegate to a Spring LobHandler