1.1Hibernate的学习路线
- Hibernate的入门(环境搭建,API,Hibernate的CRUD )
- Hibernate的一级缓存,其他API
- Hibernate的一对多配置,Hibernate的多对多配置
- Hibernate的查询方式,抓取策略
1.2CRM案例
1.2.1CRM的概述
1.2.1.1什么是CRM
1.2.1.2CRM有那些模块
1.3Hibernate框架的概述
1.3.1框架的概述
1.3.1.1什么是框架
框架:指的是软件的半成品,已经完成了部分功能。sa
1.3.2EE的三层结构
以及每一层在三大框架中的对应板块
1.3.2.1EE的经典三层结构
1.3.3hibernate的概述
1.3.1什么是hibernate
Hibernate:Hibernate是一个持久层的ORM框架
1.3.2什么是ORM
对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping)
是将Java中的对象与关系型数据库建立一种隐射关系,从而操作对象就可以操作数据库中的表
1.3.3.2为什么要学习Hibernate?
1.4Hibernate入门
1.4.1hibernate的入门
1.4.1.1下载hibernate的开发环境
下载地址:
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
1.4.1.2解压hibernate 目录
documentation包:这个包是开发入门使用手册
lib包:是开发的开发包
- required: hibernate 开发的必须依赖包
- optional: hibernate可选的开发包
project:hibernate 提供的项目
1.4.1.3创建项目 引入Jar包
- hibernate开发的必须引入jar包
- hibernate引入日志记录包
1.4.1.4创建表
CREATE TABLE `cst_customer` (
`cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
1.4.1.5创建实体类
1.4.1.6创建映射
映射需要通过xml的配置文件来完成,这个配置文件可以任意命名。
尽量统一类名规范(类名.hbm.xml)
要在这里面定义到配置文件的约束:
位置如下:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
完整映射:
<?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.st.hibernate.demo.Customer" table="Customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id">
<!-- 使用的是本地策略 -->
<generator class="native"></generator>
</id>
<!-- 建立类中的普通的属性和字段的对应 -->
<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source" />
<property name="cust_industrye" column="cust_industrye" />
<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.cfg.xml)
找到约束
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
路径:hibernate-release-5.0.7.Final\project\etc\hibernate.properties
具体配置方式:
<?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的根标签 -->
<hibernate-configuration>
<session-factory>
<!-- 配置链接数据库可以在他提供的project里面找到 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernatest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</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>
<!-- 配置映射文件 -->
<mapping resource="com/st/hibernate/demo/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.4.1.8编写一个测试案例
接下来 使用hibernate 创建链接:
package com.st.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateDemo1 {
@Test
//保存客户端
public void demo1() {
//1.加载hibernate的核心配置文件
Configuration configuration=new Configuration().configure();
//2.创建一个SessionFactory对象:类似于JDBC中连接池
SessionFactory sessionFactory=configuration.buildSessionFactory();
//3.通过SessionFactory获取到Session对象:类似与JDBC中Connection
Session session=sessionFactory.openSession();
//4.手动开启事务
Transaction transaction=session.beginTransaction();
//5.编写代码
Customer customer=new Customer();
customer.setCust_name("测试hibernate保存");
session.save(customer);
//6.事务提交
transaction.commit();
//7.释放资源
session.close();
}
}
打印的sql语句与 结果
1.5Hibernate的常见配置
1.5.1XML的提示的配置
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
这个联网可以提示。
1.5.2XML的提示的配置问题
先复制上这段链接地址:http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
没有网络的情况下
改成url
找到dtd
还要配置一个。
注意:其中这里配置文件中的hbm.xml配置也是一样的!
保存即可!
1.5.2Hibernate的映射的配置
1.5.2.1映射的配置
-
【class标签的配置】
- 标签用来建立类与表映射的关系。
-
属性
- name:类的全路径
- table :表名(类名与表名一致,table可以省略)
- catalog:数据库名 -
【id标签的配置】
- 标签用来建立类中的属性与表中的主键的对应关系
- 属性:
- name:类中的属性名
- column:表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)
- length:长度
- type:类型
-
【property标签的配置】
- 标签用来建立类中的普通属性与表的字段的对应关系
- 属性:
- name:类中的属性名
- column:类中的字段名
- length:长度
- type:类型
- not-null:设置非空
- unique:设置唯一
1.5.3Hibernate的核心配置
1.5.3.1Hibernate的核心配置方式(了解)
- 一种方式:属性文件的方式
- hibernate.properties
- hibernate.connection.driver_class=com.mysql.jdbc.Driver
- …
- hibernate.show_sql=true
- 属性文件的方式不能引入映射文件(手动编写代码加载映射文件)
- hibernate.properties
- 二种方式:XML文件的方式
- hibernate.cfg.xml
1.5.3.2核心的配置
- 必须的配置
- 驱动类
- url路径
- 用户名
- 密码
- 可选的配置
- 显示SQL:hibernate.show_sql
- 格式化SQL:hibernate.format_sql
- 自动建表:hibernate.hbm2ddl.auto
- name:不使用hibernate的自动建表
- carte:如果数据库中已经有表,删除原有表,重新建立,如果没有表,重新建立
- create-drop:如果数据库中已经有表,删除原有表,执行操作。删除这个表,如果没有表,新建一个,使用完了删除该表。(测试)
- update:如果数据库中有表,会使用原结构表,如果没有表,创建新表(更新表结构)
-validate:如果没有,不会创建表只会使用数据库中的表,只会使用数据库中原有的表(效验你的映射和表结构是否一致)
<property name="hibernate.hbm2ddl.auto">update</property>
- 映射文件的引入
- 引入映射文件的位置
- - 通过mapping标签引入
- 引入映射文件的位置
1.6Hibernate的核心API
1.6.1Hibernate的API
- 1.6.1.1 configuration:hibernate的配置对象
- 作用:
- 加载核心配置文件
- hibernate.properties
- hibernate.cfg.xml
- Configguration cfg=new Configuration();
- hibernate.cfg.xml
- Configuration cfg=new Configuration().configure();
(如果映射文件注释)
(那么需要调用方法加载映射文件 方式如下:)
- 加载映射文件
//手动加载映射:
configuration.addResource("com/st/hibernate/demo/Customer.hbm.xml");
1.6.1.2SessionFactory:Session工厂
SessionFactory内部维护了hibernate的连接池和hibernate的二级缓存(不用已经用redis代替),是一个线程安全的对象,一个项目只需要创建一个就够了
- 配置连接池(知道)
记录日志文件 放在src下面:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout
error warn info debug trace这个分别是四个等级
也可以在核心文件中自己配置连接池等属性。
连接池的一些信息可以通过控制台输出看到
- 抽取工具类
package com.st.hibernate.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*
* Hibernate的工具类
* */
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();
}
}
编写测试类:
package com.st.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.st.hibernate.utils.HibernateUtils;
public class HibernateDemo2 {
@Test
//保存客户
public void demo() {
Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
Customer customer=new Customer();
customer.setCust_name("小刘");
session.save(customer);
tx.commit();
session.close();
}
}
1.6.1.3Session:类似于Connection对象 是一个连接对象
Session代表的是于hibernate与数据库的连接对象,不是线程安全的。与数据库交互桥梁
- Session中的API
-
- 保存方法 :
- Serializable save(Object obj);
- 查询方法 :
- T get(Class c,Serializable id);
- T load(Class c,Serializabie id);
- Get 方法和load方法的区别
区别在于:
get方法:1.我们可以通过debug中看到 在get 方法中 当执行语句时候就会加载sql语句,2.返回的是真实对象本身3.查询到一个不存在的对象的时候返回为空
load方法:1.也通过debug中可以看到 在执行过程中一直到commit才会加载,这个load方法 采用的是延时加载(lazy加载),执行到这行代码的时候不会发送sql语句,在真正使用对象时候才会发送sql语句2.返回的是代理对象。/lib/javassist-3.18.1-GA.jar这个包,利用javassist技术产生的代理。3.查询找不到的对象的时候是报一个ObjectNotFundException的异常
@Test
//查询
public void demo1() {
Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
//get 方式查询
/* Customer cutomer =new Customer();
cutomer=session.get(Customer.class, 1l);
System.out.println(cutomer);
*/
//load方式
Customer cutomer=session.load(Customer.class, 2l);
System.out.println(cutomer);
tx.commit();
session.close();
}
- 修改方法
- void update(Object obj);
- 删除的方法
- void delete(Object obj);
- void delete(Object obj);
- 保存或更新(不常用)
查询所有:(根据情况两种方式使用)
1.6.1.4Transaction:事务对象
Hibernate处理事务的对象
- commit();这个是提交
- eollback();这个为回滚
(这里在c3p0里面配置了可以不用管)