create project
Create a new web project, pay attention to check the corresponding Hibernate library file.
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.
Connect to the database
Add the Hibernate framework
Check the following content and use the library to select the previously selected Hibernate library dependency
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
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&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
Test success