hibernate是一个开源的轻量级对象关系映射框架,对JDBC进行了轻量级的对象封装,将POJO与数据库表建立映射关系,是一个全自动的ORM框架,hibernate可以自动生成SQL语句,使java程序员可以使用对象思维来操控数据库。
ORM:Object Relational Mapping (对象关系映射):指的是将java中的对象与关系数据库中的表建立映射关系,从而操作对象就可以操作数据库中的表;
一、hibernate开发环境配置
1.下载hibernate压缩包 http://hibernate.org/orm/
解压后目录文件
- documentation:hibernate开发文档
- lib:hibernate开发包(required:开发必须依赖的包;optional:开发可选的包)
- peoject:hibernate提供的项目
二、创建一个测试项目
1.引入jar包:在项目下创建一个lib目录,引入如下jar包,并构建路径
- 数据库驱动包
- hibernate开发必须依赖的jar包
- hibernate引入日志记录包
三个日志记录包:log4j-102.16.jar、slf4j-api-1.6.1.jar、slf4j-log4j12.-1.7.2.jar
2.创建表:创建一个测试表
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;
3.创建表对应的实体类:Customer
注意:在创建实体类的时候,尽量使用包装类。如long类型使用Long。包装类的默认值为null,而基本数据类型long的
默认值为0,保存进数据库表中,0容易引起歧义;
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;
所有的属性生成get和set方法;
3.创建映射文件(映射文件习惯命名为 Xxx.hbm.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!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">
<!--主键的生成策略-->
<generator class="native"></generator>
</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>
</class>
</hibernate-mapping>
hibernate配置文件的DTD信息可以在引入jar包的hibernate-core-5.0.7.Final.jar→org.hibernate→hibernate-mapping-3.0.dtd找到
4.创建hibernate的核心配置文件(核心配置文件习惯命名为 hibernate.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!--hibernate核心配置文件的dtd信息-->
<!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>
<!-- //localhost:3306/ -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">rootroot</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 引入映射文件 -->
<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
hibernate核心配置文件的DTD可以在hibernate-core-5.0.7.Final.jar→org.hibernate→hibernate-configuration-3.0.dtd找到
5.编写工具类:加载xml文件和建立sessionFactory(类似连接池)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate的工具类
* @author luowen
*/
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();
}
}
6.最后一步测试类的编写
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.hibernate.utils.HibernateUtils;
/**
* Hibernate的工具类的测试
* @author luowen
*
*/
public class HibernateDemo2 {
@Test
public void demo1(){
Session session = HibernateUtils.openSession();
//4.手动开启事务
Transaction transaction = session.beginTransaction();
//5.编写代码
Customer customer = new Customer();
customer.setCust_name("王东东");
session.save(customer);
//6.事务提交
transaction.commit();
//7.资源释放
session.close();
}
}