JPA的配置文件名爲persistence.xml,位於classpath根目錄的META-INF目錄下,它的做用和Hibernate的配置文件hibernate.cfg.xml很類似。如下persistence.xml是本範例的JPA配置文件。java
/** persistence.xml */ <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="myunit"> <provider> org.hibernate.jpa.HibernatePersistenceProvider </provider> <class>mypack.Customer</class> <properties> <!-- 使用的數據庫方言 --> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" /> <!-- 數據庫驅動程序--> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <!--鏈接數據庫的URL--> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sampledb?useSSL=false" /> <!--鏈接數據庫的用戶名--> <property name="javax.persistence.jdbc.user" value="root" /> <!--鏈接數據庫的口令--> <property name="javax.persistence.jdbc.password" value="1234" /> <!-- 運行時在控制檯輸出Hibernate所執行的SQL語句 --> <property name="hibernate.show_sql" value="true" /> <!--在啓動時刪除並從新建立數據庫Schema --> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence>