Hibernate的学习笔记(一)

写在前面

    什么是ORM?ORM全称是Object Relational Mapping,也叫对象关系映射,是一种持久层的技术。它解决了域模型对象和关系型数据库不匹配的现象,通过映射从而将对象自动持久到数据库中。目前主流的框架是Mybatis,它是一种半自动化的ORM框架,需要写sql语句,操作性强,更为灵活。另一种在许多企业中仍然还在使用,它就是Hibernate,它是一种全自动的ORM框架,对数据库的可移植性更强。它们都对传统的JDBC访问数据库的代码做了轻量级的封装,从而大大简化了数据访问层大量重复的代码,减少内存消耗,提高了开发效率。

1.Hibernate框架的搭建

    1.1 导包

          这里主要包括Hibernate所必须的jar包和数据库驱动包

    1.2 创建表,实体(持久化类)

CREATE TABLE cst_customer(
	cust_id BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
	cust_name VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
	cust_level VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
	cust_mobile VARCHAR(16) DEFAULT NULL COMMENT '移动电话', 
	PRIMARY KEY(cust_id)
)ENGINE=INNODB AUTO_INCREMENT=95 DEFAULT CHARSET=utf8;
package com.csdn.domain;

public class Customer {
	private Long cust_id;
	private String cust_name;
	private String cust_level;
	private String 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_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}
}

    1.3 创建映射文件

          实体类Customer目前还不具备持久化操作的能力,所以需要在映射文件中进行实体类和数据库表的映射,以及类中的属性和表的字段的映射,故需要创建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>  
	<!-- class标签用来建立实体类和表的映射 -->
    <class name="com.csdn.domain.Customer" table="cst_customer">
	    <!-- id标签用来建立实体类中的属性与表中主键字段的映射 -->
	    <id name="cust_id" colume="cust_id">
	    	<!-- 主键生成策略 -->
	    	<generator class="native"></generator>
	    </id>
	    <!-- property用来建立实体类中的属性和表中普通字段的映射 -->
	    <property name="cust_name" column="cust_name"/>
	    <property name="cust_source" column="cust_source"/>
	    <property name="cust_level" column="cust_level"/>
	    <property name="cust_mobile" column="cust_mobile"/>
    </class>
</hibernate-mapping>

    1.4 创建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://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
		
		<!-- Hibernate方言:作用时根据配置的方言生成相应的sql语句 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- Hibernate显示sql语句 -->
        <property name="show_sql">true</property>
		<!-- Hibernate格式化sql语句 -->
        <property name="format_sql">true</property>
		<!-- 这里的取值有
		none:不用Hibernate自动生成表
		create:每次都会创建一个新的表(测试)
		create-drop:每次都会创建一个新的表,程序执行结束删除表(测试)
		update:如果数据库中有表,使用原来的表,如果没有表,创建一个新的表,可以更新表结构
		validate:只使用原来的表,对映射关系进行校验 -->
        <property name="hbm2ddl.auto">update</property>
		<!-- Hibernate加载映射,这里只选取src之后的 -->
        <mapping resource="com/csdn/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

    1.5 编写测试代码

          至此,框架基本搭成,需要新建一个测试包,并书写测试文件进行验证。

package com.csdn.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.csdn.domain.Customer;

public class CustomerTest {
	
	@Test
	public void test() {
		//1.加载配置文件
		Configuration cfg = new Configuration().configure();
		//2.创建一个SessionFactory
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		//3.创建Session对象
		Session session = sessionFactory.openSession();		
		//4.开启事务
		Transaction tx = session.beginTransaction();
		//5.执行相关操作
		Customer customer = new Customer();
		customer.setCust_name("Tom");
		customer.setCust_mobile("13678234567");
		
		session.save(customer);
		//6.事务提交
		tx.commit();
		//7.释放资源
		session.close();
	}
	
}

    1.6 结果展示

         当在控制台上看到如下情况,说明成功了。当然也可以直接进入到数据库中查看。

2.总结

    Hibernate中最重要的两个文件:映射文件和配置文件。首先创建Configuration类的实例,并通过它来读取并解析配置文件。然后在通过创建SessionFactory读取解析映射文件信息,并将Configuration对象中的所有配置信息拷贝到SessionFactory的内存中。同时openSession,让SessionFactory提供连接,并开启事务,并创建对象,同时像对象中添加数据。最后通过session.save将数据保存至数据库中。提交事务,关闭资源。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值