[size=large][color=red]Druid数据库驱动: Hibernate操作数据库文件不会报错,jdts容易报错。[/color][/size]
注解:
在hibernate Annotation中,实体BLOB、CLOB类型的注解与普通的实体属性有些不同,具体操作如下:BLOB类型,类型声明为byte[]:
CLOB类型,类型声明为String即可:
按照以上的设置实体类的注解就搞定了。
两种方式,保存到数据库:
注解:
在hibernate Annotation中,实体BLOB、CLOB类型的注解与普通的实体属性有些不同,具体操作如下:BLOB类型,类型声明为byte[]:
private byte[] content;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "CONTENT", columnDefinition = "BLOB",nullable=true)
public byte[] getContent() {
return this.content;
}
public void setContent(byte[] content) {
this.content = content;
}
CLOB类型,类型声明为String即可:
private String remark;
@Lob
@Basic(fetch = FetchType.EAGER)
@Column(name="REMARK", columnDefinition="CLOB", nullable=true)
public String getRemark() {
return this.remark;
}
public void setRemark(String recvdocRemark) {
this.remark = remark;
}
按照以上的设置实体类的注解就搞定了。
LobHelper.createBlob()
Hibernate.getLobCreator(getCurrentSession()).createBlob(stream, length)
package com.pandy.ssh4.domian;
// Generated 2013-3-13 16:41:18 by Hibernate Tools 4.0.0
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Table4 generated by hbm2java
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "TABLE4", schema = "dbo", catalog = "Test")
public class Table4 implements java.io.Serializable {
private int t4Id;
private String filename1;
private String content;
private String filename2;
private byte[] fileContent;
......
}
package com.pandy.ssh4.domian;
// Generated 2013-3-13 16:41:18 by Hibernate Tools 4.0.0
import java.sql.Blob;
import java.sql.Clob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Table4 generated by hbm2java
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "TABLE4", schema = "dbo", catalog = "Test")
public class Table41 implements java.io.Serializable {
private int t4Id;
private String filename1;
private Clob content;
private String filename2;
private Blob fileContent;
......
}
两种方式,保存到数据库:
package com.pandy.ssh4.dao.impl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.pandy.ssh4.dao.BasicDao;
import com.pandy.ssh4.domian.Table4;
import com.pandy.ssh4.domian.Table41;
@Repository
public class Table4Dao extends BasicDao {
public StringBuffer readerTxtFile() {
StringBuffer sb;
BufferedReader bf;
try {
sb = new StringBuffer();
bf = new BufferedReader(new FileReader(new File("d:\\a.txt")));
String str = null;
while ((str = bf.readLine()) != null) {
sb.append(str);
sb.append("\n");
}
bf.close();
return sb;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
}
}
public byte[] getFileBtyes() {
InputStream in;
try {
in = new FileInputStream(new File("d:\\a.jpg"));
byte[] bytes = new byte[in.available()];
in.read(bytes);
in.close();
return bytes;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
// 第一种方式
public void insertFile1() {
try {
InputStream in = new FileInputStream(new File("d:\\a.jpg"));
StringBuffer sb = readerTxtFile();
Session session = getCurrentSession();
Clob clob = Hibernate.getLobCreator(session).createClob(sb.toString());
Blob blob = Hibernate.getLobCreator(session).createBlob(in,in.available());
Table41 table41 = new Table41();
table41.setFilename1("a.txt");
table41.setContent(clob);
table41.setFilename2("a.jpg");
table41.setFileContent(blob);
session.save(table41);
} catch (Exception e) {
e.printStackTrace();
}
}
// 第二种方式
public void insertFile2() {
try {
Session session = getCurrentSession();
StringBuffer sb = readerTxtFile();
byte[] bytes = getFileBtyes();
Table4 table4 = new Table4();
table4.setFilename1("a.txt");
table4.setContent(sb.toString());
table4.setFilename2("a.jpg");
table4.setFileContent(bytes);
session.save(table4);
} catch (Exception e) {
e.printStackTrace();
}
}
// 读取操作1
@SuppressWarnings("rawtypes")
public Table4 getTable4() {
Session session = getCurrentSession();
String hql = "from Table4";
Query query = session.createQuery(hql);
List list = query.list();
if (list != null && !list.isEmpty()) {
return (Table4) list.get(0);
} else
return null;
}
// 读取操作2
@SuppressWarnings("rawtypes")
public Table41 getTable41() {
Session session = getCurrentSession();
String hql = "from Table41";
Query query = session.createQuery(hql);
List list = query.list();
if (list != null && !list.isEmpty()) {
return (Table41) list.get(0);
} else
return null;
}
}