【通过Hibernate反向生成对应的数据库表单】

首先,在src目录下,有一个文件,hibernate.cfg.xml,该文件的内容如下:
 
   
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <!-- Generated by MyEclipse Hibernate Tools. -->
  6. <hibernate-configuration>
  7. <session-factory>
  8. <property name="connection.username">FI_XIAO</property>
  9. <property name="connection.url">
  10. jdbc.url=jdbc:oracle:thin:@localhost:1521:FINANCE
  11. </property>
  12. <property name="dialect">
  13. org.hibernate.dialect.OracleDialect
  14. </property>
  15. <property name="myeclipse.connection.profile">oracle</property>
  16. <property name="connection.password">FI_XIAO</property>
  17. <property name="connection.driver_class">
  18. oracle.jdbc.driver.OracleDriver
  19. </property>
  20. <property name="show_sql">true</property>
  21. <mapping resource="com/system/SysUser.hbm.xml" />
  22. </session-factory>
  23. </hibernate-configuration>

其中,数据库名为:finance 
2.
接下来,就是编写生成表的代码了,很简单,代码如下:
 
   
  1. package utils;
  2. import Java.io.File;
  3. import org.hibernate.HibernateException;
  4. import org.hibernate.Session;
  5. import org.hibernate.SessionFactory;
  6. import org.hibernate.Transaction;
  7. import org.hibernate.cfg.Configuration;
  8. import org.hibernate.tool.hbm2ddl.SchemaExport;
  9. public class HibernateSchemaExport {
  10. static Session session;
  11. ?static Configuration config = null;
  12. static Transaction tx = null;
  13. ?public static void main(String[] args) {
  14. try {
  15. config = new Configuration().configure(new File(
  16. "src/hibernate.cfg.xml"));
  17. System.out.println("Creating tables...");
  18. SessionFactory sessionFactory = config.buildSessionFactory();
  19. session = sessionFactory.openSession();
  20. tx = session.beginTransaction();
  21. SchemaExport schemaExport = new SchemaExport(config);
  22. schemaExport.create(true, true);
  23. System.out.println("Table created.");
  24. tx.commit();
  25. } catch (HibernateException e) {
  26. e.printStackTrace();
  27. try {
  28. tx.rollback();
  29. } catch (HibernateException e1) {
  30. e1.printStackTrace();
  31. }
  32. } finally {
  33. }
  34. }
  35. }

在运行前,请先确定数据库服务已经开启,
运行结果如下:
 
   
  1. log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
  2. log4j:WARN Please initialize the log4j system properly.
  3. Creating tables...
  4. drop table if exists Message
  5. drop table if exists ProjectInfo
  6. drop table if exists about
  7. drop table if exists gongshi
  8. drop table if exists newslist
  9. drop table if exists projectList
  10. drop table if exists userlist
  11. create table Message (id varchar(50) not null, msg text, adddate varchar(45) not null, ip varchar(45) not null, isflog varchar(2) not null, primary key (id))
  12. create table ProjectInfo (id varchar(50) not null, names varchar(100) not null, info text not null, img text not null, primary key (id))
  13. create table about (id varchar(50) not null, names varchar(45), tel varchar(45) not null, telcz varchar(45) not null, email varchar(100) not null, url varchar(200) not null, address text, primary key (id))
  14. create table gongshi (id varchar(10) not null, info text not null, info2 text not null, primary key (id))
  15. create table newslist (id varchar(50) not null, title varchar(100) not null, txt text not null, img text not null, adddate varchar(45) not null, downcount varchar(45) not null, ly varchar(45) not null, isflog varchar(45) not null, primary key (id))
  16. create table projectList (id varchar(50) not null, img text not null, adddate varchar(45) not null, primary key (id))
  17. create table userlist (id varchar(50) not null, names varchar(45) not null, pwd varchar(100) not null, regdate varchar(45) not null, flog varchar(2) not null, primary key (id))
  18. Table created.

这样子,就完成了数据表的创建。




转载于:https://www.cnblogs.com/yaoxiaoxing/p/5420321.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值