hibernate与mysql进行大文件的存储实例教程
程序员文章站
2022-05-30 14:33:46
1、hibernate4下创建sessionfactory
package com.hibernate;
import org.hibernate.session;
import org...
1、hibernate4下创建sessionfactory
package com.hibernate; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; import org.hibernate.service.serviceregistry; import org.hibernate.service.serviceregistrybuilder; public class hibernatebuilder { private final static class lock = hibernatebuilder.class; private static sessionfactory sessionfactory; public static sessionfactory getsessionfactoryinstance() { synchronized (lock) { if(sessionfactory != null) { return sessionfactory; } try { configuration configuration = new configuration().configure(); serviceregistry serviceregistry = new serviceregistrybuilder() .applysettings(configuration.getproperties()) .buildserviceregistry(); sessionfactory = configuration.buildsessionfactory(serviceregistry); } catch (exception e) { e.printstacktrace(); } } return sessionfactory; } public static session getsession() { if (sessionfactory == null) { getsessionfactoryinstance(); } return sessionfactory.opensession(); } }
2、hibernate.cfg.xml文件配置(如上方法会默认寻找根路径下的hibernate.cfg.xml文件)
数据库部分配置 必须-->com.mysql.jdbc.driver jdbc:mysql://ip:3306/demo root org.hibernate.connection.c3p0connectionprovider --> true true update org.hibernate.dialect.mysqldialect thread
3、创建实体类与xml文件
package com.hibernate.entity; import java.io.serializable; import java.sql.blob; public class people implements serializable { /** * */ private static final long serialversionuid = -2516745270873057482l; private integer id; public integer getid() { return id; } public void setid(integer id) { this.id = id; } private string name; private integer age; private string personintro; private blob personfile; public string getname() { return name; } public void setname(string name) { this.name = name; } public integer getage() { return age; } public void setage(integer age) { this.age = age; } public string getpersonintro() { return personintro; } public void setpersonintro(string personintro) { this.personintro = personintro; } public blob getpersonfile() { return personfile; } public void setpersonfile(blob personfile) { this.personfile = personfile; } }
4、向数据库存储大文件
session session = hibernatebuilder.getsession(); session.begintransaction(); people people = new people(); people.setage(21); people.setname("孙策"); try { fileinputstream input = new fileinputstream("1.wav"); blob blob = hibernate.getlobcreator(session).createblob(input,input.available()); people.setpersonfile(blob); session.save(people); } catch (ioexception e) { e.printstacktrace(); } finally { session.gettransaction().commit(); session.close(); }
5、从数据库读取大文件
session session = hibernatebuilder.getsession(); session.begintransaction(); query query = session.createquery("from people"); listresult = query.list(); if(result.size() > 0) { blob pf = result.get(0).getpersonfile(); try { outputstream output = new fileoutputstream("2.wav"); //将blob格式转换为输入流 inputstream input = pf.getbinarystream(); byte[] bytes = new byte[1024]; while(input.read(bytes) > 0) { output.write(bytes, 0, bytes.length); output.flush(); } output.close(); input.close(); } catch (exception e1) { e1.printstacktrace(); } } session.gettransaction().commit(); session.close();
测试代码 没有合理try catch