①引入Hibernate可以使工作人员角色细化,让程序员更关心业务流程。让数据库人员更关注数据库相关的各种操作。
②分层更加清晰,耦合性更小
③通用性强:可以更轻松地从一个数据库平台转移到别的平台
④对象化:把关系数据库编程Java对象,更加方便操作
⑤性能保证:Hibernate可能按不同的数据库,处理不同的操作是用最优化的SQL语句,不用我们去想,对于分等算法,在Hibernate中会显得更简单,可靠。
⑥增加了程序的鲁棒性
①介绍:Hibernate开发的三种方式:
- 由Domain Object->mapping->db。(官方推荐)
- 由DB开始,用工具生成mapping和Domain Object。(使用较多)
- 由映射文件开始。
②数据持久层/对象关系映射文件【该文件会说明表和对象的关系,以及对象的属性和表的字段的对应关系;命名规范:Domain对象名.hbm.xml】
①引入Hibernate开发包(http://pan.baidu.com/s/1qYwe6ny)
②创建数据表,编写Domain代码
③编写Employee.hbm.xml文件(对象关系映射文件)
<hibernate-mapping package="com.test.domain">
<class name="Employee" table="employee">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">employee_seqparam>
generator>
id>
<property name="name" type="java.lang.String">
<column name="name" not-null="false">column>
property>
<property name="email" type="java.lang.String">
<column name="email" not-null="false">column>
property>
<property name="hiredate" type="java.util.Date">
<column name="hiredate" not-null="false">column>
property>
class>
hibernate-mapping>
④手动配置hibernate.cfg.xml文件(Hibernate的核心配置文件),该文件用于配置连接的数据库的类型,driver,用户名,密码等(hibernate-distribution-3.3.1.GA-dist\hibernate-distribution-3.3.1.GA\project\etc)
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriverproperty>
<property name="connection.username">scottproperty>
<property name="connection.password">tigerproperty>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orclproperty>
<property name="dialect">org.hibernate.dialect.OracleDialectproperty>
<mapping resource="com/test/domain/Employee.hbm.xml"/>
session-factory>
hibernate-configuration>
⑤编写testMain.java文件,测试crud
public static void deleteEmployee() {
Session session = MySessionFactory.getSessionfactory().openSession();
Transaction ts = session.beginTransaction();
session.delete(session.load(Employee.class, 3));
ts.commit();
session.close();
}
public static void updateEmployee() {
Session session = MySessionFactory.getSessionfactory().openSession();
Transaction ts = session.beginTransaction();
// 修改用户
Employee employee = (Employee) session.load(Employee.class, 3);
employee.setName("你好啊");
ts.commit();
session.close();
}
public static void addEmployee() {
// 1 创建Configuration,该对象用于读取hibernate.cfg.xml文件,并完成初始化
// Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
Configuration configuration = new Configuration().configure(); // 默认是hibernate.cfg.xml
// 2 创建SessionFactory【这是一个会话工厂,是一个重量级的对象】
SessionFactory sessionfactory = configuration.buildSessionFactory();
// 3 创建session,相当于jdbc connection【】
Session session = sessionfactory.openSession();
// 4 对Hibernate来说,要求在进行CRUD操作时,使用事务提交
Transaction transcation = session.beginTransaction();
// 添加一个雇员
Employee employee = new Employee();
employee.setName("jiaozenglian");
employee.setEmail("[email protected]");
employee.setHiredate(new Date());
// 保存
session.save(employee); // ==> insert into ...
// 提交
transcation.commit();
session.close();
}
MySessionFactory.java文件(由于配置对象比较耗费资源,故将其设置为单例)
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public final class MySessionFactory {
private static SessionFactory sessionfactory = null;
static {
sessionfactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getSessionfactory() {
return sessionfactory;
}
}
①首先,重新配置hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="connection.username">rootproperty>
<property name="connection.password">111111property>
<property name="connection.url">jdbc:mysql://localhost:3306/testproperty>
<property name="dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="show_sql">trueproperty>
<property name="hbm2ddl.auto">updateproperty>
<mapping resource="com/test/domain/Employee.hbm.xml"/>
session-factory>
hibernate-configuration>
②配置Employee.hbm.xml文件
<hibernate-mapping package="com.test.domain">
<class name="Employee" table="employee">
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment">generator>
id>
<property name="name" type="java.lang.String">
<column name="name" length="64" not-null="false">column>
property>
<property name="email" type="java.lang.String">
<column name="email" not-null="false">column>
property>
<property name="hiredate" type="java.util.Date">
<column name="hiredate" not-null="false">column>
property>
class>
hibernate-mapping>
只有Configuration是一个类class,其他三个都是接口interface。
- Hibernate需要的其他库:
public static void updateEmployee() {
// 回滚事务
Session session = MySessionFactory.getSessionfactory().openSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
// 修改用户
Employee employee = (Employee) session.load(Employee.class, 3);
employee.setName("nihao");
ts.commit();
} catch (Exception e) {
if(ts!=null) {
ts.rollback();
}
e.printStackTrace();
throw new RuntimeException(e.getMessage());
} finally {
if(session!=null && session.isOpen()) {
session.close();
}
}
}