hibernate自动建表的例子

  使用hibernate自动建表时需要注意的是hibernate不会去创建数据库,所以数据库的创建要手动去完成。创建数据库后就可以通过实体类由hibernate自动去创建数据库表了。

hibernate配置文件hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>

        <!--指定数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!--在控制台显示执行的数据库操作语句 -->
        <property name="hibernate.show_sql">true</property>
        <!--在控制台显示执行的数据库操作语句(格式) -->
        <property name="hibernate.format_sql">true</property>
        <!--hibernate根据实体自动生成数据库表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 配置 CurrentSession-->
        <property name="hibernate.current_session_context_class">thread</property>

        <!--引入映射文件 -->
        <mapping resource="demo/entity/User.hbm.xml" />

    </session-factory>

</hibernate-configuration>

创建实体类 user.java

public class User {
    private int userId;
    private String username;
    private String password;
    private String nickname;
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

}

创建User类的映射文件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 package="demo.entity">

    <class name="User" table="user" lazy="true">
        <!-- 设置主键 -->
        <id name="userId" column="userId">
            <!-- id生成策略 -->
            <generator class="native"/>
        </id>
        <!-- unique="true"唯一约束 -->
        <property name="username" column="username" unique="true"/>
        <property name="password" column="password"/>
        <property name="nickname" column="nickname"/>
    </class>

</hibernate-mapping>

运行main方法

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {
    public static void main(String[] args) {
        // 读取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();
        // 建立SessionFactory
        SessionFactory factory = cfg.buildSessionFactory();
    }

}

运行结果
这里写图片描述
这里写图片描述

关于参数 hibernate.hbm2ddl.auto 参数的配置

定义
update 最常用的值,第一次加载hibernate时根据model类会自动建立起表的结构,以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了也不会删除以前数据表中的数据。
validate 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表。
create 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表。
create-drop 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除

猜你喜欢

转载自blog.csdn.net/qq_35224639/article/details/80299047