Hibernate第一个demo及工作原理

首先来搭建一个Hibernate项目。

  环境:MyEclipse和MySQL数据库。

  创建一个java项目,创建User Libraary,加入相关的依赖包,以及mysql的驱动。

*HIBERNATE_HOME/lib/*.jar

*HIBERNATE_HOME/hibernate3.jar

* 加入数据库驱动(mysql驱动)


通过例子中的代码来解析我们的Hibernate的工作原理。

一、使用HIbernate之前的配置工作

1、配置hibernate.cfg.xml文件,完成数据库的连接。

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/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_first</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
<!--2、读取并解析映射信息   User.hbm.xml-->
		<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration></span>


这个文件我们可以在HIbernate包中找到。并且可以参考hibernate.properties来配置里面的东西。


2、在我们建立的User.java 的实体类之后,我们提供User.hbm.xml文件,完成实体类的映射和数据库的。同时 在hibernate.cfg.xml添加映射的实体

User.hbm.xml文件加入到hibernate.cfg.xml核心文件中。使数据库知道这个映射存在。我们在上面已经配置了。

<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.bjpowernode.hibernate;

import java.util.Date;

public class User {

	private String id;
	
	private String name;
	
	private String password;
	
	private Date createTime;
	
	private Date expireTime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}
}</span>

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.bjpowernode.hibernate.User">
		<id name="id">
			<generator class="uuid"></generator>
		</id>
		<property name="name"></property>
		<property name="password"></property>
		<property name="createTime"></property>
		<property name="expireTime"></property>
	</class>
	
</hibernate-mapping>

如果我们不会写这个映射文件可以参考hibernate包中提供的文件。


3、通过编写工具类,将自己写好的实体映射文件映射到数据库中生成表。前提,得有相应的库在mysql中已经建好了。

package com.bjpowernode.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * 将编写工具类ExoprtDB.java,将hbm生成ddl,也就是hbm2ddl  
写个类,不然不知道如何生成表
 * @author pc
 *
 */
public class ExportDB {
	public static void main(String [] args){
	/*	//读取hibernate.properties文件
		Configuration cfg = new Configuration();*/
		
		//读取默认hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		
		SchemaExport export = new SchemaExport(cfg);
		
		//导出DDL
		export.create(true, true);
	}
}


二、HIbernate的工作原理
    我们通过HIbernate对于数据库的操作。其实在配置好前面的东西之后这里就是HIbernate的工作原理了。

1.通过Configuration config = new Configuration().configure();//读取并解析hibernate.cfg.xml配置文件
2.由hibernate.cfg.xml中的<mapping resource="com/xx/User.hbm.xml"/>读取并解析映射信息
3.通过SessionFactory sf = config.buildSessionFactory();//创建SessionFactory
4.Session session = sf.openSession();//打开Sesssion
5.Transaction tx = session.beginTransaction();//创建并启动事务Transation
6.persistent operate操作数据,持久化操作
7.tx.commit();//提交事务
8.关闭Session,关闭SesstionFactory

package com.bjpowernode.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Client {

	public static void main(String[] args) {
		
		//1、读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		
		//3、建立SessionFactory,一个sessionfactory对应一个数据库
		SessionFactory factory = cfg.buildSessionFactory();
		
		//取得session
		Session session = null;
		try {
		    //4、打开session,session负责是持久化对象的curd操作
			session = factory.openSession();
			//5、创建并开启事务
			session.beginTransaction();
			//6、操作数据,持久化操作
			User user = new User();
			user.setName("张三");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			
			//保存User对象
			session.save(user);
			
			//7、提交事务
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			//回滚事务
			session.getTransaction().rollback();
		}finally {
			//8、关闭session和sessionfactory
			if (session != null) {
				if (session.isOpen()) {
					//关闭session
					session.close();
				}
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值