利用hibernate中的SchemaExport生成數據表

PS:一般在項目開發過程中,使用比較多的就是先建好表,再利用hibernate反向工程生成*.hbm.xml文件跟POJO類,個人認爲由於目前所使用的數據庫都是關係數據庫,而hibernate作爲一個ORM,把對數據庫的操作都對象化了,更應當從對象出發,生成數據庫裏面相關表,這樣更加符合人認知事物的習慣。
      由於hibernate3提供了自帶的工具hbm2ddl,建立根據你的對象建立數據庫是一件非常簡單的事情。
      Demo結構圖如下:

1、首先建立POJO類
1package org.apple.hibernate;
2
3publicclass User {
4private String id;
5private String name;
6private String password;
7public String getId() {
8return id;
9    }

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

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

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

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

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

25
26}
2、根據POJO類裏面裏面相關的字段寫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<!--默認把類的變量映射爲相同名字的表列,當然我們可以修改其映射方式-->
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數據庫鏈接
## 數據庫鏈接,密碼自己根據自己的實際數據庫進行修改!
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
1package org.apple.hibernate;
2
3import org.hibernate.cfg.Configuration;
4import org.hibernate.tool.hbm2ddl.SchemaExport;
5
6
7
8class UserTest{
9publicstaticvoid main(String[] args) throws Exception{            
10//配置環境,分析xml映射文件
11        Configuration conf=new Configuration()
12            .addClass(User.class);
13
14//生成並輸出sql到文件(當前目錄)和數據庫
15        SchemaExport dbExport=new SchemaExport(conf);
16        dbExport.create(true, true);
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數據庫裏面先建立好usertest表,然後運行UserTest類,這樣就可以順利通過hibernate3提供了自帶的工具hbm2ddl,建立根據你的對象建立數據庫相關表。
打開mysql數據庫:

大功告成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章