一、Hibernate框架简介
Hibernate框架是帮助我们完成数据库操作的,Hibernate是一款ORM(object relationg mapping对象关系映射)框架,我们在操作数据库时可以以明显对象的方式来完成,不需要书写SQL语句。ORM框架分为四级:Hibernate属于4级:完全面向对象操作数据库;Mybatis属于2级;jdbc数据一级。
二、Hibernate框架搭建
Hibernate框架搭建需要以下几步:
1.导包
下载 hibernate-release-5.0.7.Final压缩包(版本号不一定),将压缩包下的lib/required下的所有包导入项目中
2.创建数据库,准备表,实体
根据设计文档建立数据库以及相应的表(表可以省略,后期配置文件配好可以自动生成)
3.书写orm元数据(对象与表的映射配置文件)
1>导入约束
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
此语句在/org/hibernate/hibernate-mapping-3.0.dtd文件中可以找到
2>准备实体类,需要包含set/get方法,属性私有尽量使用包装类定义,包含空参构造函数(默认有),不能用final修饰class。例如准备实体类如下:
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;
3>orm元数据(文件名称为:xxx.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="cn.xben.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.书写主配置文件
1>导入约束和上一个步骤一样,url换为http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
<?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">
此语句在/org/hibernate/hibernate-configuration-3.0.dtd文件中可以找到
2>
<?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_test</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.connection.isolation">4</property> <!-- 隔离级别 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="cn/xben/domain/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.测试