数据库的搭建
数据库为了方便搭建,使用docker镜像的方式进行搭建,一行命令就可以
docker run --name <container_name> -e MYSQL_ROOT_PASSWORD=<password> -p 3306:3306 -d mysql:8
搭建项目环境
ide使用IntelliJ Idea,按下图配置创建项目
implementation勾选Hibernate
创建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 xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="UserEntity.hbm.xml"/>
<mapping class="com.example.hiber.entity.UserEntity"/>
</session-factory>
</hibernate-configuration>
接下来在Idea的Database选择卡中,连接自己的数据库,然后去Persistence中找到Hibernate的选项
点击By Database Schema,按下图配置
这样就配置好啦🍺