導GAV先:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.5.RELEASE</version> </dependency> <!-- hibernate start --> <!-- spring data jpa --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.5.0</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.0.Final</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- log日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
然后創建JPA的配置文件xml :
<?xml version="1.0" encoding="UTF-8"?> <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"> <!--配置持久化單元 name:持久化單元名稱 transaction-type:事務類型 RESOURCE_LOCAL:本地事務管理 JTA:分布式事務管理 --> <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="root" /> <!--jpa提供者的可選配置:我們的JPA規范的提供者為hibernate,所以jpa的核心配置中兼容hibernate的配 --> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence>
因為實際提供者是 hibernate 所以這里也配了一下 hibernate的配置。
這么文件名字一定要是 persistence.xml 而且 一定要放在 resource 下面的 META-INF 文件夾下 不然會找不到報錯!!!
第三步:創建對應的 Bena ,並且 把Bean中的字段映射到 數據庫表中:
package com.sze.jpa.bean; import javax.persistence.*; /** *@Entity 聲明實體類 * @Table 配置實體類和表的映射關系 name是映射表名字 */ @Entity @Table(name = "cst_customer") /** * @Id 主鍵 * @GeneratedValue(strategy = GenerationType.IDENTITY) 設置自增長 * @Column 映射表字段名 */ public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cust_id") private Long custId; //ID @Column(name = "cust_name") private String custName; //名字 @Column(name = "cust_source") private String custSource; //來源 @Column(name = "cust_industry") private String custIndustry; //所屬行業 @Column(name = "cust_level") private String custLevel; //級別 @Column(name = "cust_address") private String custAddress; //地址 @Column(name = "cust_phone") private String custPhone; //聯系方式 public Customer() { } public Customer(Long custId, String custName, String custSource, String custIndustry, String custLevel, String custAddress, String custPhone) { this.custId = custId; this.custName = custName; this.custSource = custSource; this.custIndustry = custIndustry; this.custLevel = custLevel; this.custAddress = custAddress; this.custPhone = custPhone; } public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }
【注解一定得是JPA的規范提供的注解一定要導入javax.persistence下的 包】里面的注解也有解釋,認真看看即可。下面是常用的注解:
@Entity
作用:指定當前類是實體類。
@Table
作用:指定實體類和表之間的對應關系。
屬性:
name:指定數據庫表的名稱
@Id
作用:指定當前字段是主鍵。
@GeneratedValue
作用:指定主鍵的生成方式。。
屬性:
strategy :指定主鍵生成策略。
@Column
作用:指定實體類屬性和數據庫表之間的對應關系
屬性:
name:指定數據庫表的列名稱。
unique:是否唯一
nullable:是否可以為空
inserttable:是否可以插入
updateable:是否可以更新
columnDefinition: 定義建表時創建此列的DDL
secondaryTable: 從表名。如果此列不建在主表上(默認建在主表),該屬性定義該列所在從表的名字搭建開發環境[重點]
下面我們做一個插入一條數據測試:
package com.sze.jpa; import com.sze.jpa.bean.Customer; import org.junit.Test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class TestJpa { @Test public void testSave(){ //實體管理類工廠,借助Persistence的靜態方法獲取 其中傳遞的參數為持久化單元名稱,需要jpa配置文件中指定 EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa"); //創建實體管理類 EntityManager entityManager = factory.createEntityManager(); //獲取事務對象 EntityTransaction transaction = entityManager.getTransaction(); //開啟事務 transaction.begin(); //創建一個對象 Customer customer = new Customer(); customer.setCustName("XianYu"); customer.setCustAddress("School"); //保存操作(插入) entityManager.persist(customer); //提交事務 transaction.commit(); //釋放資源 entityManager.close(); factory.close(); } }
這只是簡單使用JPA的實例
結果: