JPA 環境搭建
阿新 • • 發佈:2020-07-30
JPA 核心配置檔案
類路徑 META-INF/persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- persistence-unit(持久化單元) name:持久化單元名稱 transaction-type(事務型別): JTA:分散式事務 RESOURCE_LOCAL:本地事務 --> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL"> <!--JPA實現方式--> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <!--資料庫資訊--> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="743216wm"/> <!--JPA實現方(Hibernate)資訊--> <!--顯示 SQL--> <property name="hibernate.show_sql" value="true"/> <!--格式化 SQL--> <property name="hibernate.format_sql" value="true"/> <!-- hibernate.hbm2ddl.auto(自動建立資料庫表): create:如果有表,先刪除表再建立 update:如果有表,不會建立表;如果沒表,就建立表 none:不會建立表 --> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
JPA 相關依賴
以 Hibernate 作為 JPA 的實現
<dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- hibernate對jpa的支援包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${project.hibernate.version}</version> </dependency> <!-- c3p0 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${project.hibernate.version}</version> </dependency> <!-- log日誌 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- Mysql and MariaDB --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> </dependencies>