Hibernate 概念
来源:互联网 发布:小组抽签软件 编辑:程序博客网 时间:2024/06/05 00:26
一、Hibernate框架
ORM概念
O, Object 对象
R, Realtion关系 (关系型数据库: MySQL, Oracle…)
M,Mapping 映射
ORM, 对象关系映射!
ORM, 解决什么问题?
存储: 能否把对象的数据直接保存到数据库?
获取: 能否直接从数据库拿到一个对象?
想做到上面2点,必须要有映射!
总结:
Hibernate与ORM的关系?
Hibernate是ORM的实现!
组件学习:
1. 源码,引入jar文件
2. 配置
3. Api
Hibernate HelloWorld案例
搭建一个Hibernate环境,开发步骤:
1. 下载源码
版本:hibernate-distribution-3.6.0.Final
2. 引入jar文件
hibernate3.jar核心 + required 必须引入的(6个) + jpa 目录 +数据库驱动包
3. 写对象以及对象的映射
Employee.java 对象
Employee.hbm.xml 对象的映射(映射文件)
4. src/hibernate.cfg.xml 主配置文件
-à 数据库连接配置
-à 加载所用的映射(*.hbm.xml)
5. App.java 测试
Employee.java 对象
//一、 对象
public class Employee {
private int empId;
private String empName;
private Date workDate;
}
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="cn.itcast.a_hello">
<class name="Employee" table="employee">
<!-- 主键 ,映射-->
<id name="empId" column="id">
<generator class="native"/>
</id>
<!-- 非主键,映射 -->
<property name="empName" column="empName"></property>
<property name="workDate" column="workDate"></property>
</class>
</hibernate-mapping>
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>
<!-- 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 加载所有映射 -->
<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
App.java 测试类
public class App {
@Test
public void testHello()throws Exception {
// 对象
Employee emp = new Employee();
emp.setEmpName("班长");
emp.setWorkDate(new Date());
// 获取加载配置文件的管理类对象
Configuration config = new Configuration();
config.configure(); // 默认加载src/hibenrate.cfg.xml文件
// 创建session的工厂对象
SessionFactory sf = config.buildSessionFactory();
// 创建session (代表一个会话,与数据库连接的会话)
Session session = sf.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
//保存-数据库
session.save(emp);
// 提交事务
tx.commit();
// 关闭
session.close();
sf.close();
}
}
- Hibernate 概念
- 【Hibernate】1.Hibernate基础概念
- hibernate中的一些概念
- hibernate的一些概念
- Hibernate 概念解释
- hibernate的概念
- Hibernate的几个概念
- Hibernate基础概念
- Hibernate一些概念
- Hibernate中的主要概念
- 【Hibernate】ORM概念
- 【Hibernate】事务的概念
- Hibernate的缓存机制概念:
- Hibernate的缓存机制概念
- Hibernate的缓存机制概念
- Hibernate的概念及其配置
- Hibernate基本原理及概念详解
- Hibernate副本概念原理视图
- Spark中RpcEnv和SparkEnv的区别
- 1009. 说反话 (20) Python 2 编译
- Maven 手动添加 JAR 包到本地仓库
- JSON.parse()与JSON.stringify()的区别
- 为什么C++编译器不能支持对模板的分离式编译
- Hibernate 概念
- UNIX File I/O
- C++学习(55)
- 传统线程技术回顾
- 利用JS AJAX向datatables中的表格体填充数据
- 用tcpdump来抓包和分析IP/TCP的结构
- 实现商品换购
- linux系统通过内核调试器kdb获取root权限
- myeclipse svn安装教程