hibernate自动建表

                                                               hibernate自动建表

 

 

               Oracle官方推荐的hibernate开发方式是domain类——>配置文件——>数据库。现在我们就来熟悉下该流程。一般domain类要实现Serializable接口成为一个序列化类。我们的domian类如下所示:我们设置的主键为编号,还有三个字段为英雄名字,英雄势力所属以及英雄属性分别为:hero_id、hero_name、hero_side、hero_attribute以及它们的set、get方法:

package com.zxd.hibernate.domain;
import java.io.Serializable;
public class Dota implements Serializable{
	private static final long serialVersionUID = 7239345948537354445L;
	private Integer hero_id;
	private String hero_name;
	private String hero_side;
	private String hero_attribute;	
	public Integer getHero_id() {
		return hero_id;
	}
	public void setHero_id(Integer hero_id) {
		this.hero_id = hero_id;
	}
	public String getHero_name() {
		return hero_name;
	}
	public void setHero_name(String hero_name) {
		this.hero_name = hero_name;
	}
	public String getHero_side() {
		return hero_side;
	}
	public void setHero_side(String hero_side) {
		this.hero_side = hero_side;
	}
	public String getHero_attribute() {
		return hero_attribute;
	}
	public void setHero_attribute(String hero_attribute) {
		this.hero_attribute = hero_attribute;
	}
}

              之后我们配置domain.hbm.xml文件如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zxd.hibernate.domain">
	<class name="Dota" table="Dota_hero">
	<id name="hero_id" column="hero_id" type="java.lang.Integer"><!-- id tag is for primary key -->
	<generator class="increment"></generator>
	</id>
	<property name="hero_name" column="hero_name" type="java.lang.String" not-null="false" >
	</property>
	<property name="hero_side" column="hero_side" type="java.lang.String" not-null="false">
	</property>
	<property name="hero_attribute" column="hero_attribute" not-null="false" type="java.lang.String">
	</property>
	</class>
</hibernate-mapping>

               然后配置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>
		<!-- 配置driver -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 切换数据库改这里 -->
		<property name="connection.username">username</property> <!-- 用户名和密码 -->
		<property name="connection.password">password</property>
		<property name="connection.url">jdbc:mysql://zxdzxdzxd.mysql.rds.aliyuncs.com:3333/zxddb</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>		<!-- 明确指出hibernate连接的是哪种数据库 -->
		<property name="show_sql">true</property>
		<!-- the property can create a table if the table is not exist -->
		<property name="hbm2ddl.auto">update</property>
		<!-- Indicates the location of domain.hbm.xml -->
		<mapping resource="com/zxd/hibernate/domain/Dota.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

               我们的测试代码如下:

package com.zxd.hibernate.view;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zxd.hibernate.domain.Dota;
import com.zxd.hibernate.factory.MyFactory;
public class CreateTable {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Session session = MyFactory.getSessionFactory().openSession();
		Transaction transacation = session.beginTransaction();
		Dota dota = new Dota();
		dota.setHero_name("CG");
		dota.setHero_side("jinwei");
		dota.setHero_attribute("strength");
		session.save(dota);
		transacation.commit();
		session.close();
	}
}

             在这里,MyFactory是产生SessionFactory的类,因为SessionFactory是一个重量级的耗费内存比较多的组件,所以这里利用单例模式生成。如下:

package com.zxd.hibernate.factory;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public final class MyFactory {
	static SessionFactory sessionFactory = null;
	static {
		 sessionFactory = new Configuration().configure().buildSessionFactory();
	}
	public MyFactory(){
	}
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
}

                最后测试成功,创建新的表并添加了数据如下:
hibernate自动建表_第1张图片

               小结:

               1.hibernate的自动建表功能其实就是在hibernate.cfg.xml配置文件中添加一项property也就是

<property name ="hbm2ddl.auto">update</property>,中间的值为update,如果为create的话会将原来的同名表覆盖掉。

               2.此外,整个文件的结构如下:

hibernate自动建表_第2张图片
              3.使用单例模式时,一般将该类用final修饰符修饰。

你可能感兴趣的:(Hibernate)