Spring整合JPA
程序员文章站
2022-06-13 09:02:07
...
<?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 MVC拦截器导致静态资源访问失败(基于java注解配置)
-
MAC将最近使用的应用程序整合到Stack放在一个文件夹里
-
手写spring时的一些工具类
-
spring boot踩坑记
-
spring boot从redis取缓存发生java.lang.ClassCastException异常
-
spring-boot-2.0.3不一样系列之源码篇 - run方法(三)之createApplicationContext,绝对有值得你看的地方
-
简单了解Spring Web相关模块运行原理
-
Spring Boot @Scheduled定时任务代码实例解析
-
基于Nacos实现Spring Cloud Gateway实现动态路由的方法
-
Spring 事务隔离与事务传播的详解与对比