本文的示例代碼基於 Hibernate EntityManager 開發,程式碼中使用到的都是 JPA 規範提供的Interface / Class,以便瞭解 JPA 基本的概念。整個系統架構如下
Step 01 : create mysql table: person
CREATE TABLE `person` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `firstName` varchar(45) NOT NULL, `lastName` varchar(45) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; |
Step 02: 利用 Hibernate Tool 自動建立 Entity model :Person.java
package com.mis.demos.model; // Generated Jan 20, 2016 10:35:00 AM by Hibernate Tools 4.3.1 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.Table; /** * Person generated by hbm2java */ @Entity @Table(name = "person", catalog = "spring") public class Person implements java.io.Serializable { private Integer id; private String firstName; private String lastName; public Person() { } public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "firstName", nullable = false, length = 45) public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Column(name = "lastName", nullable = false, length = 45) public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } |
Step 03: 直接用 Service 去存取 table ,不建立 DAO (Data Access Object),正常來說,是應該要先建立 DAO 的,我有點偷懶...:
package com.mis.demos.services; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import com.mis.demos.model.Person; /** * 沒有透過 DAO 操作 CRUD * @author polin.wei * */ public class PersonPU { private EntityManager em; private EntityManagerFactory emf; public void clientSave(Person person){ emf = Persistence.createEntityManagerFactory("mysqlLocalPU"); em = emf.createEntityManager(); if (person.getId()==null){ // new em.getTransaction().begin(); em.persist(person); em.getTransaction().commit(); } else { // update em.getTransaction().begin(); em.merge(person); em.getTransaction().commit(); } em.close(); emf.close(); } } |
Step 04: 建立 src\main\resources\META-INF\persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="mysqlLocalPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/spring" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="root" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="false" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence> |
Step 05: 測試
package com.mis.demos; import javax.annotation.Resource; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mis.demos.dao.PersonDao; import com.mis.demos.dao.PersonSpringDataDao; import com.mis.demos.model.Person; import com.mis.demos.services.PersonPU; import com.mis.demos.services.PersonService; import com.mis.demos.services.PersonSpringDataService; import com.mis.demos.services.PersonSpringDataServiceImpl; public class SimpleSpringJpaDemo { public static void main(String[] args) { doPersistenceSave(); } public static void doPersistenceSave(){ Person person = new Person(); // 利用 ID 是否有給值來作 save & update 的判斷 person.setId(2); person.setFirstName("polin-2-6"); person.setLastName("wei-2-6"); new PersonPU().clientSave(person); } } |
從 Spring + Hibernate 到 Spring Data JPA
http://cw1057.blogspot.tw/2013/12/more-in-spring-data-jpa-150-m1.html
https://struts.apache.org/docs/struts-2-spring-2-jpa-ajax.html#Struts2+Spring2+JPA+AJAX-Themavenway
https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-jpa/
文章標籤
全站熱搜
留言列表