如果要將
將檔案存入資料庫,我們在JDBC中可以使用CLOB與BLOB來分別針對文字檔案與二進位檔案進行儲存,Spring中可以透過JdbcTemplate來處理CLOB與BLOB。
舉個例子來說,假設您的MySQL資料庫表格如下:
1 2 3 4 5
|
CREATE TABLE test ( id INT AUTO_INCREMENT PRIMARY, txt TEXT, image BLOB );
|
假設我們現在分別讀進一個文字檔案與二進位檔案,並想將之儲存至資料庫中,則我們可以使用JdbcTemplate,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
final File binaryFile = new File("c:\\workspace\\wish.jpg"); final File txtFile = new File("c:\\workspace\\test.txt"); final InputStream is = new FileInputStream(binaryFile); final Reader reader = new FileReader(txtFile); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); final LobHandler lobHandler = new DefaultLobHandler(); jdbcTemplate.execute("INSERT INTO test (txt, image) VALUES(?, ?)", new AbstractLobCreatingPreparedStatementCallback(lobHandler) { protected void setValues(PreparedStatement pstmt, LobCreator lobCreator) throws SQLException, DataAccessException { lobCreator.setClobAsCharacterStream(pstmt, 1, reader, (int) txtFile.length()); lobCreator.setBlobAsBinaryStream(pstmt, 2, is, (int) binaryFile.length()); } }); reader.close(); is.close();
|
JdbcTemplate中傳入了AbstractLobCreatingPreparedStatementCallback的實作,並傳入一 個 LobHandler,對於MySQL(MS SQL Server或Oracle 10g),這邊使用DefaultLobHandler即可,對於Oracle 9i特定的LOB處理,我們可以使用OracleLobHandler。
如果要從資料庫中將資料讀取出來,並另存為檔案,我們可以使用以下的程式:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
final Writer writer = new FileWriter("c:\\workspace\\test_bak.txt"); final OutputStream os = new FileOutputStream(new File("c:\\workspace\\wish_bak.jpg")); jdbcTemplate.query("SELECT txt,image FROM test WHERE id = ?", new Object[] {new Integer(1)}, new AbstractLobStreamingResultSetExtractor() { protected void streamData(ResultSet rs) throws SQLException, IOException, DataAccessException { FileCopyUtils.copy(lobHandler.getClobAsCharacterStream(rs, 1), writer); FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, 2), os); } }); writer.close(); os.close();
|
在使用Spring搭配Hibernate時,可以簡化對Lob型態的處理,只要在SessionFactory建構時指定LobHandler,例如:
* beans-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql: </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> </bean> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="lobHandler"> <ref bean="lobHandler"/> </property> <property name="mappingResources"> <list> <value>onlyfun/caterpillar/User.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> </beans>
|
在這邊指定LobHandler時,對於MySQL、DB2、MS SQL Server、Oracle 10g,使用DefaultLobHandler即可,而對於Oracle 9i,則可以使用OracleLobHandler。
接下來的操作與一般對HibernateTemplate的操作無異,例如您的資料庫表格為:
1 2 3 4 5
|
CREATE TABLE user ( id INT auto_increment PRIMARY Key, txt TEXT, image BLOB );
|
Spring的ClobStringType可以將CLOB映射至String,而BlobByteArrayType可以將BLOB映射至byte[],所以我們可以設計一個User類別如下:
* User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
package onlyfun.caterpillar; public class User { private Integer id; private String txt; private byte[] image; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public byte[] getImage() { return image; } public void setImage(byte[] image) { this.image = image; } public String getTxt() { return txt; } public void setTxt(String txt) { this.txt = txt; } }
|
Use.hbm.xml沒什麼特別的:
* User.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<?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"> <hibernate-mapping> <class name="onlyfun.caterpillar.User" table="user"> <id name="id" column="id"> <generator class="native"/> </id> <property name="txt" column="txt"/> <property name="image" column="image"/> </class> </hibernate-mapping>
|
以下是個簡單的儲存與讀取Lob的程式片段示範:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
ApplicationContext context = new FileSystemXmlApplicationContext("beans-config.xml"); InputStream is = new FileInputStream(new File("c:\\workspace\\wish.jpg")); byte[] b = new byte[is.available()]; is.read(b); is.close(); User user = new User(); user.setTxt("long...long...text"); user.setImage(b); HibernateTemplate hibernateTemplate = (HibernateTemplate) context.getBean("hibernateTemplate"); hibernateTemplate.save(user); user = (User) hibernateTemplate.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { User user = (User) session.load(User.class, new Integer(1)); Hibernate.initialize(user); return user; } }); System.out.println(user.getTxt()); b = user.getImage(); OutputStream os = new FileOutputStream(new File("c:\\workspace\\wish_bak.jpg")); os.write(b); os.close();
|
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1064190
分享到:
相关推荐
lob笔记.doclob笔记.doclob笔记.doc
Packt.Mastering.LOB.Development.for.Silverlight.5.A.Case.Study.in.Action
oracle dbms_lob
Oracle数据库中LOB的调优.pdf
NULL 博文链接:https://sosuny.iteye.com/blog/446112
1.6 Spring对Java版本的要求 1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring...
第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识。 第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。 第3章:...
前端开源库-lob.zip
ORACLE LOB大对象处理.doc ORACLE LOB大对象处理.doc
Oracle中对LOB对象的操作.pdf
Oracle的LOB(CLOB)大字段以及(SYS_LOB$$)清理.txt
第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。 第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到...
VB.Net学生成绩信息管理系统源码源码下载后Rar包解压密码均为lob.cn
KM_IKM Oracle Incremental UpdateLOB.xml KM_LKM SQL to SQL LOB(JYTHON).xml 里面包含两个KM 可以直接用
spring.jpa.propertires.hibernate.jdbc.lob.non_contextual_creation=trueblog spring.datasource.url=jdbc:mysql://localhost:3306/notas?useTimezone=true&serverTimezone=UTC spring.datasource.username=root ...
3在Spring中使用org.springframework.jdbc.support.lob.OracleLobHandler处理Oracle数据库的Blob类型字段。 通过这样的设置和配置,我们就可以象持久化表的一般字段类型一样处理Blob字段了。 以上是Spring+...
oracle dbms_lob 智能大对象(Blob、Clob)
CLOB变量需要dbms_lob.createtemporary,临时表空间中,建立临时LOB。 大数据量,返回值虽然是CLOB,依然会报错; CLOB变量需要dbms_lob.createtemporary,临时表空间中,建立临时LOB。 大数据量,返回值虽然是CLOB...
NULL 博文链接:https://liubing1883.iteye.com/blog/405679