spring Data Jpa的依赖+配置

时间:2022-11-29 20:32:05

spring data jpa 是spring基于的orm框架,jpa规范的基础上封装的一套JPA应用框架

添加的相关依赖:

<properties>
        <spring.version>4.2.4.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>
        <!-- junit单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        
        <!-- spring beg -->
        <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>
        
        <!-- spring end -->

        <!-- hibernate beg -->
        <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>
        <!-- hibernate end -->

        <!-- c3p0 beg -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>
        <!-- c3p0 end -->

        <!-- log end -->
        <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>
        <!-- log end -->

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

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        
        <!-- el beg 使用spring data jpa 必须引入 -->
        <dependency>  
            <groupId>javax.el</groupId>  
            <artifactId>javax.el-api</artifactId>  
            <version>2.2.4</version>  
        </dependency>  
          
        <dependency>  
            <groupId>org.glassfish.web</groupId>  
            <artifactId>javax.el</artifactId>  
            <version>2.2.4</version>  
        </dependency> 
        <!-- el end -->
    </dependencies>

2.整合spring data jpa 与spring

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
11         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/data/jpa 
14         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
15     
16     <!-- 1.dataSource 配置数据库连接池-->
17     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
18         <property name="driverClass" value="com.mysql.jdbc.Driver" />
19         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jpa(数据库名)" />
20         <property name="user" value="root" />
21         <property name="password" value="111111"(数据库密码) />
22     </bean>
23     
24     <!-- 2.配置entityManagerFactory -->
25     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
26         <property name="dataSource" ref="dataSource" />
27         <property name="packagesToScan" value="cn.itcast.entity(包名" />
28         <property name="persistenceProvider">
29             <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
30         </property>
31         <!--JPA的供应商适配器-->
32         <property name="jpaVendorAdapter">
33             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
34                 <property name="generateDdl" value="false" />
35                 <property name="database" value="MYSQL" />
36                 <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
37                 <property name="showSql" value="true" />
38             </bean>
39         </property>
40         <property name="jpaDialect">
41             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
42         </property>
43     </bean>
44     
45     
46     <!-- 3.事务管理器-->
47     <!-- JPA事务管理器  -->
48     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
49         <property name="entityManagerFactory" ref="entityManagerFactory" />
50     </bean>
51     
52     <!-- 整合spring data jpa-->
53     <jpa:repositories base-package="cn.itcast.dao(自己所建的包"
54         transaction-manager-ref="transactionManager"
55         entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
56         
57     <!-- 4.txAdvice-->
58     <tx:advice id="txAdvice" transaction-manager="transactionManager">
59         <tx:attributes>
60             <tx:method name="save*" propagation="REQUIRED"/>
61             <tx:method name="insert*" propagation="REQUIRED"/>
62             <tx:method name="update*" propagation="REQUIRED"/>
63             <tx:method name="delete*" propagation="REQUIRED"/>
64             <tx:method name="get*" read-only="true"/>
65             <tx:method name="find*" read-only="true"/>
66             <tx:method name="*" propagation="REQUIRED"/>
67         </tx:attributes>
68     </tx:advice>
69     
70     <!-- 5.aop-->
71     <aop:config>
72         <aop:pointcut id="pointcut" expression="execution(* cn.itcast.service.*.*(..))" />
73         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
74     </aop:config>
75     
76     <context:component-scan base-package="cn.itcast(自己所写的包"></context:component-scan>
77         
78     <!--组装其它 配置文件-->
79     
80 </beans>

*因为所有的注解都是使用JPA规范提供的,所以在实体类导入注解包时使用javax.persistece下的