hibernate课程 初探单表映射3-3 对象类型
本节简介:
1 简介对象类型(重点是音视频blob类型)
2 demo(对图片的写入数据库与读取)
1 简介对象类型
映射类型 java类型 标准sql类型 mysql类型 oracle类型
binary byte[] varchar blob blob
text(大文本类型) java.lang.String clob text clob
clob(大文本类型) java.sql.Clob clob text clob
blob(二进制数据类型) java.sql.Blob blob blob blog 用于存储图片,音频,视频等
2 demo
hibernate.cft.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bendi</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource = "Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.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">
<!-- Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.ddwei.student.Student" table="STUDENT">
<id name="pid" type="int">
<column name="PID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" />
</property>
<property name="birthday" type="date">
<!-- <property name="birthday" type="time"> -->
<!-- <property name="birthday" type="timestamp"> -->
<column name="BIRTHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="picture" type="java.sql.Blob">
<column name="Picture" />
</property>
</class>
</hibernate-mapping>
Student.java
package com.ddwei.student;
import java.sql.Blob; import java.util.Date;
public class Student {
// java beans 的设计原则 /** * 1 公有的类 2 共有不带参数构造方法 3 私有属性 4 属性setter/getter方法 */
private int pid;// 学号 private String name;// 姓名 private String sex;// 性别 private Date birthday;// 出生日期 private String address;// 家庭地址 private Blob picture;//相片
public Student() {
}
public Student(int pid, String name, String sex, Date birthday, String address) { // super(); this.pid = pid; this.name = name; this.sex = sex; this.birthday = birthday; this.address = address; }
@Override public String toString() { return "Student [pid=" + pid + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; }
public int getPid() { return pid; }
public void setPid(int pid) { this.pid = pid; }
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 String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public Blob getPicture() { return picture; }
public void setPicture(Blob picture) { this.picture = picture; }
}
StudentTest.java
package hibernate_001;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; 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.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;
import com.ddwei.student.Student;
public class StudentTest { private SessionFactory sessionFactory; private Session session; private Transaction trasaction; @Test public void testSaveStudent(){ // Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象 Student student = new Student(); // student.setPid(100); student.setName("秦始皇"); student.setSex("男"); student.setBirthday(new Date()); student.setAddress("阿房宫"); session.save(student);//会话保存学生对象进入数据库 } /** * 图片(二进制)写入数据库 * @throws IOException */ @Test public void testWriteBlob() throws IOException{ Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象 //1 先获取照片文件: File.separator 是文件分隔符 File file = new File("g:"+File.separator+"tupian"+File.separator+"psb.jpg"); //2 获得文杰输入流(字节流) InputStream input = new FileInputStream(file); //3 创建一个blob对象 input.available()输入流的长度 Blob picture = Hibernate.getLobCreator(session).createBlob(input, input.available()); //4 设置照片属性 student.setPicture(picture); //5 保存学生 session.save(student); } /** * 图片(二进制)读取 * @throws SQLException * @throws IOException */ @Test public void testReadBlob() throws SQLException, IOException{ //1 加载学生对象 Student student = (Student) session.get(Student.class, 1); // System.out.println(student.getName()); //2 获得Blob对象 Blob picture = student.getPicture(); //3 获得输入流 InputStream input = picture.getBinaryStream(); //4 获得文件 File file = new File("d:"+File.separator+"a.jpg"); //5 获得输出流 OutputStream output = new FileOutputStream(file); //6 创建缓冲区 byte[] buff = new byte[input.available()]; //7 把输入流读取到缓冲区 input.read(buff); //8 输出流写入缓冲区的内容 output.write(buff); //9 关闭输入流和输出流 input.close(); output.close(); } @Before public void init(){ //1 创建配置对象 Configuration config = new Configuration().configure(); //2 创建服务对象 ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //3 创建会话工厂 sessionFactory = config.buildSessionFactory(serviceRe); //4 打开会话 session = sessionFactory.openSession(); //5 创建事务 trasaction = session.beginTransaction(); } @After public void destroy(){ trasaction.commit(); session.close(); sessionFactory.close(); }
}
hibernate课程 初探单表映射3-3 对象类型的更多相关文章
- hibernate课程 初探单表映射4-1 课程总结
ORM是一种面向对象编程的方法,用这种方法来避免写数据库底层语言sql语句,这样有利于java的跨平台,扩展.维护.而hirenate是ORM的一种框架 hirbernate开发基本步骤编写配置文档h ...
- hibernate课程 初探单表映射3-1 hibernate单表操作简介
本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例
- hibernate课程 初探单表映射1-3 hibernate简介
1 hibernate定义: Java领域一项开源的orm框架技术: hibernate对jdbc进行轻量级的封装. hibernate 作为持久层存在.就是通过对象关系映射把项目中的对象持久化到数据 ...
- hibernate课程 初探单表映射1-9 创建关系映射文件
创建关系映射文件:(把实体类映射成一个表) 1 右键src==>new==>other==>hibernate==>hbm.xml==>Student==>Fini ...
- hibernate课程 初探单表映射2-7 hbm配置文件常用设置
本节主要简介hbm配置文件以下内容: 1 mapping标签 2 class标签 3 id标签 1 hibbernate-mapping标签 schema 模式名称 catalog 目录名称 defa ...
- hibernate课程 初探单表映射2-4 transaction简介
1 hibernate是非自动提交.如果transaction不写的话,会只创建表结构而不插入语句. 如果不写transaction而想实现插入的功能的话,需要重写session的dowork方法 ...
- hibernate课程 初探单表映射2-2 hibernate常用配置
1 hibernate.cfg.xml常用配置: show_sql 控制台打印sql format_sql 控制台将sql排版 hbm2ddl.auto: create 删除表结构,重新建表并插值 u ...
- hibernate课程 初探单表映射2-1 hibernate进阶 本章简介
本章简介,主要讲5大块的内容 1 hibernate.cfg.xml的配置 2 session 的简介 3 transaction的简介 4 session的详解 5 对象关系映射常用配置
- hibernate课程 初探单表映射1-11 通过hibernate API访问编写第一个小例子
hibernate 业务流程 1 创建配置对象 Configuration config = new Configuration().configure(); 2 创建服务注册对象 Service ...
随机推荐
- 5种最流行的AI编程语言
人工智能如今正是蓬勃发展的时期,许多开发者都在跃跃欲试,如果你写想转做AI相关的开发,那么来了解更多与AI开发有关的内容吧,本文将介绍创建AI程序时可以使用的5种最佳语言. 人工智能如今正是蓬勃发展的 ...
- day18-事务与连接池 6.事务隔离级别与解决问题
开两个cmd窗口,相当于两个事务. read-uncommitted这种级别是解决不了任何问题的,它什么情况都能出现.刚才演示了脏读,再演示就出现了不可重复读. read-committed隔离级别能 ...
- Razor中的 内容标记块语法
在C#中,有两种方法来进行内容块标记 第一种方式 用@: 来标识 @if (true) { @: 测试内容标记块 @DateTime.Now.ToString() } <hr /> 第2种 ...
- 2010辽宁省赛F(字典树,动态规划)
#include<bits/stdc++.h>using namespace std;int n,x;char s[10010];char a[31010];int val[100010] ...
- 51nod1007(01背包)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1007 题意:中文题诶- 思路:尽量将一个数组分成两个相等的部 ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- 浮点数与快速log2
请先于浮点数的文章:http://blog.jobbole.com/86371/ 先贴一张关于float和double的图: float: double: 快速log2长这样: int flog2(f ...
- 洛谷P4707 重返现世(扩展MinMax容斥+dp)
传送门 我永远讨厌\(dp.jpg\) 前置姿势 扩展\(Min-Max\)容斥 题解 看纳尔博客去→_→ 咱现在还没搞懂为啥初值要设为\(-1\)-- //minamoto #include< ...
- 谈谈python修饰器
前言 对python的修饰器的理解一直停留在"使用修饰器把函数注册为事件的处理程序"的层次,也是一知半解:这样拖着不是办法,索性今天好好整理一下关于python修饰器的概念及用法. ...
- Luogu P5103 「JOI 2016 Final」断层 树状数组or线段树+脑子
太神仙了这题... 原来的地面上升,可以倒着操作(时光倒流),转化为地面沉降,最后的答案就是每个点的深度. 下面的1,2操作均定义为向下沉降(与原题意的变换相反): 首先这个题目只会操作前缀和后缀,并 ...