随笔-61  评论-159  文章-0  trackbacks-0
PS:一般在项目开发过程中,使用比较多的就是先建好表,再利用hibernate反向工程生成*.hbm.xml文件跟POJO类,个人认为由于目前所使用的数据库都是关系数据库,而hibernate作为一个ORM,把对数据库的操作都对象化了,更应当从对象出发,生成数据库里面相关表,这样更加符合人认知事物的习惯。
       由于hibernate3提供了自带的工具hbm2ddl,建立根据你的对象建立数据库是一件非常简单的事情。
       Demo结构图如下:
                       
1、首先建立POJO类
 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、根据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
 9    public static void 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(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数据库里面先建立好usertest表,然后运行UserTest类,这样就可以顺利通过hibernate3提供了自带的工具hbm2ddl,建立根据你的对象建立数据库相关表。
打开mysql数据库:
                      
大功告成!
demo源码下载
                        



-------------------------------------------------------------------------------------------------
PS:本博客文章,如果没有注明是有“转”字样,属于本人原创。如果需要转载,务必注明作者文章的详细出处地址,否则不允许转载,多谢合作!
posted on 2008-09-29 12:56 apple0668 阅读(18619) 评论(5)  编辑  收藏 所属分类: hibernate

评论:
# re: 利用hibernate中的SchemaExport生成数据表 2008-09-29 22:20 | Bryan
可以使用annotation生成数据库的脚本,然后在通过数据库生成hbm,这样很快   回复  更多评论
  
# re: 系统学习hibernate之一:利用hibernate中的SchemaExport生成数据表 2008-10-14 14:33 | kelly.zhang
可以使用annotation生成数据库的脚本,然后在通过数据库生成hbm,这样很快, 应该怎么做啊???  回复  更多评论
  
# re: 系统学习hibernate之一:利用hibernate中的SchemaExport生成数据表 2014-06-06 11:57 | 路飞
Configuration conf= new Configuration().addClass(User.class);
这一句有问题!  回复  更多评论
  
# re: 系统学习hibernate之一:利用hibernate中的SchemaExport生成数据表 2014-08-04 17:26 | fsdfa
fsdfafsf  回复  更多评论
  
# re: 系统学习hibernate之一:利用hibernate中的SchemaExport生成数据表[未登录] 2014-10-25 21:37 | a
Configuration conf= new Configuration().configure().
addClass(User.class);  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: