Hibernate之SchemaExport+配置文件生成表结构
首先要生成表,得先有实体类,以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表了
//ssh下配置
配置好person类和hbm.xml文件后,在spring配置文件中<sessionfactory>中加上
<prop key="hibernate.hbm2ddl.auto">
update
</prop>
Hibernate之SchemaExport+配置文件生成表结构的更多相关文章
- 菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构
今天说点基础的东西,说说怎样通过SchemaExport跟Hibernate的配置文件生成表结构.事实上方法很easy,仅仅须要两个配置文件,两个Java类就能够完毕. 首先要生成表,得先有实体类,以 ...
- 菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构
前几天向大家介绍了一种用工具类生成数据表的方法,只是之前的方法须要使用一个跟项目关系不大的工具类.不免让人认为有些多余,所以呢.今天再向大家介绍一种方法.即Hibernate与Spring配合生成表结 ...
- 使用schemaExport自动生成表结构
一.Hibernate原生状态 ? 1 2 3 4 5 Configuration cfg = new Configuration().configure(); SchemaExport expo ...
- Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...
- hibernate.hbm2ddl.auto=update不能自动生成表结构
在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...
- Activiti7 生成表结构
首先创建一个Maven项目 整体的项目结构 activiti.cfg.xml配置文件 <?xml version="1.0" encoding="UTF-8&quo ...
- EF:根据实体类生成表结构SQL
根据实体类生成表结构SQL: PM> Enable-Migrations -ProjectName Domain -StartUpProjectName Handler -Force PM> ...
- 生成表结构数据库文档sql语句
CREATE PROCEDURE [dbo].[生成表结构数据库文档]ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from ...
- django migrate生成表结构DateTimeField 类型加了6位精度别的框架无法调用的问题?
背景介绍 django migrate 生成表结构时,对于DateTimeField 类型的处理是加了6位精度的,只用django处理是没有任何问题的,但是如何别的框架来读取这种字段会读取不到该字段值 ...
随机推荐
- [转载]Access to the path '' is denied.解决方案
原文地址:Access to the path '' is denied.解决方案作者:趴着墙等红杏 ccess to the path '路径' is denied.我在网上找了很多资料,最后终于解 ...
- JS手动创建标签
代码: <html> <head> <title>js标签属性的添加</title> <script > function setxxx() ...
- Ajax无刷新提交表单和显示
ajax无刷新表单提交: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- centos7 安装php5.6.0 、nginx1.7.4、phpssdbadmin
1 创建用户.网站目录和下载相关的安装包 groupadd www #添加www组 创建目录/data/www/ chown www:www /data/www/ -R #设置目录所有者 chmod ...
- cas系列(三)--HTTP和HTTPS、SSL
(这段时间打算做单点登录,因此研究了一些cas资料并作为一个系列记录下来,一来可能会帮助一些人,二来对我自己所学知识也是一个巩固.) 本文转自異次元藍客点击打开链接 1. HTTPS HTTPS(全 ...
- 1 Winform 异步更新控件
刚才看到有人问为了winfrom中,在大数据绑定的时候出现画面假死的状态,为了解决这个问题希望通过再开一个线程来给控件绑定数据,可是画面还是会假死.现在看到的方法有1.掩耳盗铃法(Control.Ch ...
- 一个js加css加html完成的HTML
效果图: 代码: HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- linux服务端的网络编程
常见的Linux服务端的开发模型有多进程.多线程和IO复用,即select.poll和epoll三种方式,其中现在广泛使用的IO模型主要epoll,关于该模型的性能相较于select和poll要好不少 ...
- 找不好重现的bug的一个小技巧————守株待兔
最近碰到一个问题就是数据库中偶尔出现一条没有id的数据,可恨的是怎么也找不到重现这个问题的原因,只好换种方式来找了,那么就是我标题所说的守株待兔方法. 因为我发现出现bug的数据库里面的数据有个字段为 ...
- mysql5.5提示Deprecated: mysql_query(): The mysql extension is deprecated
解决方法1:在php程序代码里面设置报警级别 <?php error_reporting = E_ALL & ~E_DEPRECATED 方法2:禁止php报错 display_erro ...