Hibernate_01

本文介绍Hibernate框架的基本概念,包括其作为ORM框架如何简化数据库操作,并通过示例演示如何将Hibernate集成到项目中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 先回顾一下在dao层操作数据库的技术?
 * 条件:只要操作数据库的数据,必须遵循java的规范->jdbc规范
 * jdbc缺点:
 * 		步骤太频繁(创建链接,销毁链接(太消耗资源));
 * 			举个例子:创建链接和销毁链接花费时间为各0.1毫秒,双11 1千万人访问就 算算消耗多少时间
 * jdbc+连接池:一样频繁
 * apache组织:DBUtils 对jdbc+连接池的封装:缺点:
 * 		QueryRunner qr = new QueryRunner(连接池)
 * 		当qr.update(sql,对象)
 * 		如果对象特别多的话,得一一get属性
 * 这时候hibernate框架出现了
 * 		最终目的就是解决qr.update(对象)
 * 
 * hibernate的入门
 * 		Hibernate就是一个持久层ORM框架,对JDBC做了封装(ps:ORM框架有很多,hibernate只是其中一种)
 * 什么是ORM框架:对象关系映射
 * 对象关系映射:就是将对象和数据库的表建立映射关系
 * 		怎么建立:
 * 			a.将类名和表名建立映射关系,b.将属性和主键建立映射关系,c.将其他属性和其他字段映射关系
 * 		好处:操作对象就相当于操作数据库的表
 * Hibernate框架的作用?
 * 		a.简化了Dao层的编码工作,简化了数据访问层繁琐的重复性代码,加快了运行效率
 * 		b.程序更加面向对象(可以不需要编写sql语句),只需要操作相应的对象就可以完成数据库的crud(针对单表)
 * 
 * 
 * 将hibernate和项目集成:
 * 		将hibernate开发包(hibernate-release-5.0.7.Final.zip)
 * 		https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
 * 		解压效果:
 * 			documentation:hibernate的开发规范和文档
 * 			lib			    :hibernate开发使用jar包
 * 			project		    :hibernate提供的测试工程
 * 		hibernate-release-5.0.7.Final\lib\required\*  9个所有包 
 * 		hibernate-release-5.0.7.Final\lib\optional\c3p0\*  3个所有包
 * 		hibernate在运行的过程中会加载一个日志包    apacha-log4j.jar 3个日志包
 * 		外加一个数据驱动包(mysql) 1个包
 * @author zx
 *
 */
<?xml version="1.0" encoding="UTF-8"?>

<!-- 约束可以在hibernate-core-5.0.7.Final.jar->org.hibernate倒数dtd约束大概10行 -->
<!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>
			<!-- 配置数据库的信息 
				 ps:去hibernate-release-5.0.7.Final\project\etc\hibernate.properties
			-->
			<!-- 配置mysql驱动 -->
			<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>	
			<!-- 配置mysql数据库地址 -->  
			<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>	
			<!-- 配置mysql数据库的用户名 -->
			<property name="hibernate.connection.username">root</property>	
			<!-- 配置mysql数据库的密码 -->
			<property name="hibernate.connection.password">root</property>	
			<!-- 配置方言  告诉hibernate生成的sql数符合我当前数据库的sql语句
					例子: 分页
						  mysql: select * from 表  limit ?,?
						  sqlserver: select * from 表 top ?,?
						  oracle: 自己手写sql嵌套
			 -->
			<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
			<!-- 为hibernate配置c3p0 -->
			<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
			<!-- 为了让hibernate生成的sql语句在控制台显示出来 -->
			<property name="hibernate.show_sql">true</property>
			<!-- 为了让显示的sql语句格式化好看点 -->
			<property name="hibernate.format_sql">true</property>
			
			<!-- 配置让hibernate根据映射关系给自动生成数据库的表  默认是不生成的 
				 取值:
				 		create:没有表创建表,有表了删除表在创建
				 		create-drop:没有表创建表,有表了删除表在创建  用完再删
				 		update: 用它  没有表创建表 有表使用表
				 		validate: 默认值 不让hibernate创建
			
			-->
			<property name="hibernate.hbm2ddl.auto">update</property>
			<!-- 引入映射文件地址 -->
			<mapping resource="Customer.hbm.xml"/>
		</session-factory>
	</hibernate-configuration>
<?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">
    <!-- 
    	class:是映射 实体与表名
    	table:是表名
     -->
    <hibernate-mapping>
    	<class name="xinfei.code.domain.Customer" table="cst_customer">
    		<!-- id:建立某个属性和表主键的映射  name:类的某个属性名  column:表的主键字段名-->
    		<id name="cust_id" column="cust_id">
    			<!-- 一个主键自增的标识	native = AUTO_INCREMENT -->
    			<generator class="native"></generator>
    		</id>
    		<!-- property 建立其他属性和其他字段的映射 name:属性名  column:表的字段名 -->
    		<!-- ps:name和column属性和表名相同 column可以不写 -->
    		<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_address" column="cust_address"></property>
    		<property name="cust_phone" column="cust_phone"></property>
    	</class>
    </hibernate-mapping>
package xinfei.code.domain;

public class Customer {
	private int cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_address;
	private String cust_phone;

	public int getCust_id() {
		return cust_id;
	}

	public void setCust_id(int 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_address() {
		return cust_address;
	}

	public void setCust_address(String cust_address) {
		this.cust_address = cust_address;
	}

	public String getCust_phone() {
		return cust_phone;
	}

	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}

	public Customer() {
		super();
	}

	public Customer(int cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
			String cust_address, String cust_phone) {
		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_address = cust_address;
		this.cust_phone = cust_phone;
	}

	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_address=" + cust_address
				+ ", cust_phone=" + cust_phone + "]";
	}

}
package xinfei.code.test;

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

import xinfei.code.domain.Customer;

public class TestDemo {

	@Test
	public void test_save_customer(){
		//加载配置文件 -> src/hibernate.cfg.xml
		Configuration configuration = new Configuration();
		//加载映射文件 类的配置文件  Customer.hbm.xml
		configuration.configure();
		//手动加载
		//configuration.addResource("src/Customer.hbm.xml");
		//获取sessionFactory 连接池
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		/**
		 * 这里需要注意:获取数据库链接 必须开启事务
		 */
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();//开始事务
		//操作对象
		Customer customer = new Customer();
		customer.setCust_name("测试名称");
		customer.setCust_phone("130xxxx8098");
		customer.setCust_source("某某地");
		session.save(customer);
		//提交事务
		transaction.commit();
		session.close();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值