1:hibernate是什么
(1)框架是什么
1.框架是用来提高开发效率的 2.封装好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现. 3.所以框架可以理解成是一个半成品的项目.只要懂得如何驾驭这些功能即可.
(2)hibernate框架是什么
(3)hibernate的好处
操作数据库的时候,可以以面向对象的方式来完成.不需要书写SQL语句
(4)hibernate是一款orm框架
orm:object relationg mapping. 对象关系映射
(5)orm分4级
hibernate属于4级:完全面向对象操作数据库 mybatis属于2级 dbutils属于1级
2:hibernate框架的搭建
(1)导包(包括驱动包)
(2)创建数据库hibernate_32(不能省略),准备表(可以省略)
CREATE TABLE `cst_customer` (
`cust_id` bigint(20) NOT NULL auto_increment,
`cust_name` varchar(255) default NULL,
`cust_source` varchar(255) default NULL,
`cust_industry` varchar(255) default NULL,
`cust_level` varchar(255) default NULL,
`cust_linkman` varchar(255) default NULL,
`cust_phone` varchar(255) default NULL,
`cust_mobile` varchar(255) default NULL,
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(3)书写orm元数据(对象与表的映射配置文件)Customer.htm.xml
a.导入约束 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">
b.实体
package cn.itheima.domain;
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_linkman;
private String cust_phone;
private String cust_mobile;
}
c.orm元数据
<?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="cn.itheima.domain">
<class name="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>
<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.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_32
</property>
<property name="hibernate.connection.username">root
</property>
<property name="hibernate.connection.password">root
</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true
</property>
<property name="hibernate.format_sql">true
</property>
<property name="hibernate.hbm2ddl.auto">update
</property>
<mapping resource="cn/itheima/domain/Customer.htm.xml"/>
</session-factory>
</hibernate-configuration>
(5)代码测试
package cn
.itheima.hello
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
public class Demo {
@Test
public void fun1(){
Configuration conf = new Configuration()
.configure()
SessionFactory sessionFactory = conf
.buildSessionFactory()
Session session = sessionFactory
.openSession()
Transaction tx = session
.beginTransaction()
//-----------------------------------------
Customer cus = new Customer()
cus
.setCust_name(
"google11111")
session
.save(cus)
//-----------------------------------------
tx
.commit()
session
.close()
sessionFactory
.close()
}
}
3.hibernate API详解
(1).Configuration
a.创建
//
1创建,调用空参构造
Configuration conf =
new Configuration();
b.加载主配置
//
2读取指定主配置文件--》空参加载方法,加载src下的hibernate
.cfg.xml文件
conf
.configure()
c.加载orm元数据(扩展|了解)
d.创建sessionFactory
//4 根据配置信息,创建 SessionFactory对象
SessionFactory sessionFactory =
conf.buildSessionFactory();
(2).SessionFactory
a. 学习SessionFactory对象
* SessionFactory功能:用户创建操作数据库核心对象session对象的工厂,简单说功能就一个----》创建session对象
* 注意:
1.sessionFactory负责保存和使用所有的配置信息,消耗内存资源非常大
*
2.sessionFactory属于线程安全的对象设计
* 结论:保证在web项目中只创建一个sessionFactory
Session session = sessionFactory.openSession();
Session currentSession = sessionFactory.getCurrentSession();
(3).session
a.学习session对象
* session对象功能:表达hibernate框架和数据库之间的连接(会话)。session类似于JDBC年代的connection对象。
* 还可以完成对数据库中数据的增删改查操作,session是hibernate操作数据库的核心对象
b.获得事务
Transaction tx1 = session.getTransaction();
Transaction tx = session.beginTransaction();
c.增
Customer cus = new Customer()
cus
.setCust_name(
"google11111")
session
.save(cus)
d.查
Customer customer = session
.get(Customer
.class,
1l)
System
.out.println(customer)
e.改
Customer customer = session.
get(Customer.
class,
1l);
customer.setCust_name(
"完美");
session.update(customer);
d.删
Customer customer = session.
get(Customer.
class,
1l);
session.
delete(customer);
(4).Transaction
//打开事务
tx.
begin();
//提交事务
tx.
commit();
//回滚事务
tx.
rollback();