jpa整合spring

首先引入该引入的包

最重要的就是beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <aop:aspectj-autoproxy/>
 <context:component-scan base-package="cn.itcast"/> 
    <!--  使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
    需要在tomcat作一些特殊配置.具体参考手册
    注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
   
   
   
     <context:property-placeholder location="classpath:jdbc.properties"/>
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${driverClass}"/>
  <property name="jdbcUrl" value="${jdbcUrl}"/>
  <property name="user" value="${user}"/>
  <property name="password" value="${password}"/>
  <property name="initialPoolSize" value="1"/>
  <property name="minPoolSize" value="1"/> 
  <property name="maxPoolSize" value="300"/>
  <property name="maxIdleTime" value="60"/> 
  <property name="acquireIncrement" value="5"/> 
  <property name="idleConnectionTestPeriod" value="60"/>
 </bean>
   -->
  
 
 <bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  
  
  <property name="driverClass" value="org.gjt.mm.mysql.Driver" />
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast" />
  <property name="user" value="root" />
  <property name="password" value="mysqladmin" />
 </bean>
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
  <property name="loadTimeWeaver">
           <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
     </property>
 </bean>
 
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
   </bean>
  
   <tx:annotation-driven transaction-manager="transactionManager"/>
  
   <bean id="compass" class="org.compass.spring.LocalCompassBean">
  <property name="classMappings">
   <list>
    <value>cn.itcast.bean.product.ProductInfo</value>
    <value>cn.itcast.bean.product.Brand</value>
    <value>cn.itcast.bean.product.ProductStyle</value>
    <value>cn.itcast.bean.product.ProductType</value>
   </list>
  </property>
  <property name="compassSettings">
   <props>
    <prop key="compass.engine.analyzer.default.type">net.paoding.analysis.analyzer.PaodingAnalyzer</prop>
    <prop key="compass.engine.connection">file://d:/index</prop>
    <!-- 在内存中建立索引
    <prop key="compass.engine.connection">ram://index</prop>
    -->
    <prop key="compass.engine.highlighter.default.formatter.simple.pre"><![CDATA[<font color='red'>]]></prop>
    <prop key="compass.engine.highlighter.default.formatter.simple.post"><![CDATA[</font>]]></prop>
    <prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
   </props>
  </property>
  <property name="transactionManager" ref="transactionManager" />
 </bean>

 <!-- 自动完成 索引的添加/更新/删除操作-->
 <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">
  <property name="compass" ref="compass" />
  <property name="gpsDevices">
   <list>
    <!-- 设置JPA驱动 -->
    <bean class="org.compass.gps.device.jpa.JpaGpsDevice">
      <property name="name" value="jpaDevice" />
      <property name="entityManagerFactory" ref="entityManagerFactory" />
      <property name="injectEntityLifecycleListener" value="true"/>
    </bean>
   </list>
  </property>
 </bean> 
</beans>

 

 

还有persistence.xml中的配置

<?xml version="1.0"?>
<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_1_0.xsd" version="1.0">
  <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.jdbc.fetch_size" value="18"/>
      <property name="hibernate.jdbc.batch_size" value="10"/>
      <property name="hibernate.show_sql" value="false"/>
      <property name="hibernate.format_sql" value="false"/>
      </properties>
  </persistence-unit>
</persistence>

posted on 2014-02-11 11:01  yaoboyyao  阅读(178)  评论(0编辑  收藏  举报

导航