Hibernate框架
ORM
1.ORM概念
O:Object 对象
R:Relation 关系
M:Mapping 映射
ORM:对象关系映射
2.ORM解决什么问题?
存储:能否把对象的数据直接保存到数据库?
获取:能否直接从数据库拿到一个对象?
想做上面两点,必须有映射!
3.Hibernate与ORM的关系?
Hibernate是ORM的实现
Hibernate Hello World案例
1.搭建一个Hibernate环境,开发步骤:
1.下载源码。版本:hibernate-distribution-3.6.0.Final 下载链接: (提取码:1a98)
2.引入jar文件:hibernate3.jar核心 + required 必须引入的(6个)+ jpa目录 + 数据库驱动包
3.写对象以及对象的映射。Employee.java(对象) Employee.hbm.xml(对象的映射)
4.新建src/hibernate.cfg.xml:数据库连接配置;加载所有配置文件
2.代码案例
1.Employee.java
import java.util.Date;
/**
* 一.对象
* Created by qianxingzhe on 16/2/13.
*/
public class Employee {
private int empId;
private String empName;
private Date workDate;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Date getWorkDate() {
return workDate;
}
public void setWorkDate(Date workDate) {
this.workDate = workDate;
}
}
2.Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.yly.a_hello">
<class name="Employee" table="employee">
<!-- 主键,映射 -->
<id name="empId" column="id">
<generator class="increment"/>
</id>
<!-- 非主键映射 -->
<property name="empName" column="empName"/>
<property name="workDate" column="workDate"/>
</class>
</hibernate-mapping>
3.hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 防止出现插入MySQL数据库中文乱码,加上useUnicode=true&characterEncoding=utf8 -->
<property name="hibernate.connection.url">
<![CDATA[jdbc:mysql:///hib_demo?useUnicode=true&characterEncoding=utf8]]>
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射-->
<mapping resource="com/yly/a_hello/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.App.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import java.util.Date;
/**
* Created by qianxingzhe on 16/2/13.
*/
public class App {
private static SessionFactory sf;
static {
//1.创建配置管理类对象
Configuration config = new Configuration();
//加载配置文件(默认加载src/hibernate.cfg.xml)
config.configure();
//2.根据加载的配置管理类对象,创建SessionFactory对象
sf = config.buildSessionFactory();
}
@Test
public void testHello() throws Exception {
//对象
Employee emp = new Employee();
emp.setEmpName("aa");
emp.setWorkDate(new Date());
//3.根据Session的工厂,创建session对象
Session session = sf.openSession();
//开启事物
Transaction ts = session.beginTransaction();
//保存-数据库
session.save(emp);
session.update(emp);
//提交事务
ts.commit();
//关闭
session.close();
sf.close();
}
}