hibernate自动建表

Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步。

一、通过Hibernate的ShemaExport来创建

1)实体类

package com.xiaomo.vo;
public class User {
private int id;// 用户id
private String name;// 用户名称
private int age;// 用户年龄
@Override
public String toString() {
return "id:"+this.id+"\tname:"+this.name+"\tage"+this.age;
}
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2)、User.hbm.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 用class元素来定义一个持久化类 -->
<class name="com.xiaomo.vo.User" table="user">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>

3)、hibernate.cfg.xml文件:

<!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在 Hibernate3.2.5软件包中的src\org\hibernate目录中找到此文件-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--声明Hibernate配置文件的开始-->
<hibernate-configuration>
<!-- 表明以下的配置是针对session-factory配置的,sessionFactory是hibernate中的一个类,这个类主要负责
保存hibernate的配置信息,以及对session的操作 -->
<session-factory>
<!-- 配置数据库的驱动程序,hibernate在连接数据库时,需要用到数据库的驱动程序 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 设置数据库的连接url:jdbc:mysql://localhost:3306/xiaomo,其中localhost表示说mysql的服务器名称,此处为本机。
port代表mysql服务器的端口号,默认为3306.xiaomo是数据库名,这是你要连接的数据库名 -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/lili
</property>
<!-- 如果你的mysql服务器都是默认设置的,且装在本机上则也可以写成
jdbc:mysql://localhost/xiaomo
或者是
jdbc:mysql:///test
-->
<!-- 连接数据库的用户名 -->
<property name="hibernate.connection.username">
root
</property>
<!-- 连接数据库的密码 -->
<property name="hibernate.connection.password">
cd_hisome
</property>
<!-- Hibernate使用的数据库方言,就是要用hibernate连接哪种类型的数据库服务器 -->
<property name="dialect">  
            org.hibernate.dialect.MySQLDialect  
        </property>  
<!-- hibernate.hbn2ddl.auto指定由java代码生成数据库脚本,进而生成具体的表结构的具体方式 -->
<property name="hbn2ddl.auto">update</property>
<!-- 是否在后台显示Hibernate生成的查询数据库的SQL语句,开发时设置为true,便于查询错误,运行时
可以在Eclipse的控制台显示Hibernate执行的sql语句。项目部署后可以设置为false,提高运行效率 -->
<property name="show_sql">true</property>
<!-- 指定映射文件为“com/xiaomo/vo/User.hbm.xml” -->
<mapping resource="com/xiaomo/vo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

4)、测试类:

package com.xiaomo.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {
public static void main(String[] args) {
//读取配置文件hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//创建SchemeExport实例
SchemaExport sExport = new SchemaExport(cfg);
//创建数据库表
sExport.create(true, true);
}
}

结果:

drop table if exists user

create table user (id integer not null auto_increment, name varchar(255), age integer, primary key (id))

hibernate自动创建表的优缺点:

一、优点:

1、自动创建新表

2、自动创建新字段

3、自动修改字段类型

二、缺点:

1、不会自动删除表

2、不会自动删除字段

3、自动创建的新字段只能是在最后。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值