zoukankan      html  css  js  c++  java
  • Hibernate之SchemaExport+配置文件生成表结构

    今天说点基础的东西,说说如何通过SchemaExport跟hibernate的配置文件生成表结构。其实方法非常简单,只需要两个配置文件,两个Java类就可以完成。

    首先要生成表,得先有实体类,以Person.java为例:

    [java] view plain copy
     
    1. /** 
    2.  *  
    3.  * @author Administrator 
    4.  * @hibernate.class table="T_Person" 
    5.  */  
    6. public class Person {  
    7.       
    8.     /** 
    9.      * @hibernate.id 
    10.      * generator-class="native" 
    11.      */  
    12.     private int id;  
    13.       
    14.     /** 
    15.      * @hibernate.property 
    16.      */  
    17.     private String name;  
    18.       
    19.     /** 
    20.      * @hibernate.property 
    21.      */  
    22.     private String sex;  
    23.       
    24.     /** 
    25.      * @hibernate.property 
    26.      */  
    27.     private String address;  
    28.       
    29.     /** 
    30.      * @hibernate.property 
    31.      */  
    32.     private String duty;  
    33.       
    34.     /** 
    35.      * @hibernate.property 
    36.      */  
    37.     private String phone;  
    38.       
    39.     /** 
    40.      * @hibernate.property 
    41.      */  
    42.     private String description;  
    43.       
    44.     /** 
    45.      * @hibernate.many-to-one 
    46.      */  
    47.     private Orgnization org;  
    48.       
    49.     public String getAddress() {  
    50.         return address;  
    51.     }  
    52.     public void setAddress(String address) {  
    53.         this.address = address;  
    54.     }  
    55.     public String getDescription() {  
    56.         return description;  
    57.     }  
    58.     public void setDescription(String description) {  
    59.         this.description = description;  
    60.     }  
    61.     public String getDuty() {  
    62.         return duty;  
    63.     }  
    64.     public void setDuty(String duty) {  
    65.         this.duty = duty;  
    66.     }  
    67.     public int getId() {  
    68.         return id;  
    69.     }  
    70.     public void setId(int id) {  
    71.         this.id = id;  
    72.     }  
    73.     public String getName() {  
    74.         return name;  
    75.     }  
    76.     public void setName(String name) {  
    77.         this.name = name;  
    78.     }  
    79.     public String getPhone() {  
    80.         return phone;  
    81.     }  
    82.     public void setPhone(String phone) {  
    83.         this.phone = phone;  
    84.     }  
    85.     public String getSex() {  
    86.         return sex;  
    87.     }  
    88.     public void setSex(String sex) {  
    89.         this.sex = sex;  
    90.     }  
    91.     public Orgnization getOrg() {  
    92.         return org;  
    93.     }  
    94.     public void setOrg(Orgnization org) {  
    95.         this.org = org;  
    96.     }  
    97. }  

    接下来就是Person类对应的配置文件Person.hbm.xml,配置如下:

    [html] view plain copy
     
    1. <hibernate-mapping>  
    2.   <class table="T_Person" name="com.tgb.model.Person">  
    3.     <id name="id">  
    4.       <generator class="native"/>  
    5.     </id>  
    6.     <property name="name"/>  
    7.     <property name="sex"/>  
    8.     <property name="address"/>  
    9.     <property name="duty"/>  
    10.     <property name="phone"/>  
    11.     <property name="description"/>  
    12.     <many-to-one name="org"></many-to-one>  
    13.   </class>  
    14. </hibernate-mapping>  


    还有包含Person.hbm.xml相关信息的Hibernate默认配置文件,hibernate.cfg.xml:

    [html] view plain copy
     
    1. <hibernate-configuration>  
    2.   <session-factory>  
    3.     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
    4.     <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>  
    5.     <property name="hibernate.connection.username">root</property>  
    6.     <property name="hibernate.connection.password">123456</property>  
    7.     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
    8.     <property name="hibernate.show_sql">true</property>  
    9.     <property name="hibernate.hbm2ddl.auto">update</property>  
    10.     <property name="hibernate.current_session_context_class">thread</property>  
    11.     <mapping resource="com/tgb/model/Person.hbm.xml"/>  
    12.   </session-factory>  
    13. </hibernate-configuration>  

    万事俱备只欠东风,最后我们还需要一个根据上述内容生成数据表的小工具,即ExportDB.Java:

    [java] view plain copy
     
    1. import org.hibernate.cfg.Configuration;  
    2. import org.hibernate.tool.hbm2ddl.SchemaExport;  
    3.   
    4. public class ExportDB {  
    5.         
    6.     /**  
    7.      * @param args  
    8.      */    
    9.     public static void main(String[] args) {    
    10.             
    11.         // 默认读取hibernate.cfg.xml文件    
    12.         Configuration cfg = new Configuration().configure();    
    13.             
    14.         // 生成并输出sql到文件(当前目录)和数据库    
    15.         SchemaExport export = new SchemaExport(cfg);    
    16.             
    17.         // 创建表结构,第一个true 表示在控制台打印sql语句,第二个true 表示导入sql语句到数据库  
    18.         export.create(true, true);    
    19.     }    
    20. }  

    完成以上步骤以后,只需要执行ExportDB类即可,当然前提是已经在MySQL中创建了对应的数据库,我们这里创建了一个名为test的测试数据库。执行成功之后我们就可以看到数据库里已经有了我们的t_person表了,如下图所示:

  • 相关阅读:
    刷过算法题汇总
    List
    PHP+JQUEY+AJAX实现分页
    关于响应式布局
    bootscript/javascript组件
    关于H5框架之Bootstrap的小知识
    SEO优化---学会建立高转化率的网站关键词库
    jQuery Mobile学习笔记
    ANGULAR $HTTP请求
    css3 transition
  • 原文地址:https://www.cnblogs.com/wangyage/p/7227681.html
Copyright © 2011-2022 走看看