最近用到blob类型的字段,在网上看了看,大部分都没成功。自己试验些时间,整理个可以运行的,以便将来用的时候不用在到网上搜来搜去的了!
闲话少说,只将关键代码贴出来
1.表
create table TBL_SYS_BULLIT (
TPI_ID VARCHAR2(10) not null,
TPI_TITLE VARCHAR2(200) not null,
TPI_CONTENT_URL BLOB )
2.实体BulletinVo.java
public class BulletinVo {
private String tpiId;
private String tpiTitle;
private byte[] tpiContentUrl;
public BulletinVo() {
}
public String getTpiId() {
return tpiId;
}
public void setTpiId(String tpiId) {
this.tpiId = tpiId;
}
public String getTpiTitle() {
return tpiTitle;
}
public void setTpiTitle(String tpiTitle) {
this.tpiTitle = tpiTitle;
}
public byte[] getTpiContentUrl() {
return tpiContentUrl;
}
public void setTpiContentUrl(byte[] tpiContentUrl) {
this.tpiContentUrl = tpiContentUrl;
}
}
3.sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- 使用命名空间 用于区分重名问题 -->
<settings useStatementNamespaces="true" />
typeHandler jdbcType="BLOB" javaType="[B"
callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler" />
<typeHandler jdbcType="CLOB" javaType="java.lang.String"
callback="org.springframework.orm.ibatis.support.ClobStringTypeHandler" />
</sqlMapConfig>
4.applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Transaction manager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- LobHandler for Oracle JDBC drivers -->
<!-- (refers to the NativeJdbcExtractor above to get access to native OracleConnections) -->
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor"
lazy-init="true" />
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor" />
</property>
</bean>
<!-- Spring提供的iBatis的SqlMap配置-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
[color=red]<property name="lobHandler">
<ref local="oracleLobHandler" />
</property>
</bean>
</beans>
5.bulletin.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="sysBullit">
<typeAlias alias="BulletinVo" type="包名.BulletinVo" />
<typeAlias alias="iCriteria" type="包名.IbatisCeteria" />
<select id="selectBulletin" parameterClass="iCriteria" resultClass="BulletinVo">
select
SysBullit.TPI_ID as tpiId,
SysBullit.TPI_TITLE as tpiTitle,
SysBullit.TPI_CONTENT_URL as tpiContentUrl
from TBL_SYS_BULLIT SysBullit
where 1=1
</select>
<update id="updateBulletin" parameterClass="BulletinVo">
update TBL_SYS_BULLIT
set
<isNotNull property="tpiId" prepend=",">TPI_ID = #tpiId# </isNotNull>
<isNotNull property="tpiTitle" prepend=",">TPI_TITLE = #tpiTitle# </isNotNull>
<isNotNull property="tpiContentUrl" prepend=",">TPI_CONTENT_URL = #tpiContentUrl:BLOB# </isNotNull>
where TBL_SYS_BULLIT.TPI_ID = #tpiId#
</update>
<insert id="insertBulletin" parameterClass="BulletinVo">
insert into TBL_SYS_BULLIT (
<isNotNull property="tpiId" prepend=",">TPI_ID</isNotNull>
<isNotNull property="tpiTitle" prepend=",">TPI_TITLE</isNotNull>
<isNotNull property="tpiContentUrl" prepend=",">TPI_CONTENT_URL </isNotNull>
)
values (
<isNotNull property="tpiId" prepend=",">#tpiId#</isNotNull>
<isNotNull property="tpiTitle" prepend=",">#tpiTitle#</isNotNull>
<isNotNull property="tpiApprDate" prepend=",">#tpiApprDate#</isNotNull>
<isNotNull property="tpiContentUrl" prepend=",">#tpiContentUrl:BLOB# </isNotNull>
)
</insert>
</sqlMap>