温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

(01)Hibernate入门

发布时间:2020-05-26 07:53:24 来源:网络 阅读:354 作者:lsieun 栏目:数据库


1、Hibernate在SSH中的地位

(01)Hibernate入门


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/

(01)Hibernate入门

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

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


(2)引入jar文件

a)hibernate3.jar (核心文件)

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

(01)Hibernate入门

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

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

(01)Hibernate入门

c)jpa 目录

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

(01)Hibernate入门

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

(01)Hibernate入门


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

a)Employee.java            对象

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的缩写)

<?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)

<!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)测试

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");
	}
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI