1、 什么是Hibernate
就是一个持久层ORM的框架
2、 什么是ORM :Object Relation Mapping
概述:ORM对象关系映射,将Java中的类和数据库中的表建立一种映射的关系,操作对象就可以操作数据库中的表。
Hibernate的入门案例
1、 引入jar包
2、 创建实体映射文件
(1)引入Hibernate的映射约束
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 建立类和表的映射 --> <class name="com.lwt.hibernate.domain.Customer" table="cst_customer"> <!-- 建立属性与表中字段的映射 --> <!-- 建立OID与表中主键映射 --> <id name="cust_id" column="cust_id"> <generator class="native"/> </id> <!-- 普通属性与表中字段建立映射 --> <property name="cust_name" column="cust_name"/> <property name="cust_source" column="cust_source"/> <property name="cust_industry" column="cust_industry"/> <property name="cust_level" column="cust_level"/> <property name="cust_phone" column="cust_phone"/> <property name="cust_mobile" column="cust_mobile"/> </class> </hibernate-mapping>
|
(2)编写Customer.hbm.xml
压缩包中:project/etc/
3、 Hibernate的核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!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:///hibernate_01_36</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 配置Hibernate的属性 -->
<!-- Hibernate的方言: -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置Hibernate的其他的属性 -->
<!-- 显示SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!--
hbm2ddl.auto :
* create :如果数据库中有表,将该表删除,重新创建一个新的表,如果没有表,创建新表。
* create-drop :如果数据库中有表,将表删除,重新创建,使用完毕会删除该表。如果没有表,创建新表,用完删除掉。
* update :如果有表,使用原有表,如果没有表,创建新表。而且更新表结构。
* validate :如果没有表报错,如果有表,使用原有表,对表进行校验。
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置C3P0连接池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
<!-- 加载映射文件 -->
<mapping resource="com/lwt/hibernate/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、 编写保存客户代码
@Test
/**
* 保存客户
*/
public void demo1(){
// 加载核心配置文件
Configuration configuration = new Configuration().configure();
// 创建一个SessionFactory:类似于连接池.
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建一个Session:相当于Connection.
Session session = sessionFactory.openSession();
// 开启事务:
Transaction transaction = session.beginTransaction();
// 操作
Customer customer = new Customer();
customer.setCust_name("郝宝强");
session.save(customer);
// 提交事务:
transaction.commit();
// 释放资源
session.close();
}