Hibernate--通过代码生成数据库

 

DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,以及数据库对象,像:表、视图等等,DDL对这些对象和属性的管理和定义具体表现在 Create、Drop和Alter上。特别注意:DDL操作的“对象”的概念,”对象“包括对象及对象的属性,而且对象最小也比记录大个层次。以表举 例:Create创建数据表,Alter可以更改该表的字段,Drop可以删除这个表,从这里我们可以看到,DDL所站的高度,他不会对具体的数据进行操 作。

DML(Data Manipulation Language 数据操控语言)用于操作数据库对象中包含的数据,也就是说操作的单位是记录。

DCL(Data Control Language 数据控制语句)的操作是数据库对象的权限,这些操作的确定使数据更加的安全。

 

 

ExportDB:

package com.bjpowernode.hibernate;

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

/**
 * 将hbm生成ddl
 * @author Administrator
 *
 */
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);
	}
}

 User

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;
	}
	

}

User.hbm.xml

 

<?xml version='1.0' encoding='UTF-8'?>
     <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 


<!-- 完成实体映射类 -->
<hibernate-mapping>
	<class name="com.bjpowernode.hibernate.User">
		<id name="id">
			<!-- 提供生成策略 -->
			<generator class="uuid"/>
		</id>
		<property name="name"/>
		<property name="password"/>
		<property name="createTime"/>
		<property name="expireTime"/>
		
	</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://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">wm</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> 
	</session-factory>
	
</hibernate-configuration/>

 

posted @ 2016-08-14 15:47  ZeroWM  阅读(445)  评论(0编辑  收藏  举报