hibernate 类生成表 的实现源码

来源:互联网 发布:淘宝管控交易风险保障 编辑:程序博客网 时间:2024/05/19 13:45

      使用hibernate的schemaExport 工具类实现将实体类转换成数据库中的表。在工程设计中,应该先设计表结构而不是先生成实体类,但是这只是个方法,虽然不推荐使用,但是还是需要记下来。

第一步:
在test包中创建一个生成表的java类:

 

    1. package com.test;   
    2.   
    3. import org.hibernate.cfg.Configuration;   
    4. import org.hibernate.tool.hbm2ddl.SchemaExport;   
    5.   
    6. public class 生成表 {   
    7.   
    8.     /**  
    9.      * @param args  
    10.      */  
    11.     public static void main(String[] args) {   
    12.         Configuration cfg = new Configuration().configure();   
    13.         SchemaExport ex   = new SchemaExport(cfg);   
    14.         ex.create(truetrue);   
    15.            
    16.     }   
    17.   
    18. }  

 

第二步:
写一个创建session的类:

  1. package com.test;   
  2.   
  3. import org.hibernate.Session;   
  4. import org.hibernate.SessionFactory;   
  5. import org.hibernate.cfg.Configuration;   
  6.   
  7. public class HibernateSessionFactory {   
  8.     private static Configuration cfg = new Configuration().configure();   
  9.     private static SessionFactory factory = cfg.buildSessionFactory();   
  10.     private static ThreadLocal<Session> local  = new ThreadLocal<Session>();   
  11.        
  12.     public static Session getSession(){   
  13.         Session session = local.get(); //取  
  14.         if (session==null || session.isOpen()==false){   
  15.             session = factory.openSession();   
  16.             local.set(session); //存   
  17.         }   
  18.         return session;   
  19.     }   
  20.        
  21. }  

第三步:
修改hibernate的配置文件相关属性、驱动。

  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.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->   
  7. <hibernate-configuration>   
  8.   
  9.     <session-factory>   
  10.         <property name="myeclipse.connection.profile">【这里是什么数据库就写什么】mysql</property>   
  11.         <property name="connection.url">   
  12.             【数据库连接是什么数据库写什么数据库test   
  13. 】jdbc:mysql://localhost:3306/test   
  14.         </property>   
  15.         <property name="dialect">   
  16.             【是mysql就写mysql orc就orc】org.hibernate.dialect.MySQLDialect   
  17.         </property>   
  18.         <property name="connection.username">root</property>   
  19.     <property name="connection.password">admin</property>   
  20.         <property name="connection.driver_class">   
  21.             【驱动要改 】com.mysql.jdbc.Driver   
  22.         </property>   
  23.         <property name="show_sql">true</property>   
  24.         <property name="format_sql">true</property>   
  25.         <mapping resource="com/pojos/TSaleformDetail3.hbm.xml" />   
  26.         <mapping resource="com/pojos/TSaleform3.hbm.xml" />   
  27.   
  28.     </session-factory>   
  29.   
  30. </hibernate-configuration>

第四步 改pojo映射文件:

  1. <hibernate-mapping>   
  2.     <class name="com.pojos.TSaleform3" table="T_SALEFORM3" schema="test">【这地方的schema=“数据库名字”   
  3. 】  

本文章转自网络,仅供学习交流用

 

 

    原创粉丝点击