通过实体类及映射文件生成数据库表

待实体类及映射文件生成后,进行如下操作:

1、在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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
    <session-factory>
        <property name="myeclipse.connection.profile">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://192.0.0.0:3306/databaseName
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">****</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="show_sql">true</property>   
<!-- auto schema export --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/prochanges/framework/domain/hbm/Addressbook.hbm.xml" /> <mapping      ......

</session-factory>
</hibernate-configuration>

2、编写hbm2ddl工具类,将实体类生成数据库表,运行如下main方法即可

package com.hutton;

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

public class GenerateDatabaseTable
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        //read the config file
        Configuration conf = new Configuration().configure();
        SchemaExport export = new SchemaExport(conf);
        export.create(true, true);
    }

}

相关属性信息可参见:

  D:\...\hibernate-3.2\etc\hibernate.properties文件

#hibernate.hbm2ddl.auto create-drop  根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变
#hibernate.hbm2ddl.auto create 根据model类生成表,但是sessionFactory一关闭,表就自动删除
#hibernate.hbm2ddl.auto update 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
#hibernate.hbm2ddl.auto validate  只会和数据库中的表进行比较,不会创建新表,但是会插入新值

 

转载于:https://www.cnblogs.com/hutton/p/3738275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值