码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate是怎么工作的——Hibernate的工作流程

时间:2017-05-28 20:50:03      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:流程   配置   ati   java   article   文件夹   .hbm.xml   文档   for   

举个简单的样例说明:

1.Base.java

package cn.flymouse.hibernate;

import java.util.Date;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.flymouse.hibernate.domain.User;

public class Base {

	public static void main(String[] args) {
		//读取并解析配置文档hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		//读取并解析映射文件,以Configuration创建SessionFactory
		SessionFactory sf = cfg.buidSessionFactory();
		//创建session
		Session session = sf.openSession();
		//开启事务
		Transaction tx = session.beginTransaction();
		//持久化操作
		User user = new User();
		user.setBirthday(new Date());
		user.setName("name");
		session.save(user);
		//提交事务
		tx.commit();
		//关闭Session
		session.close();
		//关闭SessionFactory
		sf.close();
	}
}

2.User.java

package cn.flymouse.hibernate.domain;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private Date birthday;

	public int getId() {
		return id;
	}
	pubilc String getName(){
		return name;
	}
	public Date getBirthday() {
		return birthday;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

3.User的映射文件User.hbm.xml(跟User类放同个文件夹): 

<?

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 package="cn.flymouse.hibernate.domain"> <class name="User"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="false"/> <property name="birthday" /> </class> </hibernate-mapping>


4.hibernate的配置文件hibernate.cfg.xml(放在类路径下,即src文件夹下): 

<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///test</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<!-- 方言,用于生成SQL语句 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 自己主动建表 -->
		<property name="hbm2ddl.auto">create</property>
		<property name="show_sql">true</property>
	
		<mapping resource="cn/flymouse/hibernate/domain/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


Hibernate是怎么工作的——Hibernate的工作流程

标签:流程   配置   ati   java   article   文件夹   .hbm.xml   文档   for   

原文地址:http://www.cnblogs.com/yutingliuyl/p/6916503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!