1. 程式人生 > >介紹 複雜查詢,包括多表關聯,分頁,排序等

介紹 複雜查詢,包括多表關聯,分頁,排序等

本篇進行Spring-data-jpa的介紹,幾乎涵蓋該框架的所有方面,在日常的開發當中,基本上能滿足所有需求。這裡不講解JPA和Spring-data-jpa單獨使用,所有的內容都是在和Spring整合的環境中實現。如果需要了解該框架的入門,百度一下,很多入門的介紹。在這篇文章的接下來一篇,會有一個系列來講解mybatis,這個系列從mybatis的入門開始,到基本使用,和spring整合,和第三方外掛整合,快取,外掛,最後會持續到mybatis的架構,原始碼解釋,重點會介紹幾個重要的設計模式,這樣一個體系。基本上講完之後,mybatis在你面前就沒有了祕密,你能解決mybatis的幾乎所有問題,並且在開發過程中相當的方便,駕輕就熟。

這篇文章由於介紹的類容很全,因此很長,如果你需要,那麼可以耐心的看完,本人經歷了很長時間的學識,使用,研究的心血濃縮成為這麼短短的一篇部落格。

大致整理一個提綱:

  1、Spring-data-jpa的基本介紹;

  2、和Spring整合;

  3、基本的使用方式;

  4、複雜查詢,包括多表關聯,分頁,排序等;

現在開始:

  1、Spring-data-jpa的基本介紹:JPA誕生的緣由是為了整合第三方ORM框架,建立一種標準的方式,百度百科說是JDK為了實現ORM的天下歸一,目前也是在按照這個方向發展,但是還沒能完全實現。在ORM框架中,Hibernate是一支很大的部隊,使用很廣泛,也很方便,能力也很強,同時Hibernate也是和JPA整合的比較良好,我們可以認為JPA是標準,事實上也是,JPA幾乎都是介面,實現都是Hibernate在做,巨集觀上面看,在JPA的統一之下Hibernate很良好的執行。

  上面闡述了JPA和Hibernate的關係,那麼Spring-data-jpa又是個什麼東西呢?這地方需要稍微解釋一下,我們做Java開發的都知道Spring的強大,到目前為止,企業級應用Spring幾乎是無所不能,無所不在,已經是事實上的標準了,企業級應用不使用Spring的幾乎沒有,這樣說沒錯吧。而Spring整合第三方框架的能力又很強,他要做的不僅僅是個最早的IOC容器這麼簡單一回事,現在Spring涉及的方面太廣,主要是體現在和第三方工具的整合上。而在與第三方整合這方面,Spring做了持久化這一塊的工作,我個人的感覺是Spring希望把持久化這塊內容也拿下。於是就有了Spring-data-**這一系列包。包括,Spring-data-jpa,Spring-data-template,Spring-data-mongodb,Spring-data-redis,還有個民間產品,mybatis-spring,和前面類似,這是和mybatis整合的第三方包,這些都是乾的持久化工具乾的事兒。

  這裡介紹Spring-data-jpa,表示與jpa的整合。

  2、我們都知道,在使用持久化工具的時候,一般都有一個物件來操作資料庫,在原生的Hibernate中叫做Session,在JPA中叫做EntityManager,在MyBatis中叫做SqlSession,通過這個物件來操作資料庫。我們一般按照三層結構來看的話,Service層做業務邏輯處理,Dao層和資料庫打交道,在Dao中,就存在著上面的物件。那麼ORM框架本身提供的功能有什麼呢?答案是基本的CRUD,所有的基礎CRUD框架都提供,我們使用起來感覺很方便,很給力,業務邏輯層面的處理ORM是沒有提供的,如果使用原生的框架,業務邏輯程式碼我們一般會自定義,會自己去寫SQL語句,然後執行。在這個時候,Spring-data-jpa的威力就體現出來了,ORM提供的能力他都提供,ORM框架沒有提供的業務邏輯功能Spring-data-jpa也提供,全方位的解決使用者的需求。使用Spring-data-jpa進行開發的過程中,常用的功能,我們幾乎不需要寫一條sql語句,至少在我看來,企業級應用基本上可以不用寫任何一條sql,當然spring-data-jpa也提供自己寫sql的方式,這個就看個人怎麼選擇,都可以。我覺得都行。

  2.1與Spring整合我們從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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    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/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/context     
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/data/mongo
           http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- 資料庫連線 -->
    <context:property-placeholder location="classpath:your-config.properties" ignore-unresolvable="true" />
    <!-- service包 -->
    <context:component-scan base-package="your service package" />
    <!-- 使用cglib進行動態代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- 支援註解方式宣告式事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    <!-- dao -->
    <jpa:repositories base-package="your dao package" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" />
    <!-- 實體管理器 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="your entity package" />
        <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="MYSQL" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
                <!-- <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" />
            </map>
        </property>
    </bean>
    
    <!-- 事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    
    <!-- 資料來源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${userName}" />
        <property name="password" value="${password}" />
        <property name="initialSize" value="${druid.initialSize}" />
        <property name="maxActive" value="${druid.maxActive}" />
        <property name="maxIdle" value="${druid.maxIdle}" />
        <property name="minIdle" value="${druid.minIdle}" />
        <property name="maxWait" value="${druid.maxWait}" />
        <property name="removeAbandoned" value="${druid.removeAbandoned}" />
        <property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}" />
        <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
        <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="${druid.validationQuery}" />
        <property name="testWhileIdle" value="${druid.testWhileIdle}" />
        <property name="testOnBorrow" value="${druid.testOnBorrow}" />
        <property name="testOnReturn" value="${druid.testOnReturn}" />
        <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
        <property name="filters" value="${druid.filters}" />
    </bean>
    
    <!-- 事務 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="select*" read-only="true" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- 事務入口 -->
    <aop:config>
        <aop:pointcut id="allServiceMethod" expression="execution(* your service implements package.*.*(..))" />
        <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
    </aop:config>

</beans>
複製程式碼

  2.2對上面的配置檔案進行簡單的解釋,只對“實體管理器”和“dao”進行解釋,其他的配置在任何地方都差不太多。

    1.對“實體管理器”解釋:我們知道原生的jpa的配置資訊是必須放在META-INF目錄下面的,並且名字必須叫做persistence.xml,這個叫做persistence-unit,就叫做持久化單元,放在這下面我們感覺不方便,不好,於是Spring提供了

org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

這樣一個類,可以讓你的隨心所欲的起這個配置檔案的名字,也可以隨心所欲的修改這個檔案的位置,只需要在這裡指向這個位置就行。然而更加方便的做法是,直接把配置資訊就寫在這裡更好,於是就有了這實體管理器這個bean。使用

<property name="packagesToScan" value="your entity package" />

這個屬性來載入我們的entity。

  2.3 解釋“dao”這個bean。