首页 > 代码库 > hibernate_05_单表操作_对象类型
hibernate_05_单表操作_对象类型
本篇使用hibernate输出一个对象(图片)
先写一个java类
1 package com.imooc.hibernate; 2 3 import java.sql.Blob; 4 import java.util.Date; 5 6 public class Students { 7 8 private int sid; 9 private String sname; 10 private String gender; 11 private Date birthday; 12 private String address; 13 private Blob picture; 14 15 public Blob getPicture() { 16 return picture; 17 } 18 19 public void setPicture(Blob picture) { 20 this.picture = picture; 21 } 22 23 public Students() {} 24 25 public Students(int sid, String sname, String gender, Date birthday, String address) { 26 super(); 27 this.sid = sid; 28 this.sname = sname; 29 this.gender = gender; 30 this.birthday = birthday; 31 this.address = address; 32 } 33 34 @Override 35 public String toString() { 36 return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday 37 + ", address=" + address + "]"; 38 } 39 40 public int getSid() { 41 return sid; 42 } 43 44 public void setSid(int sid) { 45 this.sid = sid; 46 } 47 48 public String getSname() { 49 return sname; 50 } 51 52 public void setSname(String sname) { 53 this.sname = sname; 54 } 55 56 public String getGender() { 57 return gender; 58 } 59 60 public void setGender(String gender) { 61 this.gender = gender; 62 } 63 64 public Date getBirthday() { 65 return birthday; 66 } 67 68 public void setBirthday(Date birthday) { 69 this.birthday = birthday; 70 } 71 72 public String getAddress() { 73 return address; 74 } 75 76 public void setAddress(String address) { 77 this.address = address; 78 } 79 }
创建Students.hbm.xml文件
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="com.imooc.hibernate.Students" table="STUDENTS"> 7 <id name="sid" type="int"> 8 <column name="SID" /> 9 <generator class="native" /> 10 </id> 11 <property name="sname" type="java.lang.String"> 12 <column name="SNAME" /> 13 </property> 14 <property name="gender" type="java.lang.String"> 15 <column name="GENDER" /> 16 </property> 17 <property name="birthday" type="java.util.Date"> 18 <column name="BIRTHDAY" /> 19 </property> 20 <property name="address" type="java.lang.String"> 21 <column name="ADDRESS" /> 22 </property> 23 <property name="picture" type="java.sql.Blob"> 24 <column name="PICTURE" /> 25 </property> 26 </class> 27 </hibernate-mapping>
创建hibernate.cfg.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.username">root</property> 8 <property name="connection.password">root</property> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property> 11 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 12 <property name="show_sql">true</property> 13 <property name="format_sql">true</property> 14 <property name="hbm2ddl.auto">update</property> 15 16 <mapping resource="com/imooc/hibernate/Students.hbm.xml"/> 17 </session-factory> 18 </hibernate-configuration>
创建测试类
1 package com.icoom.test; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.sql.Blob; 8 import java.util.Date; 9 10 import org.hibernate.Hibernate; 11 import org.hibernate.Session; 12 import org.hibernate.SessionFactory; 13 import org.hibernate.Transaction; 14 import org.hibernate.cfg.Configuration; 15 import org.hibernate.service.ServiceRegistry; 16 import org.hibernate.service.ServiceRegistryBuilder; 17 import org.junit.After; 18 import org.junit.Before; 19 import org.junit.Test; 20 21 import com.imooc.hibernate.Students; 22 23 public class StudentsTest { 24 25 private SessionFactory sessionFactory; 26 private Session session; 27 private Transaction transaction; 28 29 @Before 30 public void init() { 31 // 1.创建配置对象 32 Configuration config = new Configuration().configure(); 33 // 2.创建服务注册对象 34 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 35 // 3.创建会话工厂对象 36 sessionFactory = config.buildSessionFactory(serviceRegistry); 37 // 会话对象 38 session = sessionFactory.openSession(); 39 // 开启事务 40 transaction = session.beginTransaction(); 41 } 42 @After 43 public void destory() { 44 transaction.commit(); //提交事务 45 session.close(); //关闭session 46 sessionFactory.close();//关闭会话工厂 47 } 48 49 50 /** 51 * 使用hibernate输出对象 52 * @throws Exception 53 */ 54 @Test 55 public void testReadBlob() throws Exception { 56 Students s = (Students)session.get(Students.class, 1); 57 // 获得Blob对象 58 Blob image = s.getPicture(); 59 // 获得照片的输入流 60 InputStream input = image.getBinaryStream(); 61 // 创建输出流 62 File f = new File("d:" + File.separator + "out龙猫.jpg"); 63 // 获得输出流 64 OutputStream output = new FileOutputStream(f); 65 // 创建缓冲区 66 byte[] buff = new byte[input.available()]; 67 input.read(buff); 68 output.write(buff); 69 input.close(); 70 output.close(); 71 } 72 }
执行成功,在D盘根目录中就会出现out龙猫.jpg文件。
hibernate_05_单表操作_对象类型
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。