Hibernate框架
Hibernate框架入门:
一、框架的概述
1、什么是框架
框架:指的是软件的半成品,已经完成的部分功能。
二、EE的三层结构
三、什么是 Hibernate
Hibernate (开放源代码的对象关系映射框架)
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的JaveEE架构中取代CMP,完成数据持久化的重任。
1、Hibernate 持久层的ORM框架
什么是ORM Object Relational Mapping(对象关系映射)。指的是对象与关系型数据库中的表建立一种映射关系。从而来操作我们的对象就可以操作数据库当中的表。
四、Hibernate 特点
五、Hibernate 入门
1、下载Hibernate 官网
https://sourceforge.net/projects/hibernate/
下载好解压
2、Hibernate 目录文件当中各自作用
documentation: Hibernate 开发文档
lib:Hibernate 开发包
—》required : Hibernate 开发的必须依赖的包
—》optional: Hibernate 开发的可选的jar包
project:提供的测试的项目
3、创建一个项目引入jar包
1、引入数据库的驱动包
2、Hibernate 开发必须的jar包
3、Hibernate 引入日志记录包:
然后Builder path
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;
5、创建实体类Customer
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;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
public Customer() {
// TODO Auto-generated constructor stub
}
public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
String cust_phone, String cust_mobile) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_source = cust_source;
this.cust_industry = cust_industry;
this.cust_level = cust_level;
this.cust_phone = cust_phone;
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
+ ", cust_mobile=" + cust_mobile + "]";
}
}
6、创建映射
映射需要通过XML配置文件来完成,这个配置文件可以任意命名。尽量统一命名规范(类名.hbm.xml)
复制该头,引入jar当中的配置
7、创建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.itzheng.hibernate.demo01.Customer"
table="cst_customer">
<!-- 建立类当中的属性与表当中的主键对应的 -->
<id name="cust_id" column="cust_id">
<generator class="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>
8、创建hibernate的核心配置文件*
hibernate核心配置文件的名称:hibernate.cfg.xml
创建Customer.hbm.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">root</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/itzheng/hibernate/demo01/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
配置文件与hibernate提供资料的联系
9、编写测试代码
在这里可能会遇到jdk新版本和hibernate不兼容的问题
真正解决方案:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
参考解决办法https://blog.csdn.net/hadues/article/details/79188793
public class HibernateDemo01 {
@Test
// 保存客户的案例
public void demo01() {
// 1、加载Hibernate核心配置文件
Configuration configuration = new Configuration().configure();// 该方法加载了Customer.hbm.xml
// 2、创建SessionFactory对象:类似于JDBC中的链接池
SessionFactory sessionFactory = configuration.buildSessionFactory();// sessionFactory工厂
// 3、通过SessionFactory获取到Session对象:类似于我们JDBC中的Connection
Session session = sessionFactory.openSession();// Hibernate以及和mysql数据库建立起链接
// 4、手动开启事务:
Transaction transaction = session.beginTransaction();
// 5、编写代码
Customer customer = new Customer();
customer.setCust_name("张三");
session.save(customer);
// 6、事务提交
transaction.commit();
// 7、资源释放
session.close();
}
}
输出该结果表示插入数据库数据成功
转载:https://blog.csdn.net/qq_44757034/article/details/105441933