Hibernate入门

Hibernate入门教程

1、Hibernate在SSH中的地位

wKiom1d9qd3DATK1AABUe2AVxAI770.png

 

SSH
序号技术作用
1Struts基于mvc模式的应用层框架技术!
2Spring创建对象、处理对象的依赖关系以及框架整合!
3Hibernate基于持久层的框架(数据访问层使用)!

 

 

2、DAO层的代码是如何编写的?

(1)操作XML数据

(2)使用Jdbc技术

a)原始的jdbc操作, Connection/Statement/ResultSet

b)自定义一个持久层框架, 封装了dao的通用方法

c)DbUtils组件, 轻量级的dao的组件;

d)Hibernate技术【hibernate最终执行的也是jdbc代码!】

 

 

3、ORM和Hibernate

 

 

3.1、ORM的概念

O,  Object  对象

R, Realtion 关系  (关系型数据库: MySQL, Oracle…)

M,Mapping  映射

ORM, 对象关系映射!

 

ORM, 解决什么问题?

存储:  把对象的数据直接保存到数据库

获取:  直接从数据库拿到一个对象

想做到上面2点,必须要有映射

 

 

3.2、Hibernate和ORM的关系

Hibernate是ORM的实现!

 

 

4、组件学习的三方面

1、源码,引入jar文件

2、配置(.xml or .properties)

3、API

 

 

5、Hibernate入门

Hibernate开发步骤

(1)下载源码

版本:hibernate-distribution-3.6.0.Final,下载地址如下:

https://sourceforge.net/projects/hibernate/files/hibernate3/3.6.0.Final/

wKiom1d9qq_CSSSkAAEBci5PBo8390.png

选择141.0MB的文件,它的文件格式是.zip格式的,而下面的文件是.gz格式的。

解压之后,它的源码位于hibernate-distribution-3.6.0.Final\project\core\src目录下

 

(2)引入jar文件

a)hibernate3.jar (核心文件)

    位于hibernate-distribution-3.6.0.Final目录下

wKioL1d9q3WAlVnxAABCfClRNrQ225.png

b)required (必须引入的jar,共6个)

    位于hibernate-distribution-3.6.0.Final\lib\required目录下

wKioL1d9q8LQyxGlAAAbK9yINt8148.png

c)jpa 目录

    位于hibernate-distribution-3.6.0.Final\lib\jpa目录

wKioL1d9rAfii4PdAAAI4Ao_OJE228.png

d)数据库驱动包(我用的是mysql的驱动包)

wKioL1d9rHDzBBZVAAAJlGMvEIk578.png

 

(3)写对象以及对象的映射

a)Employee.java            对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import  java.util.Date;
 
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;
     }
}

b)Employee.hbm.xml        对象的映射 (映射文件)

(.hbm可能是hibernate mapping的缩写)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<? 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">
 
<!-- 
 
   This mapping demonstrates content-based discrimination for the
   table-per-hierarchy mapping strategy, using a formula
   discriminator.
 
-->
 
< hibernate-mapping  package = "com.rk.hibernate.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 >

 

(4)主配置文件 src/hibernate.cfg.xml

a)数据库连接配置

b)加载所用的映射(*.hbm.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!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 >
         < property  name = "hibernate.connection.url" >jdbc:mysql:///test</ property >     <!-- 等同于jdbc:mysql://localhost:3306/test -->
         < property  name = "hibernate.connection.username" >root</ property >
         < property  name = "hibernate.connection.password" >root</ 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/rk/hibernate/a_hello/Employee.hbm.xml" />
         
     </ session-factory >
</ hibernate-configuration >

(5)测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package  com.rk.hibernate.a_hello;
 
import  java.util.Date;
 
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;
import  org.hibernate.cfg.Configuration;
import  org.hibernate.classic.Session;
 
public  class  App
{
     public  static  void  main(String[] args)
     {
         // 对象
         Employee emp =  new  Employee();
         emp.setEmpName( "张三" );
         emp.setWorkDate( new  Date());
         
         /*
          * 主体思路:Configuration-->SessionFactory-->Session
          * 细节:Session-->Transaction,必须由session创建transaction,否则无法保存。
          */
         // 获取加载配置文件的管理类对象
         Configuration config =  new  Configuration();
         config.configure();
         // 创建session的工厂对象
         SessionFactory sessionFactory = config.buildSessionFactory();
         // 创建session (代表一个会话,与数据库连接的会话)
         Session session = sessionFactory.openSession();
         // 开启事务
         Transaction transaction = session.beginTransaction();
         //保存数据
         session.save(emp);
         // 提交事务
         transaction.commit();
         // 关闭
         session.close();
         sessionFactory.close();
         System.out.println( "Over" );
     }
}

 

本文转自:http://blog.itpub.net/29917475/viewspace-2121485/

转载于:https://www.cnblogs.com/plan123/p/5650709.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值