zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    C#读写XML no
    ComboBox、ListBox绑定和获取数据 no
    win7 安装oracle 11g no
    2011没有惊天动地 no
    Oracle 删除重复数据只留一条 no
    Oracle 创建用户并分配权限 no
    Oracle 导出、导入某用户所有数据(包括表、视图、存储过程...) no
    谢谢,博客园这么快就审批通过了我的申请!
    Eclipse 中 JAVA AWT相关包不提示问题(解决)
    Eclipse java项目转Maven项目
  • 原文地址:https://www.cnblogs.com/yaoboyyao/p/3544006.html
Copyright © 2011-2022 走看看