jpa +hibernate+spring +ehcache config

来源:互联网 发布:窑洞价值 知乎 编辑:程序博客网 时间:2024/06/06 04:03

1. pom.xml

 <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>1.6.0</version>
            <scope>runtime</scope>
     </dependency>

 

 <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.5.4</version>
      <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.4</version>
        <scope>runtime</scope>
    </dependency>

2.spring.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

   <aop:aspectj-autoproxy proxy-target-class="true"/>

   <bean id="ehy-persistentUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="dataSources">
            <map>
                <entry key="dataSource" value-ref="dataSource"/>
            </map>
        </property>
        <property name="defaultDataSource" ref="dataSource"/>
    </bean>

   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="persistenceUnitManager" ref="persistentUnitManager" />

      <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
      </property>

      <property name="jpaProperties" ref="jpaProperties" />
   </bean>

   <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

   <tx:annotation-driven transaction-manager="ehy-transactionManager"/>

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" lazy-init="true">
      <property name="entityManagerFactory" ref="ehy-entityManagerFactory"/>
   </bean>

   <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="doTransaction">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="doInquisition">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

    <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
        <property name="interceptorNames">
            <list>
                <idref local="transactionInterceptor"/>
            </list>
        </property>
        <property name="beanNames">
            <list>
                <idref bean="transactionExecutor"/>
            </list>
        </property>
    </bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
      <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
      <value>jdbc:mysql://localhost:3306/poker?useUnicode=true&amp;characterEncoding=utf8</value>
    </property>
    <property name="username">
      <value>service</value>
    </property>
    <property name="password">
      <value>password</value>
    </property>
    <property name="maxActive">
      <value>10</value>
    </property>
    <property name="maxIdle">
      <value>2</value>
    </property>
    <property name="maxWait">
      <value>12000</value>
    </property>
  </bean>

<bean id="jpaProperties" class="java.util.Properties">
        <constructor-arg>
            <props>       

                <!-- alternatively to <class> and <property> declarations, you can use a regular hibernate.cfg.xml file -->

                <prop key="hibernate.ejb.cfgfile">META-INF/hibernate.cfg.xml</prop>
            </props>
        </constructor-arg>
    </bean>

 

 

</beans>

 

3.persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
  <persistence-unit name="ehy-platform" transaction-type="RESOURCE_LOCAL">

    <provider>
        org.hibernate.ejb.HibernatePersistence
    </provider>

    <mapping-file>META-INF/hibernate.hbm.xml</mapping-file>
    <mapping-file>META-INF/orm.xml</mapping-file>

    <class>com.ehongyue.games.entity.Account</class>
   
    <exclude-unlisted-classes>true</exclude-unlisted-classes>

    <properties />
  </persistence-unit>

</persistence>

4.hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_structured_entries">true</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <property name="hibernate.cache.provider_configuration_file_resource_path">META-INF/ehcache.xml</property>
    <property name="hibernate.generate_statistics">true</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <class-cache class="com.games.entity.Account" include="all" usage="read-write"/>
   
    <!-- Mapping files -->
    <!--
               <mapping  resource="META-INF/hibernate.hbm.xml"/>
    -->
  </session-factory>
</hibernate-configuration>

5.ehcache.xml

?xml version="1.0" encoding="UTF-8"?>
<ehcache xsi:noNamespaceSchemaLocation="http://ehcache.sourceforge.net/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="500"
            maxElementsOnDisk="500"
            eternal="true"
            timeToIdleSeconds="60"
            timeToLiveSeconds="0"
            overflowToDisk="false"
            diskSpoolBufferSizeMB="30"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
    />

    <cache name="com.games.entity.Account"
           maxElementsInMemory="2500"
            />
 
</ehcache>

原创粉丝点击