框架的概念
框架就是已经封装好的类,类中定义了可以完成较为复杂的业务逻辑的通用方法,我们可以通过调用这些方法来完成我们自己的业务逻辑。
·可以完成通用的业务逻辑
·封装好的业务逻辑(jar)
Hibernate的概念
Hibernate是一个开放源代码的对象关系映射框架 ,简称ORM(Object-RelationShip-Mapping)
Hibernate就是对JDBC的封装,完成对数据库的持久化操作,从而简化程序员在系统
开发过程中使用JDBC对数据库操作的繁琐步骤。
Hibernate的使用
① 将Hibernate框架引入到项目中:
a) 导入Hibernate的jar包(核心jar包,依赖jar包,数据库驱动jar包)
b) 创建核心配置文件hibernate.cfg.xml(用来告诉hibernate数据库的连接信息)
² 在src中创建一个名为"hibernate.cfg.xml"的xml文件
² 在hibernate.cfg.xml中引入hibernate框架定义好的DTD规范:
hibernate3.jar/org.hibernate/hibernate-configuration-3.0.dtd
² 在hibernate.cfg.xml中配置数据库连接
c) 创建一个类:HibernateSessionFactory
d) 创建一个类:ExportTables(用于根据对应的映射文件生成数据表)
② 在项目中使用Hibernate:
·帮助我们完成数据表的创建
·帮助我们完成对数据表中数据的CRUD
a) 创建POJO类(DTO类):和DTO规范一致
b) 创建POJO类的映射文件(创建,引入dtd规范,配置POJO类和数据表的映射关系)
c) 将映射文件添加到hibernate.cfg.xml
d) 根据映射文件生成数据表(如果表不存在的话):执行ExportTables
e) 创建DAO类来完成CRUD操作
③ CRUD
Hibernate的核心接口
1)Configuration
Configuration 接口的作用是加载核心配置文件(包括映射文件),获取一个SessionFactory对象。
2)SessionFactory
SessionFactory接口负责创建数据库连接,获取Session对象。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只需要一个SessionFactory就够,当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。
3)Session
Session接口负责执行被持久化对象的CRUD操作但需要注意的是Session对象是非线程安全的。同时,Hibernate的session不同于JSP应用中的HttpSession。这里当使用session这个术语时,其实指的是Hibernate中的session,而以后会将HttpSession对象称为用户session。
CRUD操作包括:
·添加:save
·删除:delete
·修改:update
·添加或修改:saveOrUpdate
·根据主键查询:get
·根据主键查询:load
4)Transaction
Transaction 接口是对实际事务实现的一个抽象,用于对事物进行管理。
5)Query
Query接口让你方便地对数据库及持久对象进行查询,它可以有两种表达方式:HQL语言或本地数据库的SQL语句。Query经常被用来查询所有,条件查询以及分页查询。
6)Criteria
Criteria接口与Query接口非常类似,用于模糊查询。值得注意的是Query接口也是轻量级的,它不能在Session之外使用。
Hibernate的配置
hibernate.cfg.xml
<?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="myeclipse.connection.profile">oracle</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:xe
</property>
<property name="connection.username">scott</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
oracle.jdbc.OracleDriver
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
</session-factory>
</hibernate-configuration>
xe表示实例名。在这里强调一点哈,装了数据库后千万别改计算机名,不然后悔的可是自己,本人亲身体验过的
HibernateSessionFactory.java
package com.softeem.utils;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
/**
* ①加载并解析配置文件(hibernate.cfg.xml)
* ②提供sessionFactory对象
* ③提供Session对象
*/
public class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private static final ThreadLocal<Session> local=new ThreadLocal<Session>();
/**
* (单例模式)构造器私有化,强制使用类名来调用方法
*/
private HibernateSessionFactory(){}
/**
* 在静态块中加载配置文件,并获取SessionFactory对象
*/
static{
cfg=new Configuration().configure();
sessionFactory=cfg.buildSessionFactory();
}
/**
* 获取SessionFactory对象
* @return
*/
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
private static void rebuildSessionFactory() {
cfg=new Configuration().configure();
sessionFactory=cfg.buildSessionFactory();
}
/**
* 返回Session对象
* @return
*/
public static Session getSession(){
Session session=local.get();
if(session==null||!session.isOpen()){
if(sessionFactory==null){
rebuildSessionFactory();
}
session=sessionFactory.openSession();
local.set(session);
}
return session;
}
/**
* 关闭Session对象
*/
public static void closeSession(){
Session session=local.get();
if(session!=null && session.isOpen()){
session.close();
}
local.set(null);
}
}
ExportTables.java
package com.softeem.crud.utils;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 根据映射文件生产数据表
*/
public class ExportTables {
public static void main(String[] args) {
//加载核心配置文件
Configuration cfg=new Configuration().configure();
//创建导出工具
SchemaExport se=new SchemaExport(cfg);
//是否需要创建sql脚本
se.create(true, true);
}
}
在核心配置文件hibernate.cfg.xml的Properties中Add一个键值对:Property=”show_sql”,Value=”true”
然后运行ExportTables.java就可以为我们自动创建表啦,创建SQL语句的脚本会在控制台输出。在运行ExportTables.java前,可不要忘了将POJO类的映射文件添加到核心配置文件的Mappings中