`
ymgjava
  • 浏览: 22767 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring整合JPA

阅读更多
 
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!--组件扫描(扫描除了@Controller和@ControllerAdvice以外的其它的类)-->
    <context:component-scan base-package="com.test.shop">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <!--配置分散配置的属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置开发环境C3P0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
        <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
        <property name="maxStatements" value="${jdbc.maxStatements}"/>
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
        <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>
        <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"/>
        <property name="breakAfterAcquireFailure" value="${jdbc.breakAfterAcquireFailure}"/>
        <property name="testConnectionOnCheckout" value="${jdbc.testConnectionOnCheckout}"/>
    </bean>
    <!--JPA实体管理器工厂 Spring整合JPA的核心入口-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--指定持久化实现厂商类-->
        <property name="persistenceProvider">
            <bean class="org.hibernate.ejb.HibernatePersistence"/>
        </property>
        <!--设置JPA实现厂商的特定属性-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false"/>
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
        <!--指定一些高级特性-->
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
        <!--扫描的包-->
        <property name="packagesToScan" value="com.test.shop.domain"/>
        <!--JPA实现厂商类的特有属性-->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>
    <!-- 事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <!--事务通知-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="batch*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception"/>

            <tx:method name="load*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="list*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>

            <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--配置AspectJ AOP配置-->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod" expression="execution(* *..*Service.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>
</beans>
分享到:
评论

相关推荐

    简单的spring整合JPA

    这篇博客“简单的spring整合JPA”将向我们展示如何将这两个强大的工具结合在一起,以便更高效地进行数据库操作。 首先,我们需要理解Spring与JPA整合的基本概念。Spring框架提供了一个名为Spring Data JPA的模块,...

    spring整合jpa mysql

    Spring整合JPA与MySQL是现代Java企业级应用中常见的数据持久化方案,它结合了Spring框架的灵活性和Java Persistence API(JPA)的强大功能,同时利用MySQL作为关系型数据库,提供高效、便捷的数据存储和查询能力。...

    spring整合JPA

    **Spring 整合 JPA 知识点详解** Spring 框架是 Java 开发中最常用的轻量级框架之一,它提供了丰富的功能,包括依赖注入、面向切面编程以及多种数据访问集成。JPA(Java Persistence API)是 Java 平台上的一个标准...

    Spring集成JPA和MyBatis简单例子-20170622

    在IT行业中,Spring框架...在"Spring集成JPA和MyBatis简单例子-20170622"这个压缩包中,可能包含了示例代码,演示了如何在Spring项目中配置和使用这两种技术,这对于初学者理解和实践Spring的集成能力具有很大的帮助。

    spring整合jpa简单实例

    在Spring整合JPA的过程中,首先需要在项目中添加相关的依赖。如果使用Maven,我们需要在pom.xml文件中加入Spring、Spring Data JPA和Hibernate的相关依赖。例如: ```xml &lt;!-- Spring framework --&gt; &lt;groupId&gt;...

    Spring Data JPA中文文档[1.4.3]_springdatajpa_erlang_waitxpf_

    6. **Integration with Spring MVC**:Spring Data JPA 可以无缝集成到 Spring MVC 应用中,方便在 Web 层进行数据操作。通过 ModelMapper 和 Controller 方法,可以直接将 Repository 中的数据转换为视图模型并返回...

    Spring Data JPA 笔记

    5. **事务管理**:Spring Data JPA集成Spring的事务管理,可以方便地在方法级别控制事务的开始、提交、回滚,确保数据一致性。 6. ** auditing**:Spring Data JPA还提供了审计功能,通过`@CreationTimestamp`和`@...

    Spring集成Jpa(Hibernate实现)

    Spring集成JPA(Java Persistence API)是将Spring框架与ORM(Object-Relational Mapping)解决方案之一的Hibernate结合使用的常见实践。这个例子展示了如何在Spring应用中配置和使用JPA,以便利用Hibernate作为JPA...

    Spring Data JPA的优点和难点.pdf

    Spring Data JPA的主要优点在于其高度的开发效率、成熟的语法结构以及与Spring框架的紧密集成。 1. **开发效率极高**: - Spring Data JPA通过提供自动化的 Repository 实现,减少了大量手动编写SQL和DAO层代码的...

    Spring Boot整合SpringDataJPA

    本教程将深入探讨如何在Spring Boot项目中整合Spring Data JPA,实现高效且简洁的数据持久化。 首先,Spring Boot整合Spring Data JPA的基础是引入相关的依赖。在`pom.xml`或`build.gradle`文件中,我们需要添加...

    Struts2整合Spring、JPA

    Struts2整合Spring和JPA是企业级Java应用开发中常见的技术组合,它们分别负责不同的职责:Struts2作为一款成熟的MVC框架,主要用于处理Web层的请求与响应;Spring作为一个全面的轻量级框架,提供了依赖注入(DI)和...

    SPRING整合jpa

    Spring整合JPA(Java Persistence API)是现代Java开发中常用的一种技术栈,它允许开发者以声明式的方式管理数据库操作,极大地简化了数据访问层的代码。Spring Data JPA是Spring框架的一部分,它提供了对JPA的高级...

    Spring Data JPA从入门到精通

    Spring Data JPA的另一个强大特性是其与Spring Data Repositories的集成,它允许我们自定义复杂的查询,甚至支持分页和排序。通过定义接口方法,Spring Data会自动为我们生成对应的查询方法。 最后,Spring Data ...

    手动创建 SpringMvc +SpringDataJpa+Hibernate+ freemarker mavenProject+ 环境切换 webDemo

    在本项目中,我们主要探讨如何手动构建一个基于SpringMVC、Spring Data JPA、Hibernate以及FreeMarker模板引擎的Maven工程,同时实现环境切换功能。这个基础框架为日常开发工作提供了必要的支持。 首先,SpringMVC...

    spring整合jpa

    Spring整合JPA(Java Persistence API)是现代Java应用程序中常见的数据访问技术,它提供了ORM(对象关系映射)的功能,使得开发者可以使用面向对象的方式来操作数据库,而无需关心底层的SQL语句。本整合教程将深入...

    SpringMVC+Spring+SpringDataJPA+Hibernate整合登录的效果

    这是整合SpringMVC+Spring+SpringDataJPA+Hibernate简单的实现登录的功能,用的是mysql数据库,这是一个web Project 如果你用的是JavaEE6那么你要注意bean-validator.jar和weld-osgi-bundle.jar与slf4j的jar包冲突。...

    springBoot整合springData JPA

    - 整合测试:使用Spring Boot的Test Slice功能,我们可以方便地编写针对特定组件的单元测试,比如Repository的集成测试。 通过学习和实践这个项目,开发者可以在短短三小时内快速掌握SpringBoot整合SpringData JPA...

    Spring集成JPA和MyBatis简单例子

    本示例将详细讲解如何在Spring环境中集成JPA与MyBatis,以实现灵活的数据访问。 首先,让我们深入了解这两个框架。JPA是Java标准的ORM(Object-Relational Mapping)框架,它允许开发者用面向对象的方式处理数据库...

    Spring Data JPA.zip

    - **集成其他 Spring 功能**:与其他 Spring 模块无缝集成,如 Spring Security、Spring Batch 等。 **3. Spring Data JPA 的基本配置** 在项目中引入 Spring Data JPA 和相应的 JPA 实现(如 Hibernate),配置...

Global site tag (gtag.js) - Google Analytics