一.hibernate介绍:
1.框架是什么:
(1).框架是用来提高开发效率的
(2).封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现.
(3).所以框架可以理解成是一个半成品的项目.只要懂得如何驾驭这些功能即可.
Hibernate框架在web开发中的位置:
Hibernate优势:操作数据库的时候,可以以面向对象的方式来完成.不需要书写SQL语句,是一款orm(对象关系映射)框架.
二.Hibernate框架搭建
1.导包
hibernate下载地址:https://download.csdn.net/download/qq_38097815/10371820
解压后lib目录下required目录中的所有jar包,数据库驱动包
2.创建数据库,准备表实体
CREATE TABLE `cst_customer` ( `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)', `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id', `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id', `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源', `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业', `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别', `cust_linkman` varchar(64) 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.书写orm元数据
3.1导入约束
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">3.2实体(添加get/set方法)
public class Customer { private long cust_id; private String cust_name; private String cust_industry; private String cust_level; private String cust_source; private String cust_linkman; private String cust_phone; private String cust_mobile; public Customer() { super(); // TODO Auto-generated constructor stub }
3.3orm元数据配置文件
命名为:实体类名.hbm.xml例如本例为:
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"> <!-- 配置表与实体对象的关系 --> <!-- package属性 :填写包名 在元素内部 凡是需要书写完整类名的属性,可以直接写简答类名--> <hibernate-mapping package="cn.itheima.domain"> <!-- class元素,实体与表的对应关系 name:完整类名 table:数据库表名 --> <class name="cn.itheima.domain.Customer" table="cst_customer"> <!-- id元素 :配置主键映射的属性 name:填写主键对应属性名 column(可选):填写表中的主键名 默认值:列名默认使用属性名 type(可选):填写列属性类型,不填hibernate自动检测试题的属性类型. 每个类型有三种填法:java|hibernate|数据库类型 not-null(可选):配置该属性(列)是否为空,默认值:false length(可选):配置数据库中列的长度,默认值:使用数据库最大长度 --> <id name="cust_id" column="cust_id"> <!-- generator:主键生成策略 --> <generator class="native"></generator> </id> <!-- property 除id之外的普通属性映射 name:填写属性对应属性名 column:填写列 type(可选):填写列属性类型,不填hibernate自动检测试题的属性类型. 每个类型有三种填法:java|hibernate|数据库类型 not-null(可选):配置该属性(列)是否为空,默认值:false length(可选):配置数据库中列的长度,默认值:使用数据库最大长度 --> <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_linkman" column="cust_linkman"></property> <property name="cust_phone" column="cust_phone"></property> <property name="cust_mobile" column="cust_mobile"></property> </class> </hibernate-mapping>
4.书写主配置文件(约束为hibernate-configetnation)
主配置文件名称必须为: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:///hibernate_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <!-- 数据库方言 在不同的数据库中,sql语句略有不同,指定方言可以让hibernate框架生成sql语句时,针对数据库方言 DDL 定义语言 DCL 控制语言 DML 操纵语言 增删改查 mysql 选最短的那个方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- #hibernate.show_sql true hibernate.format_sql true --> <!-- 将hibernate生成的语句打印到控制台 --> <property name="hibernate.show_sql">true</property> <!-- 将hibernate生成的语句格式化 --> <property name="hibernate.format_sql">true</property> <!-- ## auto schema export 自动导出表结构,自动建表 #hibernate.hbm2ddl.auto create-drop:自动建表,每次框架运行结束都会将所有表删除(开发中测试用) #hibernate.hbm2ddl.auto create: 自动建表,每次框架运行都会创建新表,以前的表被覆盖,数据会丢失(开发中测试用) #hibernate.hbm2ddl.auto update(推介使用) :自动生成表,如果有不会再生成,如果表有变动,自动更新表(不会删除任何数据) #hibernate.hbm2ddl.auto validate :校验,不自动生成表,每次启动会检验数据库中表是否正确,检验失败,抛异常 --> <!-- --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 引入orm元数据 路径书写:填写src下的路径 --> <mapping resource="cn/itheima/domain/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
5.书写测试代码
package cn.itheima.b_api; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import cn.itheima.domain.Customer; /*功能:表达heibernate框架与数据库之间的链接(会话)Session类似于jdbc * 的connection对象.换可以完成对数据库中数据的增删改查 *Session 是hibernate操作数据库的核心对象 */ public class Demo3 { @Test //事务操作 public void fun1(){ //Configuration配置加载类,用于加载主配置,orm元数据加载 //1.创建,调用空参构造,空参加载法,加载src下的hibernate.cfg.xml Configuration conf=new Configuration().configure(); //SessionFactory:用于创建操作数据库核心对象session对象的工场 //2.根据配置信息,创建SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3.获得Session //打开一个新的Session对象 Session session = sf.openSession(); //获得与线程绑定的session,需要配置文件配置 //Session session2 = sf.getCurrentSession(); //4.session获得操作事务的 //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //----------------- //数据库增删改查操作 //--------------------- tx2.commit(); tx2.rollback(); session.close(); sf.close(); } @Test //session增加操作 public void fun2(){ //1.创建,空参构造 Configuration conf=new Configuration().configure(); //2.根据配置信息,创建SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3.获得Session //打开一个新的Session对象 Session session = sf.openSession(); //4.session获得操作事务的 //session.getTransaction(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //----------------- Customer c=new Customer(); c.setCust_name("传智播客"); session.save(c); //--------------------- tx2.commit(); tx2.rollback(); session.close(); sf.close(); } @Test //session查询操作 //查询id为1的customer对象 public void fun3(){ //1.创建,空参构造 Configuration conf=new Configuration().configure(); //2.根据配置信息,创建SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3.获得Session //打开一个新的Session对象 Session session = sf.openSession(); //4.session获得操作事务的 //session.getTransaction(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //----------------- Customer customer = session.get(Customer.class, 1l); System.out.println(customer); //--------------------- tx2.commit(); session.close(); sf.close(); } @Test //session修改操作 //修改id为1的customer对象name属性为黑马 public void fun4(){ //1.创建,空参构造 Configuration conf=new Configuration().configure(); //2.根据配置信息,创建SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3.获得Session //打开一个新的Session对象 Session session = sf.openSession(); //4.session获得操作事务的 //session.getTransaction(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //----------------- //1.获得要修改的对象 Customer c = session.get(Customer.class,1l); c.setCust_name("黑马"); session.update(c); //--------------------- tx2.commit(); session.close(); sf.close(); } @Test //session删除操作 //删除id为1的customer对象 public void fun5(){ //1.创建,空参构造 Configuration conf=new Configuration().configure(); //2.根据配置信息,创建SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3.获得Session //打开一个新的Session对象 Session session = sf.openSession(); //4.session获得操作事务的 //session.getTransaction(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //----------------- //1.获得要修改的对象 Customer c = session.get(Customer.class,6l);//3+"l" session.delete(c); //--------------------- tx2.commit(); session.close(); sf.close(); } }
6.封装utils工具类:
由于在获得session中代码重复,可使用工具类进行封装;
static{
}静态代码块,可用于加载驱动,在程序启动时只加载一次,why?移步https://blog.csdn.net/lxyzhu/article/details/41041641点击打开链接
package cn.itheima.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory sf; static { // 1.创建,空参构造 Configuration conf = new Configuration().configure(); // 2.根据配置信息,创建SessionFactory对象 sf = conf.buildSessionFactory(); } // 获得session,获得全新的session public static Session openSession() { // 3.获得Session // 打开一个新的Session对象 Session session = sf.openSession(); return session; } // 获得session,=>获得与线程绑定的session, public static Session getCurrentSession() { // 3.获得Session // 打开一个新的Session对象 Session session = sf.getCurrentSession(); return session; } }后一篇:hibernate中实体规则,对象状态,一级缓存,事务等内容.