1.4.1.5、创建实体类
public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; }
1.4.1.6、创建映射
映射文件需要通过 XML 的配置来完成,这个配置文件可以任意命名。尽量统一命名规范(类名.hbm.xml)。
<?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.itheima.hibernate.demo1.Customer" table="cst_customer"> <!-- 建立类中的属性与表中的主键对应 --> <id name="cust_id" column="cust_id" > <!-- 主键的生成策略 native 本地的 --> <generator class="native"/> </id> <!-- 建立类中的普通的属性和表的字段的对应 --> <property name="cust_name" column="cust_name" length="32" /> <property name="cust_source" column="cust_source" length="32"/> <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>
1.4.1.7、创建 Hibernate 的核心配置文件
Hibernate 的核心配置文件的名称:hibernate.cfg.xml
(文件名称可以更改,更改比较麻烦)
<?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_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">abc</property> <!-- 配置Hibernate的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置================ --> <!-- 打印SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- 自动创建表 --> <property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
编写测试类
package com.hibernate.demo1;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; /** * Hibernate的入门案例 * @author jt * */ public class HibernateDemo1 { @Test // 保存客户的案例 public void demo1(){ // 1.加载 Hibernate 的核心配置文件 Configuration configuration = new Configuration().configure(); // 手动加载映射 // configuration.addResource("com/itheima/hibernate/demo1/Customer.hbm.xml"); // 2.创建一个SessionFactory对象:类似于JDBC中连接池 // 里面维护了连接处,并且做了其他设置 SessionFactory sessionFactory = configuration.buildSessionFactory(); // 3.通过SessionFactory获取到Session对象:类似于JDBC中Connection Session session = sessionFactory.openSession(); // 4.手动开启事务: // 兼容 hibernate3,hibernate5不需要手动开启事务 Transaction transaction = session.beginTransaction(); // 5.编写代码 Customer customer = new Customer(); customer.setCust_name("王西"); session.save(customer); // 6.事务提交 transaction.commit(); // 7.资源释放 session.close(); sessionFactory.close(); } }
二、Hibernate 的常见配置
1、XML 提示的配置
2、Hibernate 映射的配置
【class 标签的配置】
标签用来建立类与表的映射关系
属性:
name :类的全路径
table :表名(类名与表名一致,table可以省略)
catalog :数据库名
【id标签的配置】
标签用来建立类中的属性与表中的主键的对应关系
属性:
name :类中的属性名
column :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)
length :长度
type :类型
【property标签的配置】
标签用来建立类中的普通属性与表的字段的对应关系
属性:
name :类中的属性名
column :表中的字段名
length :长度(默认长度为255)
type :类型(可以不写,hibernate 会自动转化)
<!-- type 有三种形式 --> <!-- 1、Java中的类型 --> <property name="cust_name" column="cust_name" length="32" type="java.lang.Strinng" /> <!-- 2、Hibernate 中的类型 --> <property name="cust_name" column="cust_name" length="32" type="string" /> <!-- 3、数据库中的类型 --> <property name="cust_name" column="cust_name" length="32"> <column name="cust_naem" sql-type="varchar"></column> </property>
not-null :设置非空
unique :设置唯一
3、hibernate 核心的配置
3.1、核心配置方式
① 属性文件的方式(不常用)
hibernate.properties
hibernate.connection.driver_class=com.mysql.jdbc.Driver
…
hibernate.show_sql=true
属性文件的方式不能引入映射文件(手动编写代码加载映射文件)
② XML文件的方式
hibernate.cfg.xml
3.2、核心的配置
【必须的配置】
连接数据库的基本的参数
驱动类
url路径
用户名
密码
方言
[可选的配置]
显示SQL :hibernate.show_sql
格式化SQL :hibernate.format_sql
自动建表 :hibernate.hbm2ddl.auto
none :不使用 hibernate 的自动建表
create :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(做测试)
create-drop :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(做测试)
// 需要关闭工厂
sessionFactory.close();
update :如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
validate :如果没有表,不会创建表,并且会报错。只会使用数据库中原有的表。(校验映射和表结构)。
【映射文件的引入】
引入映射文件的位置
<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/>
三、Hibernate 的核心 API
1、Configuration:Hibernate 的配置对象
作用:
① 加载核心配置文件
hibernate.properties
Configuration cfg = new Configuration();
hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
② 加载映射文件
// 手动加载映射 configuration.addResource("com/itheima/hibernate/demo1/Customer.hbm.xml");
2、SessionFactory:Session 工厂
Session 内部维护了 Hibernate 的连接池和 Hibernate 的二级缓存(不讲,企业在一般用 redis 代替 )。是线程安全的对象。一个项目创建一个对象即可。
配置连接池
需要引入 C3P0 的 jar 包 hibernate-release-5.0.7.Final\lib\optional\c3p0 optional(可选的)
加入到项目的 lib 文件目录下,还需要 Build Path。
<?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_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">abc</property> <!-- 配置Hibernate的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置================ --> <!-- 打印SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- 自动创建表 --> <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/itheima/hibernate/demo1/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
这时候通过日志就能看到 C3P0 的情况。
4、Transaction:事务对象
Hibernate 中管理事务的对象。
commit();
rollback();
注意:在 hibernate5 中,如果没有配置C3P0 ,开启事务和提交事务可以不写;内置的连接池是可以自动帮你去提交的,配置好以后需要自己提交。并且 hibernate3 里面不自动提交,必须要写。为了兼容 hibernate3 的代码,这两句最好写上。
Transaction tx = session.beginTransaction();
...
tx.commit();