标签:
1.hibernate.cfg.xml 配置
<!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://127.0.0.1:3306/student</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property><!-- 将SQL语句打印到控制台 --> <property name="hibernate.format_sql">true</property><!-- 将SQL语句格式化后,打印到控制台 --> <mapping resource="com/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
2.User.hbm.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <!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.hibernate.User"> <id name="id"> <generator class="uuid"/><!-- 自动生成一个32位的字符串,必须提供生成策略 --> </id> <property name="name"/> <property name="password"/> </class> </hibernate-mapping>
3.创建User实体
package com.hibernate;
public class User {
	
	private String id;
	private String name;
	private String password;
	//private String rights;
	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;
	}
}
4.ExportDB类(根据xml文件生成对应数据库的表)
package com.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/*
 * 将hbm生成ddl
 */
public class ExportDB {
	public static void main(String[] args) {
		//默认读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		SchemaExport export = new SchemaExport(cfg);
		export.create(true, true);	
	}
}
5.测试类Client
package com.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
	public static void main(String[] args) {		
		// org.hibernate.cfg.Configuration类的作用:
		// 读取hibernate配置文件(hibernate.cfg.xml或hiberante.properties)的.
		// new Configuration()默认是读取hibernate.properties
		// 所以使用new Configuration().configure();来读取hibernate.cfg.xml配置文件
		// 默认读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		// 创建SessionFactory
		// 一个数据库对应一个SessionFactory
		// SessionFactory是线线程安全的(最好创建一次)
		SessionFactory factory = cfg.buildSessionFactory();
		// 创建session
		// 此处的session并不是web中的session
		// session只有在用时,才建立concation,session还管理缓存。
		// session用完后,必须关闭。
		// session是非线程安全,一般是一个请求一个session.
		Session session = null;
		try {
			session = factory.openSession();
			//手动开启事务(可以在hibernate.cfg.xml配置文件中配置自动开启事务) 
			session.beginTransaction();
			
			// 保存数据,此处的数据是保存对象,这就是hibernate操作对象的好处,
			// 我们不用写那么多的JDBC代码,只要利用session操作对象,至于hibernat如何存在对象,这不需要我们去关心它,
			// 这些都有hibernate来完成。我们只要将对象创建完后,交给hibernate就可以了。
			User user =new User();
			user.setName("Lee");
			user.setPassword("00");
			session.save(user);
			//提交事物
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			//回滚事物
			session.getTransaction().rollback();
		}finally{
			if (session!=null) {
				if(session.isOpen())
					//关闭session
				    session.close();
			}
		}
	}
}
标签:
原文地址:http://my.oschina.net/u/2416019/blog/510072