Hibernate5.3版本连接MySQL8的一系列问题解决

本文详细介绍了如何配置MySQL8.0及更高版本与Hibernate5.3的连接驱动,包括正确的URL设置、hibernate.cfg.xml文件配置、User.hbm.xml映射文件设置等关键步骤,并提供了在Hibernate5.3环境下创建SessionFactory的正确方法。

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

MySQL8.0以后,用的连接驱动改变了,driver必须加上.cj。如com.mysql.cj.jdbc.Driver。url为jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC。驱动更新为5.0版本的驱动不能使用。

Hibernate的hibernate.cfg.xml配置为

<!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="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <mapping resource="com/pojo/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

注意:&符号在Hibernate不可用,需写成&amp;使用MySQL8Dialect

User.hbm.xml配置为

<?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>
   <class name="com.pojo.User" table="hbm">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"></property>
        <property name="psw"></property>
    </class>

</hibernate-mapping>

在Hibernate5.3下,需要使用新的方法建立SessionFactory,4.3版本下的方法会报错

package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
//import org.hibernate.cfg.Configuration;

import com.pojo.User;

public class Test01 {
    public static void main(String[] args) {
//        Configuration cfg = new Configuration().configure();
//        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
//        SessionFactory sf = cfg.buildSessionFactory(sr);

        StandardServiceRegistry sr=new StandardServiceRegistryBuilder().configure().build(); 
        SessionFactory sf=new MetadataSources(sr).buildMetadata().buildSessionFactory();        
        
        Session session = sf.openSession();
        Transaction ts = session.beginTransaction();
        User user = new User();
        user.setName("王五");
        user.setPsw("333");
        session.save(user);
        ts.commit();
        session.close();
    }
}

注意:注释掉的为4.3版本的方法,不可用。导包切记不要导错。

此内容为本人多次查阅网上资料整理出来。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值