在接触「hibernate」以前,我对于数据库操做的认知,无非就是增删改查,认为要操做数据库,必然要编写SQL脚本,或者经过DBMS图形界面。然而,hibernate 改变了个人世界观。原来咱们能够 用面向对象的方式来操做数据库。Amazing!
这篇文章讲讲 hibernate 的一些基本概念及相关配置。
Hibernate 是一个开放源代码的ORM(Object Relational Mapping
,对象关系映射)框架,它对JDBC进行了轻量级的对象封装,使得Java开发人员可使用面向对象的编程思想来操做数据库。java
使用传统的JDBC开发应用系统时,若是是小型应用系统,并不以为麻烦,可是对于大型应用系统的开发,使用JDBC就会显得力不从心。例如对几10、几百张包含几十个字段的表进行插入操做时,编写的SQL语句不但很长,并且繁琐,容易出错;在读取数据时,须要写多条getXxx()
语句从结果集中取出各个字段的信息,不但枯燥重复,而且工做量很是大。mysql
ORM利用对象与数据库表之间的映射,自动把Java应用程序中的对象,持久化到关系数据库的表中。经过操做Java对象,就能够完成对数据库表的操做。sql
与其余操做数据库的技术相比,Hibernate有如下几点优点:数据库
在Mysql中建立名为 hibernate 的数据库,在此数据库中建立cst_customer
表。编程
create database hibernate; use hibernate; CREATE TABLE `cst_customer` ( `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)', `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;
在项目src
目录下,建立com.bingo.domain
包,并在包中建立实体类Customer
(对应数据库表cst_customer
),Customer
类包含与cst_customer
数据表字段对应的属性,以及相应的getXxx()
和setXxx()
方法。api
package com.bingo.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; public Customer() { super(); } public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level, String cust_linkman, String cust_phone, String cust_mobile) { super(); this.cust_id = cust_id; this.cust_name = cust_name; this.cust_source = cust_source; this.cust_industry = cust_industry; this.cust_level = cust_level; this.cust_linkman = cust_linkman; this.cust_phone = cust_phone; this.cust_mobile = cust_mobile; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } }
Hibernate 须要知道实体类Customer
映射到数据库 Hibernate 中的哪张表,以及类中的属性分别对应数据库表中的哪一个字段,这些都须要在映射文件中配置。session
在实体类Customer
所在的包中,建立一个名为Customer.hbm.xml
的映射文件,在该文件中定义了实体类Customer
的属性时如何映射到cst_customer
表的列上的。app
<?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> <!-- 创建类和表的一个映射关系 --> <!-- class标签:用来创建类和表的映射 name属性:类中的全路径 table属性:表名(若是类名和表名一致,那么table属性能够省略) catalog属性:数据库名称,能够省略 --> <class name="com.bingo.domain.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>
Hibernate 的映射文件反映了持久化类和数据库表的映射信息,而 Hibernate 的配置文件则主要用来配置数据库链接以及 Hibernate 运行时所须要的各个属性的值。
在项目的src
下建立一个名称为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?useUnicode=true&characterEncoding=utf8&useSSL=true</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <!-- 根据配置的方言生成相应的sql语句 --> <property name="hibernate.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的取值 * create:每次都会建立一个新的表(测试用) * create-drop:每次都会建立一个新的表,执行程序结束后删除这个表(测试用) * update:若是数据库中有表,使用原来的表;若是没有表,建立一个新的表,能够更新表结构 * validate:只会使用原来的表,对映射关系进行校验 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Hibernate加载映射 --> <mapping resource="com/bingo/domain/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
在项目中新建一个名称为com.bingo.test
的包,而后在包中创建一个名为Demo.java
的文件,该文件是用来测试的类文件。dom
package com.bingo.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import com.bingo.domain.Customer; //测试hibernate 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 c = new Customer(); c.setCust_name("bingo"); session.save(c); tx.commit(); session.close(); sessionFactory.close(); } }
首先建立Configuration
类的实例,并经过它来读取并解析配置文件hibernate.cfg.xml
。而后建立SessionFactory
读取解析映射文件信息,并将Configuration
对象中的全部配置信息拷贝到SessionFactory
内存中。接下来,打开Session
,让Session
提供链接并开启一个事务,以后建立对象,向对象中添加数据,经过session.save()
方法完成向数据库中保存数据的操做。最后提交事务,并关闭资源。
End...