隨筆-61  評(píng)論-159  文章-0  trackbacks-0
          PS:一般在項(xiàng)目開(kāi)發(fā)過(guò)程中,使用比較多的就是先建好表,再利用hibernate反向工程生成*.hbm.xml文件跟POJO類(lèi),個(gè)人認(rèn)為由于目前所使用的數(shù)據(jù)庫(kù)都是關(guān)系數(shù)據(jù)庫(kù),而hibernate作為一個(gè)ORM,把對(duì)數(shù)據(jù)庫(kù)的操作都對(duì)象化了,更應(yīng)當(dāng)從對(duì)象出發(fā),生成數(shù)據(jù)庫(kù)里面相關(guān)表,這樣更加符合人認(rèn)知事物的習(xí)慣。
                 由于hibernate3提供了自帶的工具hbm2ddl,建立根據(jù)你的對(duì)象建立數(shù)據(jù)庫(kù)是一件非常簡(jiǎn)單的事情。
                 Demo結(jié)構(gòu)圖如下:
                                 
          1、首先建立POJO類(lèi)
           1package org.apple.hibernate;
           2
           3public class User {
           4    private String id;
           5    private String name;
           6    private String password;
           7    public String getId() {
           8        return id;
           9    }

          10    public void setId(String id) {
          11        this.id = id;
          12    }

          13    public String getName() {
          14        return name;
          15    }

          16    public void setName(String name) {
          17        this.name = name;
          18    }

          19    public String getPassword() {
          20        return password;
          21    }

          22    public void setPassword(String password) {
          23        this.password = password;
          24    }

          25
          26}
          2、根據(jù)POJO類(lèi)里面里面相關(guān)的字段寫(xiě)User.hbm.xml映射文件
           1<?xml version="1.0" encoding="GB2312"?>
           2<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
           3<hibernate-mapping>
           4    <class name="org.apple.hibernate.User">
           5        <!--hibernate為我們生成主鍵id-->
           6        <id name = "id" unsaved-value = "null">
           7            <generator class="uuid.hex"/>
           8        </id>
           9        
          10        <!--默認(rèn)把類(lèi)的變量映射為相同名字的表列,當(dāng)然我們可以修改其映射方式-->
          11        <property name="name"/>
          12        <property name="password"/>
          13    </class>
          14</hibernate-mapping>
          3、建立hibernate.cfg.xml

           1<!DOCTYPE hibernate-configuration PUBLIC
           2    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
           3    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
           4
           5<hibernate-configuration>
           6    <session-factory>
           7        
           8        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
           9        <property name="hibernate.show_sql">true</property>
          10        <mapping resource="org/apple/hibernate/Person.hbm.xml"/>
          11    </session-factory>
          12</hibernate-configuration>
           4、建立 hibernate.properties數(shù)據(jù)庫(kù)鏈接
          ## 數(shù)據(jù)庫(kù)鏈接,密碼自己根據(jù)自己的實(shí)際數(shù)據(jù)庫(kù)進(jìn)行修改!
          hibernate.dialect org.hibernate.dialect.MySQLDialect
          hibernate.connection.driver_class com.mysql.jdbc.Driver
          hibernate.connection.url jdbc:mysql://localhost/usertest?useUnicode=true&characterEncoding=GBK
          hibernate.connection.username root
          hibernate.connection.password password

          5、建立UserTest類(lèi)
           1package org.apple.hibernate;
           2
           3import org.hibernate.cfg.Configuration;
           4import org.hibernate.tool.hbm2ddl.SchemaExport;
           5
           6
           7
           8class UserTest
           9    public static void main(String[] args) throws Exception{            
          10        //配置環(huán)境,分析xml映射文件
          11        Configuration conf= new Configuration()
          12            .addClass(User.class);
          13        
          14        //生成并輸出sql到文件(當(dāng)前目錄)和數(shù)據(jù)庫(kù)
          15        SchemaExport dbExport=new SchemaExport(conf);
          16        dbExport.create(truetrue);
          17            }

          18}
          6、建立log4j.properties日志文件

          ### direct log messages to stdout ###
          log4j.appender.stdout=org.apache.log4j.ConsoleAppender
          log4j.appender.stdout.Target=System.out
          log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
          log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

          ### set log levels - for more verbose logging change 'info' to 'debug' ###

          log4j.rootLogger=warn, stdout


          PS:要在mysql數(shù)據(jù)庫(kù)里面先建立好usertest表,然后運(yùn)行UserTest類(lèi),這樣就可以順利通過(guò)hibernate3提供了自帶的工具hbm2ddl,建立根據(jù)你的對(duì)象建立數(shù)據(jù)庫(kù)相關(guān)表。
          打開(kāi)mysql數(shù)據(jù)庫(kù):
                                
          大功告成!
          demo源碼下載
                                  



          -------------------------------------------------------------------------------------------------
          PS:本博客文章,如果沒(méi)有注明是有“轉(zhuǎn)”字樣,屬于本人原創(chuàng)。如果需要轉(zhuǎn)載,務(wù)必注明作者文章的詳細(xì)出處地址,否則不允許轉(zhuǎn)載,多謝合作!
          posted on 2008-09-29 12:56 apple0668 閱讀(18645) 評(píng)論(5)  編輯  收藏 所屬分類(lèi): hibernate

          評(píng)論:
          # re: 利用hibernate中的SchemaExport生成數(shù)據(jù)表 2008-09-29 22:20 | Bryan
          可以使用annotation生成數(shù)據(jù)庫(kù)的腳本,然后在通過(guò)數(shù)據(jù)庫(kù)生成hbm,這樣很快   回復(fù)  更多評(píng)論
            
          # re: 系統(tǒng)學(xué)習(xí)hibernate之一:利用hibernate中的SchemaExport生成數(shù)據(jù)表 2008-10-14 14:33 | kelly.zhang
          可以使用annotation生成數(shù)據(jù)庫(kù)的腳本,然后在通過(guò)數(shù)據(jù)庫(kù)生成hbm,這樣很快, 應(yīng)該怎么做啊???  回復(fù)  更多評(píng)論
            
          # re: 系統(tǒng)學(xué)習(xí)hibernate之一:利用hibernate中的SchemaExport生成數(shù)據(jù)表 2014-06-06 11:57 | 路飛
          Configuration conf= new Configuration().addClass(User.class);
          這一句有問(wèn)題!  回復(fù)  更多評(píng)論
            
          # re: 系統(tǒng)學(xué)習(xí)hibernate之一:利用hibernate中的SchemaExport生成數(shù)據(jù)表 2014-08-04 17:26 | fsdfa
          fsdfafsf  回復(fù)  更多評(píng)論
            
          # re: 系統(tǒng)學(xué)習(xí)hibernate之一:利用hibernate中的SchemaExport生成數(shù)據(jù)表[未登錄](méi) 2014-10-25 21:37 | a
          Configuration conf= new Configuration().configure().
          addClass(User.class);  回復(fù)  更多評(píng)論
            
          Email:chensp1230@163.com
          歡迎交流
          框架是告訴人們不該去做什么,而不是告訴人們?cè)撊プ鍪裁矗蝗萜魇歉嬖V人們?cè)撊プ鍪裁矗皇歉嬖V人們不該去做什么!
          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          留言簿(10)

          我參與的團(tuán)隊(duì)

          隨筆分類(lèi)(63)

          隨筆檔案(61)

          友情鏈接

          開(kāi)源在線手冊(cè)

          最新隨筆

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 浦城县| 巢湖市| 贡觉县| 屏东县| 华坪县| 庐江县| 鹿邑县| 平谷区| 临海市| 云霄县| 罗山县| 桂林市| 延长县| 大化| 常熟市| 城口县| 潮州市| 新干县| 涟水县| 积石山| 贺州市| 虞城县| 正镶白旗| 新乐市| 汉沽区| 习水县| 漳州市| 离岛区| 长岛县| 温宿县| 方正县| 宕昌县| 来安县| 平乐县| 板桥市| 汾阳市| 长岭县| 闸北区| 绥中县| 扬中市| 扎赉特旗|