`

SSH对blob的支持

    博客分类:
  • SSH
阅读更多
刚刚解决一个图片存入数据库的问题,留个记录备查。

1、主要步骤
1.1配置hibernate:
1.1.1、配置hibernate的持久类文件中对应的字段为byte[]类型
1.1.2、配置hibernate的类映射文件中对应的字段type为 
     org.springframework.orm.hibernate3.support.BlobByteArrayType

1.2、配置spring:
1.2.1、配置nativeJdbcExtractor,如下:  
     <bean   id="nativeJdbcExtractor"                class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
1.2.2、配置oracleLobHandler
     <bean id="oracleLobHandler"   class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
         <property name="nativeJdbcExtractor">
             <ref bean="nativeJdbcExtractor" />
         </property>
    </bean>
1.2.3、把oracleLobHandler加入sessionFactory中:
     <property name="lobHandler">
         <ref bean="oracleLobHandler" />
     </property


2、源代码
2.1.1、table_test_image
create table TABLE_TEST_IMAGE
(
  ID  VARCHAR2(40),
  FID VARCHAR2(40),
  IMG BLOB
)


2.1.2、Image.java
package ie.search.test.model;

public class Image {
	private String id;
	private String fid;
	private byte[] img;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getFid() {
		return fid;
	}
	public void setFid(String fid) {
		this.fid = fid;
	}
	public byte[] getImg() {
		return img;
	}
	public void setImg(byte[] img) {
		this.img = img;
	}
}

2.1.3、Image.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="ie.search.test.model.Image" table="TABLE_TEST_IMAGE" >
        <id name="id" type="java.lang.String">
            <column name="ID" length="40" />
              <generator class="sequence"> 
                <param name="sequence">SEQ_TEST_IMAGE</param> 
              </generator>
        </id>
        
         <property name="fid" type="string">
            <column name="FID" length="40">
                <comment>外键</comment>
            </column>
        </property>
        
        <property name="img" type="org.springframework.orm.hibernate3.support.BlobByteArrayType">
            <column name="IMG" >
                <comment>图片</comment>
            </column>
        </property>
     
    </class>
</hibernate-mapping>

2.1.4、spring配置文件片段
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
	
	<!-- for Oracle9i ojdbc14.jar测试通过 -->
	<bean id="nativeJdbcExtractor" lazy-init="true" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"/>
	<bean id="lobHandler" lazy-init="true"   class="org.springframework.jdbc.support.lob.OracleLobHandler">
	        <property name="nativeJdbcExtractor">
	            <ref bean="nativeJdbcExtractor"/>
	        </property>
	</bean> 
	<!-- Oracle9i over -->
	
	<!-- for Oracle10g ojdbc14.jar测试通过-->
	<!--
	<bean id="lobHandler" lazy-init="true"   class="org.springframework.jdbc.support.lob.DefaultLobHandler">
	</bean> 
	-->
	<!-- Oracle10g over -->
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />	
		<property name="lobHandler" ref="lobHandler"/>
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath*:ie/bsp/modules/commonquery/model/</value>
				<value>classpath*:ie/bsp/modules/reference/model/</value>
				<value>classpath*:ie/bsp/modules/commcomponent/model/</value>
				<value>classpath*:ie/bsp/modules/zone/model/</value>				
				<value>classpath*:ie/search/feature/featureTick/model/</value>
				<value>classpath*:ie/search/feature/featureMosquito/model/</value>
				<value>classpath*:ie/search/vector/tick/model/</value>
				<value>classpath*:ie/search/feature/featureMouse/model/</value>
				<value>classpath*:ie/search/feature/featureFlea/model/</value>
				<value>classpath*:ie/perm/model/</value>

				

				
			</list>
		</property>

		<property name="hibernateProperties">

			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				
			</props>
		</property>


	</bean>
	</beans>


2.1.5、存取图片的action代码段
public String saveImage(){
		FileInputStream fis=null;
		Image image=new Image();
		image.setFid("1");
		byte[] img=null;
		try {
			File f=new File("d:\\upload\\-1.jpg");
			fis=new FileInputStream(f);
			byte[] b=new byte[fis.available()];
			fis.read(b);
			fis.close();
			img=b;
			image.setImg(img);
			imageDao.save2(image);
		} catch (Exception e) {
			e.printStackTrace();
		}
		imageDao.save2(image);
		return "success";
	}

	public String readImage(){
		System.out.println(this.getClass()+".readImage()...");
		Image image=imageDao.get("21");
		FileOutputStream fos=null;
		try {
			fos=new FileOutputStream("d:\\upload\\target1.jpg");
			fos.write(image.getImg());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}


3、注意
<bean id="nativeJdbcExtractor"/>
用了c3p0连接池的情况下用: C3P0NativeJdbcExtractor
如果是dpcp连接池,则用:CommonsDbcpNativeJdbcExtractor
如果没有使用连接池,则用:SimpleNativeJdbcExtractor
其他的,用了jboss,weblogic连接池等等的情况,都有专门的Extractor对应。
分享到:
评论

相关推荐

    ssh(structs,spring,hibernate)框架中的上传下载

     本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文选用的数据库为Oracle 9i,当然你可以在不...

    sftpgo:功能齐全且高度可配置的SFTP服务器,具有可选的FTPS和WebDAV支持-S3,Google云存储,Azure Blob

    支持多个存储后端:本地文件系统,加密的本地文件系统,S3(兼容)对象存储,Google云存储,Azure Blob存储,SFTP。产品特点SFTPGo使用存储在“数据提供程序”中的虚拟帐户。 支持SQLite,MySQL,PostgreSQL,bbolt...

    Firebird.Maestro

    远程火鸟服务器通过SSH隧道 数据导出/导入到/从最流行的格式 Powerful BLOB Viewer/Editor 该应用程序还为您提供了一个功能强大的工具来管理用户和角色的火鸟,编辑和执行SQL脚本,建立视觉图数字数据,构建数据立方...

    SQLyog Ultimate v11.11

    22、支持对数据表的各种高级属性修改; 23、查看数据服务器的各种状态、参数等; 24、支持更改数据表类型为ISAM, MYISAM, MERGE, HEAP, InnoDB, BDB 25、刷新数据服务器、日志、权限、表格等; 26、诊断数据表:...

    数据库管理工具 SQLyog Ultimate 13.1.1.0 + x64 中文多语免费版.zip

    支持对数据表的各种高级属性的修改。 查看数据服务器的各种状态、参数等。 支持更改数据表类型为ISAM、MYISAM、MERGE、HEAP、InnoDB、BDB。 刷新数据服务器、日志、权限、表格等。 诊断数据表–检查、压缩、修补、...

    SQLyog Ultimate v11.21(X86/X64位)多语言注册版(Key)

    22、支持对数据表的各种高级属性修改; 23、查看数据服务器的各种状态、参数等; 24、支持更改数据表类型为ISAM, MYISAM, MERGE, HEAP, InnoDB, BDB 25、刷新数据服务器、日志、权限、表格等; 26、诊断数据表:...

    kio:https://github.comKDEkio.git

    IO 网络透明访问文件和... 有许多可用的插件,例如,支持通过SSH的访问。 该框架还可以用于将本机协议桥接到基于文件的接口。 这使得可以使用KDE文件对话框或任何其他启用KIO的基础结构在所有应用程序中访问数据。

    test-github-actions

    支持多个存储后端:本地文件系统,加密的本地文件系统,S3(兼容)对象存储,Google云存储,Azure Blob存储,SFTP。 特征 SFTPGo使用存储在“数据提供程序”中的虚拟帐户。 支持SQLite,MySQL,PostgreSQL,Bolt...

    LimeRPi2-kodi:适用于OpenELEC的Limelight。 (科迪)

    需要努力支持新的C版本的Moonlight。 使用。 也发布在 安装: 通过SSH连接到您的Pi。 这个版本的limelight / moonlight仍然需要Java,因此我们将其放在/ storage / java的pi上并对其进行测试。 (将来的版本将...

    file-share-private-endpoint:此示例演示如何在虚拟网络中创建Linux虚拟机,该虚拟网络使用Azure私有端点私有访问Azure文件共享

    此示例演示如何在虚拟网络中创建Linux虚拟机,该虚拟网络使用两个于访问和ADLS Gen 2 blob存储帐户。 Azure专用终结点是一个网络接口,可将您私密安全地连接到由Azure专用链接提供支持的服务。 专用端点使用您虚拟...

    MySQL中文参考手册

    + 4.12.5 用 SSH 从 Win32 连接一个远程MySQL + 4.12.6 MySQL-Win32与Unix MySQL 比较 o 4.13 OS/2 注意事项 o 4.14 TcX 二进制代码 o 4.15 安装后期(post-installation)的设置与测试 + 4.15.1 运行mysql_...

    MYSQL

    14.1.4 编译并安装用户定义函数 14.2 增加一个新的原生(native)函数 15 为MySQL增加新过程 15.1 analyse过程 15.2 编写一个过程 16 MySQL对 ODBC 支持 16.1 MyODBC 支持的操作系统 ...

    MySQL中文参考手册.chm

    Win32 上安装 MySQL 4.12.2 在 Win95 /Win98上启动 MySQL 4.12.3 在 NT 上启动 MySQL 4.12.4 在 Win32 上运行 MySQL 4.12.5 用 SSH 从 Win32 连接一个远程MySQL 4.12.6 MySQL-Win32与Unix ...

    Python Cookbook

    6.10 保留对被绑定方法的引用且支持垃圾回收 243 6.11 缓存环的实现 245 6.12 检查一个实例的状态变化 249 6.13 检查一个对象是否包含某种必要的属性 252 6.14 实现状态设计模式 255 6.15 实现单例模式 257 ...

Global site tag (gtag.js) - Google Analytics