IDEA 2020.3 configuration creates Hibernate

create project

Create a new web project, pay attention to check the corresponding Hibernate library file.

image-20210520223751867

image-20210520223919430

Add MySQL database driver

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>

pom.xml adds the above dependencies. Then refresh the Maven project to get the database driver.

image-20210520224218214

Connect to the database

image-20210520225158849

Add the Hibernate framework

(img-6sUPE7m6-1621522783726)

Check the following content and use the library to select the previously selected Hibernate library dependency

image-20210520224924965

When selecting next, pay attention to the fact that the following two cannot be used at the same time when there is a composite primary key

image-20210520222937265

Modify the configuration file

Modify the generated hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/library2?serverTimezone=GMT%2b8&amp;characterEncoding=utf-8</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping class="com.example.demo2.BookEntity"/>
        <mapping class="com.example.demo2.BorrowEntity"/>
        <mapping class="com.example.demo2.UserEntity"/>

    </session-factory>
</hibernate-configuration>

Run auto-generated test functions

image-20210520225733582

Test success

image-20210520225639030

Guess you like

Origin blog.csdn.net/horizon08/article/details/117093528