Spring Data Jpa配置

Spring Data JPA提供的接口,也是Spring Data JPA的核心概念:
1:Repository:最頂層的接口,是一個空的接口,目的是爲了統一全部Repository的類型,且能讓組件掃描的時候自動識別。
2:CrudRepository :是Repository的子接口,提供CRUD的功能
3:PagingAndSortingRepository:是CrudRepository的子接口,添加分頁和排序的功能
4:JpaRepository:是PagingAndSortingRepository的子接口,增長了一些實用的功能,好比:批量操做等。
5:JpaSpecificationExecutor:用來作負責查詢的接口
6:Specification:是Spring Data JPA提供的一個查詢規範,要作複雜的查詢,只需圍繞這個規範來設置查詢條件便可
 
n環境構建
在Eclipse裏面構建一個普通的Java工程,主要就是要加入一堆的jar包。
1:首先去官網下載Spring Data Common 和 Spring Data JPA的包,把裏面dist的jar包加入到工程中,這裏是spring-data-commons-1.5.0.RELEASE.jar和spring-data-jpa-1.3.2.RELEASE.jar
2:把Spring3.2.3的jar包添加到工程中
3:JPA的實現選用的是Hibernate4.2.0,總共還須要額外加入以下的jar:
antlr-2.7.7.jar、aopalliance-1.0.jar、asm-3.2.jar、aspectjrt-1.7.1.jar、aspectjweaver-1.7.1.jar、commons-beanutils-1.8.3.jar、commons-codec-1.7.jar、commons-collections-3.2.1.jar、commons-dbcp-1.4.jar、commons-fileupload-1.2.2.jar、commons-io-2.4.jar、commons-lang3-3.1.jar、commons-logging-1.1.1.jar、commons-pool-1.6.jar、dom4j-1.6.1.jar、hibernate-commons-annotations-4.0.1.Final.jar、hibernate-core-4.2.0.Final.jar、hibernate-entitymanager-4.2.0.Final.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、javassist-3.15.0-GA.jar、jboss-logging-3.1.0.GA.jar、jboss-transaction-api_1.1_spec-1.0.0.Final.jar、mysql-connector-java-5.1.9.jar、slf4j-api-1.7.3.jar
 
n實體對象,就是之前的實現方式
@Entity    //聲明實體
@Table(name="tbl_user")   //對應表
public class UserModel {
//定義主鍵
@Id   
private Integer uuid;
private String name;
private Integer age;
//省略getter/setter
}

  

nDAO的接口java

public interface UserRepository extends JpaRepository<UserModel, Integer>{
//空的,能夠什麼都不用寫
}
無需提供實現,Spring Data JPA會爲咱們搞定一切
 
n寫個邏輯層的Service,其實就至關於DAO的客戶端,用來測試
@Service
@Transactional
public class Client {
@Autowired
private UserRepository ur;
public void testAdd(UserModel um){ ur.save(um); }
 
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
Client c = (Client)ctx.getBean("client");
UserModel um = new UserModel();
um.setAge(1);
um.setName("張三");
um.setUuid(1);
 
c.testAdd(um);
} }

  

n一樣須要在Spring的配置文件中配置,基本跟使用註解的配置相似:
<?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
">

<!--定義服務層代碼包存放路徑,包下自動找@service註釋的類-->
<context:component-scan base-package="com.....">
<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"/>

<!--定義repository接口存放目錄-->
<!--定義接口實現的後綴,一般用Impl-->
<!--定義實體工廠的引用-->
<!--定義事務管理器的引用-->
    <jpa:repositories
            base-package="com......repository"
            repository-impl-postfix="Impl"
            entity-manager-factory-ref="entityManagerFactory"
            transaction-manager-ref="transactionManager">
    </jpa:repositories>   

<!--定義實體工廠bean-->
     <bean id="entityManagerFactory"          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="......" ref="....."/>
        <property name="......" value="......"/>
        <property name=".......">
            <bean class="........"/>
        </property>
    </bean>

<!--事務管理器配置-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
 
</beans>
 

  

配置完成後,能夠去運行Client測試一下了,固然數據庫和表須要先準備好
也能夠在<jpa:repositories>下面添加filter,形如:
<repositories base-package="com.acme.repositories">
<context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>
相關文章
相關標籤/搜索