Hibernate数据模型导出工具类

在以前的项目中,我们都是先设计数据库,将数据库中的表一一建好,在考虑实现。在Hibernate中,我们不用在手动去建表,而是通过映射来操作数据库。怎样操作数据库呢?Hibernate3中为我们提供了hbm2ddl这样的工具,下面让我们看一个具体事例:


package com.bjpowernode.hibernate;

import java.util.Date;
/**
 * 实体类User
 * @author lyj
 *
 */
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;
}

}


package com.bjpowernode.hibernate;

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

/**
 * 将hbm生成ddl工具类
 * @author lyj
 *
 */
public class ExportDB {
  public static void main(String args[]){
	  
	    //默认读取hibernate.cfg.xml文件
	  Configuration cfg=new Configuration().configure();
	   
	   //Create(script,export)方法根据持久类和映射文件先删除架构后创建删除数据库架构。
	  //有两个参数,第一个为True就是把DDL语句输出到控制台,
	  //第二个为True就是根据持久类和映射文件先执行删除再执行创建操作
	  SchemaExport export=new SchemaExport(cfg);
	  export.create(true, true);
  }
}

如何理解 ?默认读取hibernate.cfg.xml文件,在Configuration类里我们可以看到:


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>
  <!-- "com.bjpowernode.hibernate.User" 这里的User是实体类而不是数据库要对应的表 -->
  <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核心配置文件详细解析提供一个链接:

http://www.cnblogs.com/jqyp/archive/2010/06/28/1766851.html

<!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>
	   <properyt name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</properyt>
	   <property name="hibernate.connection.username">root</property>
	   <property name="hibernate.connection.password">bjpowernode</property>
	   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	   <property name="hibernate.show_sql">true</property>
	   <property name="hibernate.format_sql">true</property>
	   
	   <!-- 具体映射文件 -->
	   <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
	   
	</session-factory>
</hibernate-configuration>

Hibernate的映射流程大概就是

实体类(.class)--->实体类映射文件(xx.hbm.xml--->hibernate核心配置文件(hibernate.cfg.xml)

--->数据库导出工具(DBExport)依赖于SchemaExport






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
private static void printTableMetaInfo(Session session) { Connection connection = session.connection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet result = metaData.getColumns(null, null, NameOfTable, null); String strInJava = ""; String typeInJava; while (result.next()) { String columnName = result.getString(4); if ("stampTime".equalsIgnoreCase(columnName)) { continue; } int columnType = result.getInt(5); String nameFirstLetterLower = columnName.substring(0, 1).toLowerCase() + columnName.substring(1); switch (columnType) { case Types.VARCHAR: case Types.LONGVARCHAR: case Types.LONGNVARCHAR: case Types.NVARCHAR: case Types.CHAR: typeInJava = "String"; break; case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: typeInJava = useInteger ? "Integer" : "int"; break; case Types.TIMESTAMP: case Types.BINARY: typeInJava = "Calendar"; break; case Types.DECIMAL: typeInJava = "BigDecimal"; break; case Types.BIGINT: typeInJava = "BigInteger"; break; case Types.LONGVARBINARY: typeInJava = "byte[]"; break; case Types.DATE: typeInJava = "Calendar"; break; default: throw new Exception("Unknown type " + columnType + " and column is " + columnName); } strInJava += " private " + typeInJava + " " + nameFirstLetterLower + ";\n"; // strInHibernate += "\n"; } String str = "import javax.persistence.Entity;\n" + "import javax.persistence.Id;\n" + "import javax.persistence.Table;\n" + "import java.util.Calendar;\n\n"; str += "@Entity\n"; str += "@Table(name=\"$\")\n".replace("$", NameOfTable); str += "public class $ {\n".replace("$", NameOfTable.substring(2)); str += "\n @Id\n"; str += strInJava; str += "}"; System.out.println(str); StringSelection stringSelection = new StringSelection(str); Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); clpbrd.setContents(stringSelection, null); } catch (Exception e) { e.printStackTrace(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值