hibernate 5.x 的schemaExport的用法

本文介绍从旧版Hibernate迁移到5.x版本的过程,包括持久类定义、映射文件配置、数据库连接设置及使用SchemaExport进行数据库表创建的方法,并解决迁移中遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习hibernate的书时版本还是3的,发现上面的schemaExport的写法放到eclipse里报错。查了资料才发现hibernate5.x已经改了。特记录一下

1.首先建立持久类

public class User {
    private int id;
    private String username;
    private String password;

    public User() {

    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

}

2.编辑映射文件

<?xml version='1.0' encoding='utf-8'?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: 
    GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the 
    lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 数据库驱动 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库路径 -->
        <property name="connection.url">
        jdbc:mysql://localhost:3306/mystudent?useSSL=false&amp;autoReconnect=true </property>
        <!-- 数据库用户名 -->
        <property name="connection.username">root</property>
        <!-- 数据库密码 -->
        <property name="connection.password">root</property>
        <!-- 数据库自身的语言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 是否显示数据库执行过程中的SQL语句 -->
        <property name="show_sql">true</property>
        <property name="current_session_context_class">thread</property>

        <!-- 加载映射文件 -->
        <mapping resource="User.hbm.xml" />
    </session-factory>

</hibernate-configuration>

3.User的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
<class name="entiy.User" table="User">
        <id name="id" >
            <generator class="native"/>
        </id>
        <property name="username" column="username"/>
        <property name="password" column="password"/>
    </class>
</hibernate-mapping>

4.schemaExport

Configuration config = new Configuration().configure();
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .configure().build();
        Metadata metadata = new MetadataSources(serviceRegistry)
                .buildMetadata();
        SchemaExport schemaExport = new SchemaExport();
    schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值