hibernate 类生成表 的实现源码

本文介绍如何通过Hibernate的schemaExport工具类将实体类转换为数据库表。包括创建生成表的Java类、会话工厂类、配置文件及POJO映射文件的修改。

      使用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. 】  

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

 

 

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值