- -
什么是ORM?
答: 对象/关系映射(Object Relational Mapping,简称ORM)
简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中。
什么是Hibernate?
答:
Hibernate 是java领域的一款开源的ORM框架技术;
Hibernate对JDBC进行了非常轻量级的对象封装。
开发环境:
- 开发工具 eclipse
- Hibernate Tools for Eclipse plugins(该插件可以简化ORM框架Hibernate)
- Mysql 数据库创建student数据库
项目引入jar包
- hibernate jar包\lib\required 路径的jar包
- mysql-connector-java-5.1.41-bin.jar
- junit-4.10.jar
Hibernate Tools 插件安装步骤:
- 访问:http://hibernate.org/tools/
- 接下来根据提示全部下一步就完成安装hibernate Tools 插件了
- 验证插件是否安装成功如下图所示:
编写第一个Hibernate小例子
- 右键 New->java Project
- 输入项目名称->student
创建hibernate.cfg.xml 文件
右键src文件夹->New ->
代码如下:
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1111</property>
<property name="hibernate.connection.url">jdbc:mysql:///student?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
- 创建实体类,代码如下:
package com.hibernate.pojo;
import java.util.Date;
public class Student {
// 持久化类遵循javaBean 设计原则
// 1、共有的类
// 2、提供公有的不带参数的默认的构造方法
// 3、属性私有
// 4、属性setter/getter方法
private int sid; // 学号
private String sname;// 姓名
private String gender;// 性别
private Date birthday;// 生日
private String address;// 地址
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;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
- 创建Student.hbm.xml 文件
右键实体类所在包 New ->
next ->finish
代码如下:
<?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-10-21 21:32:10 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hibernate.pojo.Student" table="STUDENT">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</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="java.util.Date">
<column name="BIRTHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
8.创建Junit 测试用例
- 选中项目 右键 New -> Source Folder -> 新建StudentTest.java 测试类。
测试代码如下:
package hibernatetools;
import java.util.Date;
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.hibernate.pojo.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();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.openSession();
//开启事物
transaction = session.beginTransaction();
}
@After
public void destory() {
transaction.commit();//提交事物
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
@Test
public void studentTest(){
Student people = new Student(22, "花花", "女", new Date(), "中国");
session.save(people);
}
}