Hibernate学习导图:
映射文件 Customer.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.itlwj.hibernate.demo1.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id">
<generator class="native"/><!-- 组键生成策略 native为本地策略 -->
</id>
<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
核心配置文件 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.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate02?serverTimezone=GMT</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin1993</property>
<!-- 配置Hibernate的方言,在project\etc下找 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 可选配置:打印SQL、格式化SQL -->
<!-- 打印SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 属性不只是有update -->
<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>
<!-- 设置事务隔离级别 -->
<property name="hibernate.connection.isolation">4</property>
<!-- 配置session绑定本地线程 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 加载映射文件 -->
<mapping resource="com/itlwj/hibernate/demo1/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
需要的jar包和两个文件放的位置如图:
Hibernate的工具类
package com.itlwj.hibernate.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
public static final Configuration cfg;
public static final SessionFactory sf;
static {
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
}
public static Session openSession() {
return sf.openSession();
}
public static Session getCurrentSession() {
return sf.getCurrentSession();
}
}
Hibernate的增删改查(CRUD)
C:
D:
U:
R:
注意:get方法和load方法的区别?(面试常问,通过设置断点然后Debug去找答案)
get方法
1.采用的是立即加载,执行到这行代码的时候,就会马上发生SQL语句去查询;
2.查询后返回的是真实对象本身;
3.查询一个找不到的对象的时候,返回null;
load方法
1.采用的是延迟加载(lazy懒加载),执行到这行代码的时候,不会发送SQL语句,当真正使用这个对象的时候才会发送SQL语句;
2.查询后返回的是代理对象。利用javassist包技术产生的代理;
3.查询一个找不到的对象的时候,返回ObjectNotFoundException异常;