前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构

菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构

作者头像
全栈程序员站长
发布2022-07-13 16:05:01
2120
发布2022-07-13 16:05:01
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

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

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

代码语言:javascript
复制
/**
 * 
 * @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,配置例如以下:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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(十一)——Hibernate之SchemaExport+配置文件生成表结构
菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构

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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118353.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档