1. 程式人生 > 實用技巧 >spring data jpa的配置檔案及相關依賴

spring data jpa的配置檔案及相關依賴

配置檔案:applicationContext.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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"
> <!-- 配置讀取properties檔案的工具類 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0資料庫連線池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"/> <
property name="driverClass" value="${jdbc.driver.class}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- spring整合jpa配置EntityManagerFactory 建立EntityManagerFactory物件交給spring容器 --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--掃描實體類所在包--> <property name="packagesToScan" value="com.xj.domain"></property> <!--jpa的供應商介面卡--> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <!-- 是否自動建立資料庫表 --> <property name="generateDdl" value="false"></property> <!-- 宣告資料庫 --> <property name="database" value="MYSQL"></property> <!--配置資料庫方言--> <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/> <!--控制檯輸出sql--> <property name="showSql" value="true"></property> </bean> </property> <!--jpa方言:hibernate高階的特性:一級快取、二級快取等--> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"> </bean> </property> </bean> <!--整合spring data jpa--> <!-- Spring Data JPA 的配置 --> <!-- 加入名稱空間jpa --> <!-- base-package:掃描dao介面所在的包 --> <jpa:repositories base-package="com.xj.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 配置開啟註解事務處理 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!--宣告式事務--> <!-- 配置事務切面 --> <aop:config> <!-- 考慮到後面我們整合SpringSecurity,避免把UserDetailsService加入事務控制,讓切入點表示式定位到ServiceImpl --> <aop:pointcut id="txPointCut" expression="execution(* *..*ServiceImpl.*(..))"/> <!-- 將切入點表示式和事務通知關聯起來 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> <!-- 配置事務通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 配置事務屬性 --> <tx:attributes> <!-- 查詢方法:配置只讀屬性,讓資料庫知道這是一個查詢操作,能夠進行一定優化 --> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="count*" read-only="true"/> <!-- 增刪改方法:配置事務傳播行為、回滾異常 --> <!-- propagation屬性: REQUIRED:預設值,表示當前方法必須工作在事務中,如果當前執行緒上沒有已經開啟的事務,則自己開新事務。如果已經有了,那麼就使用這個已有的事務。 顧慮:用別人的事務有可能“被”回滾。 REQUIRES_NEW:建議使用的值,表示不管當前執行緒上有沒有事務,都要自己開事務,在自己的事務中執行。 好處:不會受到其他事務回滾的影響。 --> <!-- rollback-for屬性:配置事務方法針對什麼樣的異常回滾 預設:執行時異常回滾 建議:編譯時異常和執行時異常都回滾 --> <tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/> <tx:method name="remove*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/> <tx:method name="batch*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <!-- 配置springIOC的註解掃描 --> <context:component-scan base-package="com.xj"/> </beans>

相關依賴:

<properties>
        <spring.version>5.0.2.RELEASE</spring.version>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <c3p0.version>0.9.1.2</c3p0.version>
        <mysql.version>5.1.6</mysql.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.1.Final</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.0.RELEASE</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <!--<scope>test</scope>-->
        </dependency>


        <!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.web/javax.el -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.4</version>
        </dependency>
    </dependencies>