Spring ORM - 持久化 Hibernate
persistence.xml 定义与 Hibernate 相关的各个方面,如下所示。
持久化单元 − 包含所有详细信息的持久化单元。其名称用于在 Spring 上下文中获取引用。
提供程序 − org.hibernate.jpa.HibernatePersistenceProvider 类将用作提供程序。
数据库 URL 和凭据 − 在属性部分,我们传递与数据库相关的值。
show_sql − 显示生成的 sql 查询
hbm2ddl −允许 Hibernate 创建表。
在 src -> main -> resources -> META-INF 文件夹中创建 persistence.xml。
persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1"> <persistence-unit name="Hibernate_JPA"> <description> Spring Hibernate JPA Configuration Example</description> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tutorialspoint?useSSL=false" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="root@123" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence>