今天说点基础的东西,说说怎样通过SchemaExport跟Hibernate的配置文件生成表结构。事实上方法很easy,仅仅须要两个配置文件,两个Java类就能够完毕。

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

/**
*
* @author Administrator
* @hibernate.class table="T_Person"
*/
public class Person { /**
* @hibernate.id
* generator-class="native"
*/
private int id; /**
* @hibernate.property
*/
private String name; /**
* @hibernate.property
*/
private String sex; /**
* @hibernate.property
*/
private String address; /**
* @hibernate.property
*/
private String duty; /**
* @hibernate.property
*/
private String phone; /**
* @hibernate.property
*/
private String description; /**
* @hibernate.many-to-one
*/
private Orgnization org; public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Orgnization getOrg() {
return org;
}
public void setOrg(Orgnization org) {
this.org = org;
}
}

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

<hibernate-mapping>
<class table="T_Person" name="com.tgb.model.Person">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="sex"/>
<property name="address"/>
<property name="duty"/>
<property name="phone"/>
<property name="description"/>
<many-to-one name="org"></many-to-one>
</class>
</hibernate-mapping>

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

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/tgb/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

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

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { /**
* @param args
*/
public static void main(String[] args) { // 默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure(); // 生成并输出sql到文件(当前文件夹)和数据库
SchemaExport export = new SchemaExport(cfg); // 创建表结构,第一个true 表示在控制台打印sql语句,第二个true 表示导入sql语句到数据库
export.create(true, true);
}
}

完毕以上步骤以后,仅仅须要运行ExportDB类就可以,当然前提是已经在mysql中创建了相应的数据库,我们这里创建了一个名为test的測试数据库。运行成功之后我们就能够看到数据库里已经有了我们的t_person表了,例如以下图所看到的:

OK,你会了吗,就是这么简单,假设之前没弄过,就来试试吧!

菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构的更多相关文章

  1. Hibernate之SchemaExport+配置文件生成表结构

    首先要生成表,得先有实体类,以Person.java为例: /** * * @author Administrator * @hibernate.class table="T_Person& ...

  2. 菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构

    前几天向大家介绍了一种用工具类生成数据表的方法,只是之前的方法须要使用一个跟项目关系不大的工具类.不免让人认为有些多余,所以呢.今天再向大家介绍一种方法.即Hibernate与Spring配合生成表结 ...

  3. SSH整合hibernate无法正常自动生成表

    检查持久化类的属性和映射文件是否正确配置,比如date格式的属性最容易配置错误

  4. 使用schemaExport自动生成表结构

    一.Hibernate原生状态 ? 1 2 3 4 5 Configuration cfg = new Configuration().configure();   SchemaExport expo ...

  5. Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...

  6. hibernate.hbm2ddl.auto=update不能自动生成表结构

    在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...

  7. 菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版

    前几天发了一篇Struts国际化的博客——<菜鸟学习SSH(二)——Struts2国际化手动切换版>,有网友提了一个意见,见下图: 于是就有了下面修改的版本: web.xml <?x ...

  8. hibernate如何配置自动生成表

    hibernate自动生成表有两种方法: 1.直接写代码,通过方法来创建数据库表. 2.通过 hibernate.cfg.xml配置标签来创建数据表. 下面依次实现: 1.直接写代码,通过方法来创建数 ...

  9. 使用hibernate利用实体类生成表和利用表生成实体类

    1,配置数据库,这里以oracle数据库为例.点击右侧Database图标:

随机推荐

  1. struts ModelDriven

    在表单提交的时候传值是这样,name=admin.username name=admin.password,然后在action中定义属性admin生成get和set 也可以实现ModelDriven这 ...

  2. php返回的json格式

    public function search_ip(){ $where['ip'] = $_GET['ip']; $Machine = M('Machine_info'); $arr = $Machi ...

  3. leetcode_question_64 Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  4. swift:打造你自己的折线图

    看到苹果Health里的折线图了吗.我们就是要打造一个这样的折线图.没看过的请看下图. 我们的主题在于折线图本身.其他的包括步数.日平均值等描述类的内容这里就不涉及了. 首先观察,这个图种包含些什么组 ...

  5. STL之vector详解

    一.vector容器的自增长 首先,我们知道vector容器是由数组做出来的:它具备了数组的优缺点. 数组的优点: 操作数据,读取速度很快,因为有下标: 数组的缺点: 分配之后不能在改变大小: #in ...

  6. Linux常用命令  新手必看

    文件和目录cd /home 进入 '/ home' 目录'cd .. 返回上一级目录cd ../.. 返回上两级目录cd 进入个人的主目录cd ~user1 进入个人的主目录cd - 返回上次所在的目 ...

  7. 初识Channel

    java.nio.channels 中的接口和类. A channel represents an open connection to an entity such as a hardware de ...

  8. C#实现时间戳转化

    /// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...

  9. 基于visual Studio2013解决算法导论之013基数排序

     题目 基数排序 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...

  10. VTK中国文字显示和简单的医疗图像浏览软件

    使用VTK做一个简单的医学图像浏览软件(在http://blog.csdn.net/www_doling_net/article/details/8668870这篇博文的基础上改的),支持标准的医学图 ...