IntelliJ IDEA配置 Hibernate连接Mysql8的hibernate.cfg.xml

2021/6/22 2:27:15

本文主要是介绍IntelliJ IDEA配置 Hibernate连接Mysql8的hibernate.cfg.xml,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

连接mysql8需要注意的是驱动driver和url以及dialect跟mysql5配置不同

1、driver

<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>

2、url

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=UTC</property>

这里要注意,xml文件中不能用&特殊符号,需要修改为 &amp;

3、dialect

<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>

贴上完整配置文件代码

<?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.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!-- 2、配置hibernate信息 -->
        <!--输出底层sql语句-->
        <property name="hibernate.show_sql">true</property>
        <!--输出底层sql语句格式-->
        <property name="hibernate.format_sql">true</property>
        <!--hibernate帮忙创建表配置
            update:如果已经有表,更新,否则创建
        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--配置数据库方言
            在mysql里面实现分页,关键字limit,只能使用在mysql里面
            在oracle数据库,实现分页rownum
            让hibernate框架识别不同的数据库语句
        -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <!-- 3、把映射文件放到核心配置文件中 -->
        <mapping resource="Entity/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


这篇关于IntelliJ IDEA配置 Hibernate连接Mysql8的hibernate.cfg.xml的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程