[转载]org.springframework.jdbc.support.lob.
Struts+Spring+Hibernate处理Lob(Blob,Clob)
在使用struts+spring+hibernate的开发中,有些时候用户会有数据库存储文件的需求,在数据库中一般会采用
Blob字段或Clob字段来存储二进制图片、流媒体或文件。现就将在实际开发中遇到的问题及解决方法告之。
一、问题需求:
二、解决方法:
FormFile(org.apache.struts.upload.FormFile)类型,即使是使用了动态Form同理也是使用该类型.
提示:同时要将form标签修改为<html:form action="/Action.do" method="post" enctype="multipart/form-data">
以支持文件上传后取得上传数据。在给持久对象赋值时只需调用(FormFile) form.get("property")).getFileData();
方法即可返回byte[]。
org.springframework.orm.hibernate3.support.ClobStringType、clob、blob、binary。什么时候用BlobBYteArrayType
什么时候用ClobStringType?一般如果要处理的对象是数据库字段类型是blob时(主要是图片,二进制对象等),
映射文件设置为:org.springframework.orm.hibernate3.support.BlobByteArrayType 数据库字段是clob(大文本对象)
类型时,要将java的属性的类型定为String,映射文件设置为: org.springframework.orm.hibernate3.support.ClobStringType。
簀ava.lang.IllegalStateException: No LobHandler found for configuration - lobHandler property
must be set on LocalSessionFactoryBean异常。配置如下:
则可以使用OracleLobHandler。因为Oracle9i处理lob的方式和不太一样,所以这里要用spring提供的SimpleNativeJdbcExtracto
<bean id="nativeJdbcExtractor" lazy-init="true"
SimpleNativeJdbcExtracto
<bean id="lobHandler" lazy-init="true"
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
</bean>
同时还应该使用对应的JDBC驱动。
Clob字段定义:<property name="属性名" column="列名" type="org.springframework.orm.hibernate.support.ClobStringType"
org.springframework.jdbc.UncategorizedSQLExceptio
the max_allowed_packet' variable.根据提示可以修改mysql数据库的max_allowed_packet属性大小。
打开my.ini文件中找到# SERVER SECTION后在port=3306下面加上max_allowed_packet=?M 修改完后重启mysql服务即可成功上传。
Spring让LOB数据操作变得简单易行
虽然 JDBC 定义了两个操作 LOB 类型的接口:java.sql.Blob 和 java.sql.Clob,但有些厂商的 JDBC 驱动程序并不支持这两个接口。为此,Spring 定义了一个独立于 java.sql.Blob/Clob 的 LobCreator 接口,以统一的方式操作各种数据库的 LOB 类型数据。因为 LobCreator 本身持有 LOB 所对应的数据库资源,所以它不是线程安全的,一个 LobCreator 只能操作一个 LOB 数据。
为了方便在 PreparedStatement 中使用 LobCreator,您可以直接使用 JdbcTemplate#execute(String sql,AbstractLobCreatingPrepa
方法 | 说明 |
---|---|
void close() | 关闭会话,并释放 LOB 资源 |
void setBlobAsBinaryStream(PreparedStatement ps, int paramIndex, InputStream contentStream, int contentLength) | 通过流填充 BLOB 数据 |
void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content) | 通过二进制数据填充 BLOB 数据 |
void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength) | 通过 Ascii 字符流填充 CLOB 数据 |
void setClobAsCharacterStream | 通过 Unicode 字符流填充 CLOB 数据 |
void setClobAsString(PreparedStatement ps, int paramIndex, String content) | 通过字符串填充 CLOB 数据 |
LobHandler
LobHandler 接口为操作 BLOB/CLOB 提供了统一访问接口,而不管底层数据库究竟是以大对象的方式还是以一般数据类型的方式进行操作。此外,LobHandler 还充当了 LobCreator 的工厂类。
大部分数据库厂商的 JDBC 驱动程序(如 DB2)都以 JDBC 标准的 API 操作 LOB 数据,但 Oracle
方法 | 说明 |
---|---|
InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) | 从结果集中返回 InputStream,通过 InputStream 读取 BLOB 数据 |
byte[] getBlobAsBytes(ResultSet rs, int columnIndex) | 以二进制数据的方式获取结果集中的 BLOB 数据; |
InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) | 从结果集中返回 InputStream,通过 InputStreamn 以 Ascii 字符流方式读取 BLOB 数据 |
Reader getClobAsCharacterStream | 从结果集中获取 Unicode 字符流 Reader,并通过 Reader以Unicode 字符流方式读取 CLOB 数据 |
String getClobAsString(ResultSet rs, int columnIndex) | 从结果集中以字符串的方式获取 CLOB 数据 |
LobCreator getLobCreator() | 生成一个会话相关的 LobCreator 对象 |