-
Hibernate框架是什么
Hibernate的好处?
操作数据库的时候,可以以面向对象的方式来完成,不需要书写sql语句。
Hibernate是一款ORM框架
-
Hibernate框架的搭建步骤:
- 导包
- 创建数据库,准备表,实体
- 书写ORM元数据(对象与表的映射配置文件)
- 书写主配置文件
- 书写代码测试
Hibernate API 详解:
书写ORM元数据(对象与表的映射配置文件)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="com.cn.domain">
<!--
class元素:配置实体与表的对应关系的
name:完整类名
table:数据库表名
-->
<class name= "Customer" table = "cst_customer">
<id name= "cust_id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- property元素:除id之外的普通属性映射
name: 填写属性名
column(可选): 填写列名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
-->
<property name="cust_name" column="cus_name"></property>
<property name="cust_source" column="cus_source"></property>
<property name="cust_level" column= "cus_level"></property>
<property name= "cust_linkman" column= "cust_link"></property>
</class>
</hibernate-mapping>
书写主配置文件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.connnection.driver_class">com.mysql.jdbc.Driver</property>
<!-- url -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_32</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">1234</property>
<!-- 数据库方言
不同的数据库中,sql语法略有区别,指定方言可以让hibernate框架在生成sql语句时,针对数据库的方言生成。
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 将hibernate生成的sql语句打印到控制台 -->
<property name = "hibernate.show_sql">true</property>
<!--将hibernate生成的sql语句格式化(语法缩进) -->
<property name="hhibernate.format_sql">true</property>
<!--
## auto schema export 自动导出表结构. 自动建表
#hibernate.hbm2ddl.auto create 自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)
#hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)
#hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).
#hibernate.hbm2ddl.auto validate 校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败.
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 核心配置文件中关联映射文件 -->
<!-- 引用orm元数据
路径书写:填写src下的路径
-->
<mapping resource="/com/cn/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试文件Demo.java,如下图:
package com.cn.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.cn.domain.Customer;
/*学习SessionFactory
* SessionFactory功能:用于创建操作数据库核心对象session对象的工厂
* 简单说功能就一个----创建session对象
* 注意:1.sessionfactory负责保存和使用所有的配置信息,消耗内存资源非常大
* 2.sessionFacory属于线程安全的对象设计
*
*
* 学习session对象功能
*表达hibernate框架与数据库之间的连接(会话),session类似于JDBC年代的connection对象,
*还可以完成对数据库中数据的增删改查操作。
*session是hibernate操作数据库的核心对象。
*
*
* transaction对象
* 封装了事务的操作
* 打开事务
* 提交事务
* 回滚事务
*/
public class Demo {
@Test
public void fun1(){
//1.创建,调用空参构造
//2.读取指定主配置文件=>空参加载方法,加载src下的Hibernate.cfg
Configuration conf = new Configuration().configure();
//3.根据配置信息,创建Sessionfactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
//4.获取session
Session openSession = sessionFactory.openSession();
//5.开启事务并获得操作事务的tx对象
Transaction tx = openSession.beginTransaction();
//----------------------------------
Customer c = new Customer();
c.setCust_name("google公司");
openSession.save(c);//执行保存
//--------------------------------------
tx.commit();//提交事务
openSession.close();
sessionFactory.close();
//openSession.get(arg0, arg1);
}
}
补充:Hibernate框架中如何添加约束:
1.Window-preference-搜索catalog-Add ,如下图:
1.
2.Location 中选择要导入的约束(以map为例)
key type:选择uri
key:http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd