• 【通过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.

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




  • 相关阅读:
    【学习笔记】第二章 python安全编程基础---python爬虫基础(urllib)
    java Spring boot entity编写
    UI自动化基础
    初级:第五课 Tony and his family? 托尼和他的家人?
    初级:第四课 What do you do? 你是做什么的?
    初级:第三课 My Family 我的家人
    初级:第二课 Nice to Meet You 幸会
    初级:第一课 Self Introduction 自我介绍
    jmeter自定义函数
    Go笔记-结构、类型、常量
  • 原文地址:https://www.cnblogs.com/yaoxiaoxing/p/5420321.html
Copyright © 2020-2023  润新知