Idea应用Hibernate-逆向工程生成映射
一、创建项目
1.1、在idea中创建一个maven项目,在pom.xml中添加hibernate和jbdc的依赖:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.2.Final</version>
</dependency>
</dependencies>
1.2、需要注意依赖的hibernate与平台jdk的版本支持关系:
二、创建配置文件
2.1、在resources目录下新建一个hibernate.cfg.xml文件,并添加配置内容:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
</session-factory>
</hibernate-configuration>
2.2、添加完配置后,idea会弹出检测到hibernate框架,并是否需要创建facet的信息,点击创建。
2.3、自动创建facets后,在项目中可以看到Hibernate框架
2.4、生成映射文件
1、点击View—>Tool Windows—>Persistence
2、打开工具栏
3、通过数据源生成映射
4、在项目第一次打开,还没有任何的数据源,点击创建相对应的数据连接:
5、生成实体类和映射文件