看完 Spring Data JPA 基本篇 與 Spring Data JPA 進階篇 之後,再來看一下 Spring Data JPA 框架,它主要針對的就是 Spring 唯一沒有簡化到的業務邏輯代碼。接下來我們針對前面的例子進行改造,讓 Spring Data JPA 來幫助我們完成業務邏輯。在著手寫代碼之前,開發者需要先 下載Spring Data JPA 的發佈包(需要同時下載 Spring Data Commons 和 Spring Data JPA 兩個發佈包,Commons 是 Spring Data 的公共基礎包),並把相關的依賴 JAR 檔加入到 CLASSPATH 中。
Step 01: DAO Interface: PersonSpringDataJPADao.java
package com.mis.demos.dao; import org.springframework.data.repository.Repository; import com.mis.demos.model.Person; public interface PersonSpringDataJPADao extends Repository<Person, Integer> { public Person save(Person person); } |
expends Repository 時,需要宣告 public Person save(Person person); 作為儲存的方法。expends CrudRepository 或 JpaRepository 時,則不需宣告,如下:
package com.mis.demos.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.mis.demos.model.Person; @Repository("PersonSpringDataJPADao") public interface PersonSpringDataJPADao extends JpaRepository<Person, Integer> { } |
不要懷疑,就只是宣告一個 Interface 而已,而且是空的;至於加入 @Repository("PersonSpringDataJPADao") 只是讓 Control 層也能直接引用。
Step 02: Service: PersonSpringDataJPAService.java
package com.mis.demos.services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mis.demos.dao.PersonSpringDataJPADao; import com.mis.demos.model.Person; @Service("PersonSpringDataJPAService") public class PersonSpringDataJPAService { @Autowired PersonSpringDataJPADao personSpringDataJPADao; public Person saveAccount(Person person){ return personSpringDataJPADao.save(person); } public Person findPersonById(Integer id){ return personSpringDataJPADao.findOne(id); } } |
Step 03: spring-demo-cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 聲明Spring要管理的範圍 只要搜尋 service 一個 package 就可以了,model 交給 EntityManager 負責,dao 交給 Spring Data JPA 負責。--> <context:component-scan base-package="com.mis" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> </bean> <!-- Spring Data JPA 的設定 --> <jpa:repositories base-package="com.mis.demos.dao"/> </beans> |
Step 04: 新增測試
package com.mis.demos; import java.util.ArrayList; import java.util.List; 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.dao.PersonSpringDataJPADao; import com.mis.demos.model.Person; import com.mis.demos.services.PersonPU; import com.mis.demos.services.PersonService; import com.mis.demos.services.PersonSpringDataJPAService; import com.mis.demos.services.PersonSpringDataService; import com.mis.demos.services.PersonSpringDataServiceImpl; public class SimpleSpringJpaDemo { public static void main(String[] args) { doSpringDataJPASave(); } public static void doSpringDataJPASave(){ Person person = new Person(); Person who = new Person(); List<Person> allPerson= new ArrayList<Person>(); //person.setId(1); person.setFirstName("polin-99-3"); person.setLastName("wei-99-2"); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-demo-cfg.xml"); // 透過 Service PersonSpringDataJPAService personSpringDataJPAService = ctx.getBean("PersonSpringDataJPAService",PersonSpringDataJPAService.class); personSpringDataJPAService.saveAccount(person); who = personSpringDataJPAService.findPersonById(person.getId()); System.out.println(who.getFirstName()); //直接呼叫 DAO PersonSpringDataJPADao personSpringDataJPADao = ctx.getBean("PersonSpringDataJPADao",PersonSpringDataJPADao.class); //personSpringDataJPADao.delete(who.getId()); allPerson = personSpringDataJPADao.findAll(); for (Person person2 : allPerson) { System.out.println(person2.getId()); } ctx.close(); } } |
真的可以寫入耶,真是太棒了.
Step 05: 在 DAO: PersonSpringDataJPADao.java,使用 @Query 提供自訂查詢語句示例
package com.mis.demos.dao; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import com.mis.demos.model.Person; @Repository("PersonSpringDataJPADao") public interface PersonSpringDataJPADao extends JpaRepository<Person, Integer> { //使用 @Query 提供自訂查詢語句示例 @Query("select p from Person p where p.firstName = ?1") public List<Person> findByFirstName(String FirstName); } |
Step 06: 查詢測試
package com.mis.demos; import java.util.ArrayList; import java.util.List; 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.dao.PersonSpringDataJPADao; import com.mis.demos.model.Person; import com.mis.demos.services.PersonPU; import com.mis.demos.services.PersonService; import com.mis.demos.services.PersonSpringDataJPAService; import com.mis.demos.services.PersonSpringDataService; import com.mis.demos.services.PersonSpringDataServiceImpl; public class SimpleSpringJpaDemo { public static void main(String[] args) { doSpringDataJPASave(); } public static void doSpringDataJPASave(){ Person person = new Person(); Person who = new Person(); List<Person> allPerson= new ArrayList<Person>(); //person.setId(1); person.setFirstName("polin-99-3"); person.setLastName("wei-99-2"); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-demo-cfg.xml"); PersonSpringDataJPAService personSpringDataJPAService = ctx.getBean("PersonSpringDataJPAService",PersonSpringDataJPAService.class); personSpringDataJPAService.saveAccount(person); who = personSpringDataJPAService.findPersonById(person.getId()); System.out.println(who.getFirstName()); PersonSpringDataJPADao personSpringDataJPADao = ctx.getBean("PersonSpringDataJPADao",PersonSpringDataJPADao.class); //personSpringDataJPADao.delete(who.getId()); allPerson = personSpringDataJPADao.findAll(); for (Person person2 : allPerson) { System.out.println(person2.getId()); } allPerson = personSpringDataJPADao.findByFirstName("polin-99-3"); for (Person person2 : allPerson) { System.out.println(person2.getId()); } ctx.close(); } } |
本文主要介紹了 Spring Data JPA 的使用,以及它與 Spring 框架的無縫集成。Spring Data JPA 其實並不依賴於 Spring 框架。
參考:
http://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/
留言列表