定义:
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步。
如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码
Xml代码
<property name="hbm2ddl.auto">update</property>
解释如下:
hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop
update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表;如果表里缺少列,则自动添加列。
还有其他的参数:
其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。
如果没有此方面的需求建议set value=“none”.
其它几个参数的意思:
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
如果发现数据库表丢失或新增,请检查hibernate.hbm2ddl.auto的配置 可设置
建议在开发环境下使用,在生产环境下去掉。
PS:数据库要预先建立好,因为hibernate只会建表,不会建库
代码演示:
1、User.java
package com.hibernate;
import java.util.Date;
public class User {
//用户id
private String id;
//用户名称
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、User.hbm.xml
它指定数据库表(比如t_User)和映射类(User.java)之间的关系,包括映射类和数据库表的对应关系、表字段和类属性类型的对应关系以及表字段和类属性名称的对应关系等。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<!-- 数据库默认表的名称和类名相同,
如果需要改变或类名为关键字时,
可以添加table="t_user"给表重命名 -->
<class name="com.hibernate.User" ><!-- table="t_user" -->
<!-- 映射主键 -->
<id name="id">
<!-- 主键生成策略,利用生成器 -->
<generator class="uuid"></generator>
</id>
<!-- 利用property映射其他字段 -->
<property name="name" ></property><!-- 若加上column="user_name",数据库字段名为user_name -->
</class>
</hibernate-mapping>
3、hibernate.hbm.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 驱动类 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 连接的url,数据库名称为hibernate_first -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- 适配器,方言,用于翻译成mysql的语句 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 设置打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto" value="create" />
<!--<property name=”hibernate.hbm2ddl.auto” value=”update”/>-->
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 映射文件加入 -->
<mapping resource="com/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、测试类
public static void main(String[] args){
//读取的是properties文件
//Configuration cfg=new Configuration();
Configuration cfg=new Configuration().configure();
//工具类
SchemaExport export=new SchemaExport(cfg);
//打到控制台,输出到数据库
export.create(true, true);
}
5、在hibernate.hbm.xml加入
<properties>
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
6、控制台输出结果,数据库表建好了
drop table if exists User
create table User (
id varchar(255) not null,
name varchar(255),
primary key (id)
)