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处理是没有任何问题的,但是如何别的框架来读取这种字段会读取不到该字段值 ...
随机推荐
- 给自己取了个英文名-Jamy Cai,哈哈~~
给自己取了个英文名:Jamy Cai, 同时开始启用新邮箱:Jamycai@outlook.com ~~
- Android 解决调用系统相册打不开图片 DecodeServices报解码错误
这是由于系统相册不知道你图片目录是一个相册.打开前需要向系统相册“注册一下”,说白了就是让系统相册知道你这个图片所在的文件夹是个相册. private static void scanImageFil ...
- [Mime] MediaTypes--电子邮件类型类 (转载)
点击下载 MediaTypes.rar 这个类是关于 电子邮件类型类的操作,在发送电子邮件是规定以什么样的格式发送,Xml,HTML,文本等方式1.电子邮件类型帮助类,Xml格式,HTML格式等看下面 ...
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
在使用Fragment的过程中,常常会遇到在Activity的onSaveInstanceState方法调用之后,操作commit或者popBackStack而导致的crash. 因为在onSaveI ...
- 停止Java线程,小心interrupt()方法
来源:http://blog.csdn.net/wxwzy738/article/details/8516253 程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决 ...
- LINUX nohup命令输入输出深浅进出
无论是否将 nohup命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中.如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中 ...
- CSS3实用方法小记 2016.03.16
圆角边框: border-radius : 4px; box阴影: box-shadow : 5px 5px 3px #000 ; /* 参数分别为向右拓展距离,向下拓展距离,阴影宽度,颜色*/ 背景 ...
- mysql建表出现Timestamp错误
mysql建表时如果有两个或以上的字段为Timestamp,那么可能会出现如下错误: Incorrect table definition; there can be only one TIMESTA ...
- 24种设计模式--建造者模式【Builder Pattern】
在一个周三,快要下班了,老大突然又拉住我,喜滋滋的告诉我“牛叉公司很满意我们做的模型,又签订了一个合同,把奔驰.宝马的车辆模型都交给我们公司制作了,不过这次又额外增加了一个新需求:汽车的启动.停止.喇 ...
- 【实习记】2014-08-18使用curl排错http头的content-length
总结一,用curl排错Content-Length设置错误,误导了客户端. 访问/cgi-bin/txproj_list时,firebug显示总是不多不少15秒,调试其他问题时郁闷. fire ...