Spring 框架對 JPA 提供的支持主要體如今以下幾個方面:html
首先,它使得 JPA 配置變得更加靈活。JPA 規範要求,配置文件必須命名爲 persistence.xml,並存在於類路徑下的 META-INF 目錄中。該文件一般包含了初始化 JPA 引擎所需的所有信息。Spring 提供的 LocalContainerEntityManagerFactoryBean 提供了很是靈活的配置,persistence.xml 中的信息均可以在此以屬性注入的方式提供。java
第三,也是最具意義的,Spring 將 EntityManager 的建立與銷燬、事務管理等代碼抽取出來,並由其統一管理,開發者不須要關心這些,業務方法中只剩下操做領域對象的代碼,事務管理和 EntityManager 建立、銷燬的代碼都再也不須要開發者關心了。web
<!-- spring-data-jpa start --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.7.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.5.1.RELEASE</version> </dependency> <!-- spring-data-jpa end -->
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- 註解組件掃描 --> <context:component-scan base-package="com.web.fwork"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 開啓註解事務只對當前配置文件有效 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <jpa:repositories base-package="com.web.fwork" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"></jpa:repositories> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.web.fwork.entity" /> <property name="persistenceProvider"> <bean class="org.hibernate.ejb.HibernatePersistence" /> </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="false" /> <!--指定數據庫類型--> <property name="database" value="SQL_SERVER" /> <!--<property name="databasePlatform" value="org.hibernate.dialect.SQLServer2012Dialect" />--> <property name="showSql" value="true" /> </bean> </property> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> </property> <property name="jpaPropertyMap"> <map> <entry key="hibernate.query.substitutions" value="true 1, false 0" /> <entry key="hibernate.default_batch_fetch_size" value="16" /> <entry key="hibernate.max_fetch_depth" value="2" /> <entry key="hibernate.generate_statistics" value="true" /> <entry key="hibernate.bytecode.use_reflection_optimizer" value="true" /> <entry key="hibernate.cache.use_second_level_cache" value="false" /> <entry key="hibernate.cache.use_query_cache" value="false" /> <entry key="hibernate.hbm2ddl.auto" value="update" /> </map> </property> </bean> <!--事務管理器配置 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 數據源 --> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> </property> <property name="url"> <value>jdbc:sqlserver://IP:1433;DatabaseName=db</value> </property> <property name="username" value="***" /> <property name="password" value="****" /> </bean> </beans>
2,dao層
使用 Spring Data JPA 進行持久層開發大體須要的三個步驟:spring
1.聲明持久層的接口,該接口繼承 Repository,Repository 是一個標記型接口,它不包含任何方法,固然若是有須要,Spring Data 也提供了若干 Repository 子接口,其中定義了一些經常使用的增刪改查,以及分頁相關的方法。sql
2.在接口中聲明須要的業務方法。Spring Data 將根據給定的策略來爲其生成實現代碼。數據庫
3.在 Spring 配置文件中增長一行聲明,讓 Spring 爲聲明的接口建立代理對象。配置了 <jpa:repositories> 後,Spring 初始化容器時將會掃描 base-package 指定的包目錄及其子目錄,爲繼承 Repository 或其子接口的接口建立代理對象,並將代理對象註冊爲 Spring Bean,業務層即可以經過 Spring 自動封裝的特性來直接使用該對象。express
什麼是Repository?
Repository(資源庫):經過用來訪問領域對象的一個相似集合的接口,在領域與數據映射層之間進行協調。這個叫法就相似於咱們一般所說的DAO,在這裏,咱們就按照這一習慣把數據訪問層叫Repository
Spring Data給咱們提供幾個Repository,基礎的Repository提供了最基本的數據訪問功能,其幾個子接口則擴展了一些功能。它們的繼承關係以下:
Repository: 僅僅是一個標識,代表任何繼承它的均爲倉庫接口類,方便Spring自動掃描識別
CrudRepository: 繼承Repository,實現了一組CRUD相關的方法
PagingAndSortingRepository: 繼承CrudRepository,實現了一組分頁排序相關的方法
JpaRepository: 繼承PagingAndSortingRepository,實現一組JPA規範相關的方法
JpaSpecificationExecutor: 比較特殊,不屬於Repository體系,實現一組JPA Criteria查詢相關的方法
咱們本身定義的XxxxRepository須要繼承JpaRepository,這樣咱們的XxxxRepository接口就具有了通用的數據訪問控制層的能力。 apache
參考:編程
package com.web.fwork.repository; import com.web.fwork.entity.BlogUserEntity; import com.web.fwork.entity.Student; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; /** * * @author liuhj * */ public interface UserRepository extends JpaRepository<BlogUserEntity, Integer> { /*使用原生sql語句查詢(自定義方法) */ @Query(value = "select * from Blog_User a where a.id = ?1",nativeQuery = true) public BlogUserEntity Test(Integer id); }
3 Service 層 增刪改查參考:服務器
package com.web.fwork.service; import com.web.fwork.entity.BlogArticleEntity; import com.web.fwork.entity.BlogUserEntity; import com.web.fwork.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by liuhj on 2015-11-02. */ @Service public class UserService2 { @Autowired private UserRepository userRepository; public void add(BlogUserEntity userEntity){ userRepository.save(userEntity); } public void query(Integer id){ userRepository.findOne(id); } public void delete(Integer id){ userRepository.delete(id); } public void update(BlogUserEntity userEntity) { userRepository.save(userEntity); } public List<BlogUserEntity> getList() { return userRepository.findAll(); } }
參考:
http://www.cnblogs.com/WangJinYang/p/4257383.html
http://perfy315.iteye.com/blog/1460226
官網:
http://projects.spring.io/spring-data-jpa/