Hibernate是一个非常好用的ORM框架,本篇博客将会讲述Hibernate的简单入门的操作。
在开发前我们可以下载hibernate tools利于开发,下载地址如下:
https://sourceforge.net/projects/jboss/files/JBossTools/JBossTools4.1.x/hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605.zip/download
下载完是hibernatetools-Update-4.1.2.Final_2014-03-18_15-46-19-B706.zip这样的一个压缩包,打开eclipse的help==>Install New Software==>Add==>Archive选择刚才下载的压缩包之后默认下一步即可。配置完hibernate tools我们正式进行Hibernate的测试。
1.首先我们需要将Hibernate的包以及数据库驱动包加载进来
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- hibernate所需的依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<!-- 数据库驱动包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>oracle_driver</artifactId>
<version>oracle11g</version>
</dependency>
2.编写hibernate.cfg.xml配置文件,因为安装了hibernate tools我们只需new==>Hibernate Configuration File即可得到模板,我们再继续加入需要的配置信息
<?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.username">mytest</property>
<property name="connection.password">a</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- 是否在运行的时候显示sql语句 -->
<property name="show_sql">true</property>
<!-- 显示的sql语句是否进行排版 -->
<property name="format_sql">true</property>
<!-- 表结构生成策略 -->
<property name="hbm2ddl.auto">create</property>
<!-- getCurrentSession所需的参数(本地事务) -->
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
3.编写实体类
package com.yc.entity;
import java.sql.Blob;
import java.util.Date;
public class Student {
private int sid;
private String sname;
private String gender;
private Date birthday;
private String address;
private Blob picture;
public Student() {
}
public Student(int sid, String sname, String gender, Date birthday,String address) {
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int 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 getPicture() {
return picture;
}
public void setPicture(Blob picture) {
this.picture = picture;
}
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ ", picture=" + picture + "]";
}
}
4.编写映射文件,并在cfg文件加上映射路径,右击Student类new==>Hibernate XML Mapping file自动生成如下文件
<?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 2016-8-7 14:39:26 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.yc.entity.Student" table="STUDENT">
<id name="sid" type="int">
<column name="SID" />
<generator class="sequence" >
<!-- 使用序列 -->
<param name="sequence">seq_student_aId</param>
</generator>
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<!-- type="date" 只获取年月日的数据格式 -->
<property name="birthday" type="date">
<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"></column>
</property>
</class>
</hibernate-mapping>
在cfg文件中加入映射信息
<!-- 映射 -->
<mapping resource="Student.hbm.xml"/>
package com.yc.test;
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.yc.entity.Info;
import com.yc.entity.Student;
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistry=new
ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
// 创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//创建会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
}
@After
public void destory(){
//提交事务
transaction.commit();
//关闭会话
session.close();
//关闭会话工厂
sessionFactory.close();
}
@Test
public void testSave() {
Student stu=new Student(1000, "张三", "男", new Date(), "湖南衡阳");
session.save(stu);
}
6.运行结果
OK,到此我们的Hibernate开发准备已经完成,并且测试了其是否成功,下篇博客我们将介绍对单表的CRUD操作。