将文件存储到数据库(以二进制流的形式)

来源:互联网 发布:什么笔记本适合编程 编辑:程序博客网 时间:2024/05/21 08:54

Hibernate方法

HibernateUtils.java

package yang.fang.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * hibernate工具类 *  * @author zhaoqx *  */public class HibernateUtils {private static Configuration conf;private static SessionFactory sf;static {conf = new Configuration().configure();// 读取hibernate.cfg.xmlsf = conf.buildSessionFactory();}public static Session getSession() {return sf.openSession();}public static void main(String[] args) {getSession();}}


Person.java

package yang.fang.hibernate;public class Person {private Integer id;private String username;private String password;private String email;private String birthday;private String job;private String salary;private byte[] photo; // 头像图片public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public String getSalary() {return salary;}public void setSalary(String salary) {this.salary = salary;}public Person() {super();}public Person(String username, String birthday, String salary) {super();this.username = username;this.birthday = birthday;this.salary = salary;}@Overridepublic String toString() {return "Person [id=" + id + ", username=" + username + ", password="+ password + ", email=" + email + ", birthday=" + birthday+ ", job=" + job + ", salary=" + salary + "]";}public byte[] getPhoto() {return photo;}public void setPhoto(byte[] photo) {this.photo = photo;}}

Person.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="yang.fang.hibernate"><class name="Person" table="person"><id name="id"><generator class="native" /></id><property name="username" /><property name="password" /><property name="email" /><property name="birthday" /><property name="job" /><property name="salary"/><!-- 头像,二进制类型,最好指定长度 --><property name="photo" type="binary" length="1024000"></property></class></hibernate-mapping>

TestHibernate.java

package yang.fang.hibernate;import java.io.FileInputStream;import java.io.InputStream;import java.util.List;import org.hibernate.Criteria;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.criterion.Conjunction;import org.hibernate.criterion.DetachedCriteria;import org.hibernate.criterion.Disjunction;import org.hibernate.criterion.MatchMode;import org.hibernate.criterion.Projections;import org.hibernate.criterion.Restrictions;import org.junit.Test;public class TestHibernate {// 增加测试记录@Testpublic void test1() throws Exception {Session session = HibernateUtils.getSession();Transaction tr = session.beginTransaction();InputStream in = new FileInputStream("F:/test.jpg");byte[] photo = new byte[in.available()];in.read(photo);in.close();Person person = new Person();// person.setId(5);person.setUsername("欧阳锋");person.setPassword("******");person.setEmail("ouyang@qq.com");person.setBirthday("1989-05-07");person.setJob("上班");person.setSalary("60000");person.setPhoto(photo);session.save(person);tr.commit();session.close();}}

取出图片

public void testGet() throws Exception    <span style="font-family: Arial, Helvetica, sans-serif;">Session session = HibernateUtils.getSession();</span><pre name="code" class="html">    Transaction tr = session.beginTransaction();
Person p = (Person) session.get(Person.class, 1); // 获取 System.out.println(person.getPhoto()); OutputStream out = new FileOutputStream("D:/apple.png"); out.write(person.getPhoto()); out.close(); tx.commit(); session.close();}




0 0
原创粉丝点击