一.hibernate框架是什么
是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。(hibernate是完全面系那个对象操作数据库)
二。框架的搭建
1.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://localhost:3306/hibernate?characterEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">briup</property>
<!--mysql方言 根据配置的方言来生成sql语句 这里是mysql的方言-->
<property name="hiberna.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--hibernate 在控制台显示sql语句-->
<property name="hibernate.show_sql">true</property>
<!--hibernate 在控制台显示的sql语句格式化-->
<property name="hibernate.format_sql">true</property>
<!--hbm2ddl.auto的取值
*none:不用hibernate自动生成表
*create:每次都会创建一个新表
*create-drop:每次都会创建一个新的表,执行程序结束后删除这个表
*update:如果数据库中有表,使用原来的表,如果没有表,创建一个新表,可以跟新表结构(建议使用)
*validate:只会使用原有的表,对映射关系进行校验
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--指定hibernate操作数据库时的隔离级别
#hibernate.connection.isolation 值为1|2|4|8
0001 1 读未提交
0010 2 读已提交
0100 4 可重复读
1000 8 串行化
-->
<property name="hibernate.connection.isolation">4</property>
<!--指定session与当前线程绑定-->
<property name="hibernate.current_session_context_class">thread</property>
<!--hibernate加载映射-->
<mapping resource="com/hibernate/mapper/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.orm元数据(对象与表的映射配置文件)(Customer.hbm.xml)
这里是实体类Cusotmer.java文件
package com.hibernate.bean;
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();
}
>这里是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标签是配置表与实体对象关系
package配置包名 作用:下面需要写完整类名的地方不需写包名
-->
<hibernate-mapping package="com.hibernate.bean">
<!--配置实体类 name:完整类名 table:对应数据库表名-->
<class name="Customer" table="cst_customer">
<!--配置实体属性
name:为实体类中的成员变量名
column:数据库中所对应的列名
type:hibernate:填写属性类型:会自动检测属性类型(一般不需配置)
not-null:配置给属性列是否为空,默认为false(可选配置)
length:配置数据库中列的长度,默认为:当前数据库类型中的最大长度(可选配置)
-->
<!--id标签为主键标签-->
<id name="cust_id" column="cust_id">
<!--主键生成策略
就是每条记录录入时,主键的生成规则(其中规则)
1.identity:主键自增:有数据库维护主键值,在录入时不需要指定主键
2.increment:主键自增:有hibernate来维护,每次插入前会查询表中“id最大值+1”最为新主键
3.sequence:oracle中的序列生成策略
4.hilo:高低位算法。
5.native:hilo+sequence+identity 自动三选一策略
6.uuid:用来产生随机字符串作为主键,主键类型必须是Stirng类型
7.assigned:自然主键生成策略。(自然主键:业务中某属性具备主键特性,如:身份证号)
-->
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<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>
测试hibernate(DemoTest.java)
package com.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.hibernate.bean.Customer;
public class Demo {
public static void main(String[] args) {
Configuration conf=new Configuration().configure();
SessionFactory sessionFactory=conf.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
Customer customer=new Customer();
customer.setCust_name("百度公司");
session.save(customer);
tx.commit();
session.close();
sessionFactory.close();
}
}
三.hibernateAPI详解
1.Configuration
(1)Configuration configuration=new Configuration(); Configuration功能,配置加载类,用于加载主配置文件(如上面的hibernate.cfg.xml),orm元数据(Cusotmer.hbm.xml) (2)configuration.confugration(); (这里的configuration()是上面Configuation的一个方法函数不要混淆) configuation()读取配置文件 空参加载方法们主要是读取默认在src 下的hibernate.cfg.xml(该配置文件名不能修改)。 也可使用非空参方法,参数为hibernate.cfg.xml文件路径)
2.SessionFactory:用于创建操作数据库核心对象session对象的工厂 注:1.sessionFactory 负责保存和使用所有配置信息,消耗资源较大,需在使用完关闭session ,且在一项目中最好只创建个 session对象 2. sessionFactory属于线程安全的对象设计 (1)根据配置信息创建SessionFactory对象 SessionFactory sessionFactory=configuration.buildSessionFactory();
3.Session对象(表达hibernate框架与数据库之间的连接会话)
(1) 获取Session对象session 两种方式:1.Session session=sessionFactory.openSession() ; (打开一个新的Session,每次调用都产生不同Session 对象) 2.Session session=sessionFactory.getCurrentSession();(获取一个与线程绑定的Session对象,在一个线程之 内连续调用两次调用同一Session对象)
4.Transaction(事务对象) 获取操作事务的对象 两种方式: (1)Transaction tx=session.getTransaction(); tx.begin();//该种方式需要手动打开事务 (2)Transaction tx=session.beginTransaction();//开启事务并获取操作事务的tx对象,该种方式不需手动打开事(建 议使用) 事务提交:tx.commit(); 回滚事务:tx.rollback(); 获取操作事务的对象和事务提交之间进行事务中对数据库的操作
四.hibernate对数据库的基本操作
1. 查询
(1)通过id查询(session.get(Customer.class,1l):hibernate自动生成查询的sql语句) session.get(Customer.class,1l);//Customer.class:实体类型;1l:id的值
2. 插入( session. save(customer):hibernate自动生成插入的sql语句) Customer customer=new Customer(); //创建实体类对象 customer.setCust_name("百度公司");//通过实体类中的set方法为对象属性赋值; session. save(customer); //将该对象中的所有属性插入到数据库中
3.更新修改 (session.get(Customer.class,1l):hibernate自动生成查询的sql语句; session.update(customer):hibernate自动生成更新的sql语句) Customer customr=session.get(Customer.class,1l);//获取要修改指定id的对象,Customer.class:实体类型;1l:id的值 customer.setCust_name("腾讯公司");//通过Customer类中的set方法,为指定id的对象,重新设置属性值 session.update(customer);//更新数据库中这个对象中的值
4.删除(session.get(Customer.class,1l):hibernate自动生成查询的sql语句; session.delete(customer):hibernate自动生成删除的sql语句) Customer customr=session.get(Customer.class,1l);//获取要修改指定id的对象,Customer.class:实体类型;1l:id的值 session.delete(customer);//删除数据库中查询出来的该对象