mysql hibernate blob_Hibernate学习四----------Blob

该篇博客介绍了如何在Hibernate中处理MySQL数据库的Blob类型字段,包括如何保存带有图片数据的学生信息,以及如何读取和保存到本地文件。通过示例代码展示了保存和读取Blob对象的过程。
摘要由CSDN通过智能技术生成

© 版权声明:本文为博主原创文章,转载请注明出处

实例

1.项目结构

156cf0ddaa2876bc8446858e05ce4f46.png

2.pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

org.hibernate

Hibernate-Image

war

0.0.1-SNAPSHOT

Hibernate-Image Maven Webapp

http://maven.apache.org

UTF-8

5.1.6.Final

junit

junit

4.12

test

org.hibernate

hibernate-core

${hibernate.version}

mysql

mysql-connector-java

5.1.42

Hibernate-Image

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

/p>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >

5.hibernate.cfg.xml

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

root

***

com.mysql.jdbc.Driver

jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8

update

true

true

org.hibernate.dialect.MySQL5InnoDBDialect

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()方法

ac14a99ae54eeb9aa4adb098e71e5cfd.png

7.2 执行readImage()方法

e6ce2ee195527e6b55832b7ce55219ab.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值