spring与jpa整合 简化persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库

===========appliction.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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-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/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    ">

 <!-- 自动扫描所有注解该路径 --> 
 <context:component-scan base-package="com.jubangit.smartbusiness.*" />
  <context:property-placeholder location="classpath:/META-INF/db.Properties" /><!--连接数据库的属性文件的路径-->

 <!-- 定义跳转的文件的前后缀 -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="suffix" value=".jsp" /> <!-- 指定跳转的页面为.jsp格式 -->
 </bean>
 
 <!-- JPA Entity Manager Factory -->
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"
  p:persistenceXmlLocation="classpath:/META-INF/persistence.xml" p:persistenceUnitName="jub"><!--p:persistenceXmlLocation="META-INF/persistence.xml" p:persistenceUnitName="jub",可以不加,应用服务器会扫描src/META-INF/persistence.xml-->
   <!-- <property name="packagesToScan" value="org.springside.examples.quickstart"/> --><!--Weblogic/Jboss这些自带JPA支持的应用服务器有时候会扫描persistence.xml,因此彻底删除掉这个文件需要加,否则会报错;但是删除persistence.xml,JPA找不到注解的实体类,所有还需要保留此文件-->
  <property name="jpaDialect">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
  </property>
  <property name="loadTimeWeaver">
   <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
  </property>
  <property name="persistenceProvider">
   <bean class="org.hibernate.ejb.HibernatePersistence"/><!-- 用于指定持久化实现厂商类 -->
  </property>
  
   <property name="jpaProperties">
             <props>
             <prop key="hibernate.dialect">${dialect}</prop>
       <prop key="hibernate.show_sql">true</prop>
                  <prop key="hibernate.max_fetch_depth">3</prop>
                  <prop key="hibernate.jdbc.fetch_size">18</prop>
                  <prop key="hibernate.jdbc.batch_size">10</prop>
     <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> --><!-- 定义是否自动生成表,create表示每次加载都重新生成,update表示每次加载只是更新表 -->
               </props>
           </property>
 </bean>
 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!--dbcp连接池-->
  <property name="driverClassName"><value>${driver}</value></property>
  <property name="url"><value>${url}</value></property>
  <property name="username"><value>${uname}</value></property>
  <property name="password"><value>${pwd}</value></property>
   <property name="initialSize"><value>5</value></property>
   <property name="maxActive"><value>50</value></property>
   <property name="maxIdle"><value>10</value></property>
   <property name="minIdle"><value>5</value></property>
    </bean>
   
    <!--事物-->
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>
 <!-- 事物通知 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="sav*" propagation="REQUIRED" rollback-for="Exception" /> <!-- rollback-for回滚事物,果存在一个事务,则支持当前事务。如果没有事务则开启 -->
   <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
   <tx:method name="updat*" propagation="REQUIRED" rollback-for="Exception" />
   <tx:method name="qry*" propagation="NOT_SUPPORTED" read-only="true"/>
   <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>  <!--必须要加propagation否则存在事物依然执行,NOT_SUPPORTED总是非事务地执行,并挂起任何存在的事务 -->
  </tx:attributes>
 </tx:advice>
 <!-- 事物切入 -->
 <aop:config>
  <aop:pointcut id="cut"
   expression="execution(* com.jubangit.smartbusiness.services.impl.*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="cut" />
 </aop:config>


</beans>

=================persistence.xml配置文件=================================

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd "
 version="2.0">
 <persistence-unit name="jub" transaction-type="RESOURCE_LOCAL"/>
</persistence>

你可能感兴趣的:(spring)