Hibernate学习四----------Blob
© 版权声明:本文为博主原创文章,转载请注明出处
实例
1.项目结构

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.hibernate</groupId>
<artifactId>Hibernate-Image</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Hibernate-Image Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.1.6.Final</hibernate.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
<build>
<finalName>Hibernate-Image</finalName>
</build>
</project>
3.Student.java
package org.hibernate.model; import java.sql.Blob;
import java.util.Date; public class Student { private long sid;// 学号
private String sname;// 姓名
private String gender;// 性别
private Date birthday;// 生日
private String address;// 性别
private Blob image;// 头像 public Student() {
} public Student(String sname, String gender, Date birthday, String address, Blob image) {
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
this.image = image;
} public long getSid() {
return sid;
} public void setSid(long sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public Blob getImage() {
return image;
} public void setImage(Blob image) {
this.image = image;
} }
4.Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping> <class name="org.hibernate.model.Student" table="STUDENT">
<id name="sid" type="java.lang.Long">
<column name="SID"/>
<generator class="native"/>
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME"/>
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER"/>
</property>
<property name="birthday" type="date">
<column name="BIRTHDAY"/>
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS"/>
</property>
<property name="image" type="java.sql.Blob">
<column name="IMAGE"/>
</property>
</class> </hibernate-mapping>
5.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration> <!-- SessionFactory配置 -->
<session-factory>
<!-- 数据库连接配置 -->
<property name="connection.username">root</property>
<property name="connection.password">***</property>
<property name="connection.drvier_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8
</property> <!-- 基本配置 -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 引用映射文件 -->
<mapping resource="hbm/Student.hbm.xml"/>
</session-factory> </hibernate-configuration>
6.ImageTest.java
package org.hibernate.test; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.Date; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.model.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class ImageTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void before() { Configuration config = new Configuration().configure();// 创建配置对象
sessionFactory = config.buildSessionFactory();// 创建SessionFactory对象
session = sessionFactory.openSession();// 创建session对象
transaction = session.beginTransaction(); // 开启事务 } @After
public void after() { transaction.commit();// 提交事务
session.close();// 关闭session
sessionFactory.close();// 关闭SessionFactory } @Test
public void saveStudentWithImage() throws Exception { // 获取图片文件
File file = new File("C:/Users/chen/Pictures/demo.jpg");
// 获取图片文件的输入流
InputStream is = new FileInputStream(file);
// 创建一个Blob对象
Blob image = Hibernate.getLobCreator(session).createBlob(is, is.available());
// 创建Student对象
Student s = new Student("张三", "男", new Date(), "北京市", image);
// 保存对象
session.save(s); } @Test
public void readImage() throws Exception { Student s = session.get(Student.class, 1L);
// 获取图片的Blob对象
Blob image = s.getImage();
// 获取照片的输入流
InputStream is = image.getBinaryStream();
// 创建输出文件
File file = new File("C:/Users/chen/Pictures/test.jpg");
// 获取输出流
OutputStream os = new FileOutputStream(file);
// 创建缓存区
byte[] buff = new byte[is.available()];
// 将输入流读入到缓存中
is.read(buff);
// 将缓存区数据写到输出流中
os.write(buff);
// 关闭输入流
is.close();
// 关闭输出流
os.close(); } }
7.效果预览
7.1 执行saveStudentWithImage()方法

7.2 执行readImage()方法

参考:http://www.imooc.com/video/7740
Hibernate学习四----------Blob的更多相关文章
- hibernate学习四(关系映射一对一与组件映射)
一.关系映射简介 在数据库中,表与表的关系,仅有外键.但使用hibernate后,为面向对象的编程,对象与对象的关系多样化:如 一对一,一对多,多对多,并具有单向和双向之分. 开始练习前,复制上一次项 ...
- --------------Hibernate学习(四) 多对一映射 和 一对多映射
现实中有很多场景需要用到多对一或者一对多,比如上面这两个类图所展现出来的,一般情况下,一个部门会有多名员工,一名员工只在一个部门任职. 多对一关联映射 在上面的场景中,对于Employee来说,它跟D ...
- hibernate学习四 hibernate关联关系映射
在Hibernate中对象之间的关联关系表现为数据库中表于表之间的关系(表之间通过外键关联). 1 单向的一对一 主键关联 外键关联 2 单向的一对多 3 单向的多对一 4 单向的多对多 5 双向的 ...
- Hibernate学习之——搭建log4j日志环境
昨天讲了Hibernate开发环境的搭建以及实现一个Hibernate的基础示例,但是你会发现运行输出只有sql语句,很多输出信息都看不见.这是因为用到的是slf4j-nop-1.6.1.jar的实现 ...
- SSH框架之hibernate《四》
hibernate第四天 一.JPA相关概念 1.1JPA概述 全称是:Java Persistence API.是sun公司推出的一套基于ORM的规范 ...
- Hibernate学习一:Hibernate注解CascadeType
http://zy19982004.iteye.com/blog/1721846 ———————————————————————————————————————————————————————— Hi ...
- Hibernate学习---缓存机制
前言:这些天学习效率比较慢,可能是手头的事情比较多,所以学习进度比较慢. 在之前的Hibernate学习中,我们无论是CURD,对单表查询还是检索优化,我们好像都离不开session,session我 ...
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
- >hibernate的四种状态
hibernate的四种状态 1.临时状态 与数据库中没有相对应的数据,也不在session的管理之中,一般是新new出来的对象 2.持久化状态 对象在session的管理中,最后会在事务提交后,在数 ...
随机推荐
- 2 - Django基础
一.Django流程 Django是使用python编写的web框架,遵守MTV设计思想. 实现原理: 1,浏览器发起请求. 2,Django根据URL Conf指向view(Views) 3,vie ...
- hash function 字符串哈希函数
#include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...
- What and How in an Application
Basically, two concerntrations: 1. What: business docs and prototype design 2. How: technical docs a ...
- .net web api ioc unity usage
1.use nuget to install unity.webapi 2.add configurations in application_start folder using Microsoft ...
- 关于 gstreamer 和 webrtc 的结合,有点小突破
今天让我找到了 gstreamer 的一个牛叉的杀手锏,脑海中马上想到了一个大致的框架和方案计划,用 gst-inspector 先进行对象自省属性探测,然后祭出 gst-launcher 大刀进行管 ...
- Cannot resolve symbol KeyEventCompat(android.support.v4.view.KeyEventCompat找不到)
Cannot resolve symbol KeyEventCompat(android.support.v4.view.KeyEventCompat找不到) 解决方案 KeyEventCompat类 ...
- 深度学习_2_CNN
Basic Conception: 感受野(Reception Field) 权值共享(shared weights) 池化,即降采样(sub-Sampling) 卷积核(kernel,filter) ...
- Codeforces 761E Dasha and Puzzle(构造)
题目链接 Dasha and Puzzle 对于无解的情况:若存在一个点入度大于4,那么直接判断无解. 从根结点出发(假设根结点的深度为0), 深度为0的节点到深度为1的节点的这些边长度为2^30, ...
- 【WEB基础】HTML & CSS 基础入门(8)表单
前面 前面我们已经熟悉了网页上一些常见的元素,如在网页上显示一段文字.一张图片.一个列表.一张表格等等.这些东西都是事先编辑好显示在页面上只提供给用户看的,实际上,我们可以把这样的页面称之为静态页面. ...
- Write a function that generates one of 3 numbers according to given probabilities
You are given a function rand(a, b) which generates equiprobable random numbers between [a, b] inclu ...