`

spring +jpa(转载)

    博客分类:
  • java
阅读更多
spring整合jpa
dev.firnow.com    时间 : 2010-07-19  作者:网络   编辑:huyang629 点击:  247 [ 评论 ]
-
-
spring配置文件:
<context:component-scan base-package="com.sincere" />

<!--
载入.properties属性文件
classpath表示类路径,在WEB应用中就是clases下,
只有单个.properties属性文件时,使用这种方式
<context:property-placeholder location="classpath:jdbc.properties" />
-->

<!-- 指定属性文件地址,可以在这里定义多个属性文件 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">  
       <list>  
           
          <value>classpath:smtp.properties</value>
          <value>classpath:jdbc.properties</value>   
       </list>  
    </property>  
    <property name="fileEncoding" value="utf-8"/>  
</bean>
-->

<!--  使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
需要在tomcat作一些特殊配置.具体参考手册
注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
-->
<!--  只有单个.properties属性文件时,才能使用使用这种方式-->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 数据源信息 ,集成DBCP连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="maxIdle" value="${maxIdle}" />
<property name="minIdle" value="${minIdle}" />
</bean>
<!--
数据源信息 ,集成proxool连接池
参数:1、driverUrl    用户名及密码写在 url 中
2、user和password    用户名及密码写在 url 中,在以下属性中用户名及密码可不写,但该属性必须存在。
3、prototypeCount   一旦发现空闲的连接数没达到该值将建链
4、simultaneousBuildThrottle    一次建链的数量,生产环境可设置为5
5、maximumConnectionCount   生产环境应把该值设置为50
6、minimumConnectionCount   生产环境应把该值设置为10
7、maximumActiveTime    每连接每次使用的最大时长(毫秒)
8、houseKeepingSleepTime   proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁
9、overloadWithoutRefusalLifetime   满负载时,等待的时长
10、trace   是否启用日志跟综

<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
    <property name="driver" value="com.mysql.jdbc.Driver" />       
    <property name="driverUrl" value="jdbc:mysql://localhost:3306/priceangelsmmdb?user=yourname&amp;password=yourpass&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="user" value="root" />
    <property name="password" value="mysql" />
  <property name="prototypeCount" value="5" />  
    <property name="simultaneousBuildThrottle" value="5" />
<property name="maximumConnectionCount" value="10000" />
  <property name="minimumConnectionCount" value="10" />
  <property name="maximumActiveTime" value="300000" />
  <property name="houseKeepingSleepTime" value="90000" />
  <property name="houseKeepingTestSql" value="select 1 from dual" />
  <property name="overloadWithoutRefusalLifetime" value="30000" />
<property name="trace" value="true" />  
</bean>-->








<!-- ============================================spring与JPA集成配置开始=================================================================== -->
<!-- Spring与JPA的集成,也就只要配制entityManagerFactory和transactionManager两个Bena,就可以了 -->
<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>
<!--  没有使用数据源时Spring集成JPA,参数"persistenceUnitName"的值必需与persistence.xml文件里的<persistence-unit name="jpatest" transaction-type="RESOURCE_LOCAL">,name的值相同
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="itcast"/>
</bean>
-->
<!-- 指定所使用的事务管理器,来管理数据库事务,这里指定了使用的是JPA事务管理器(也还有别的事务管理器,如果Hibernate事务管理器) -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- ============================================spring与JPA集成配置结束=================================================================== -->


<!--============================================================================================================
Make all @Transactional beans
将所有具有@Transactional注解的Bean 自动配置为声明式事务支持
<tx:annotation-driven ..../>这一行配置用于开启声明式事务功能的,如果说只注释@Transactional,也是没有事务支持的.
事务边界被限制在每个具有事务功能的对象方法中,在方法调用时开始事务,在方法返回时提交事务.

@Transactional 定义在类或接口中,此时事务边界被限制在对象中的每个方法前后
============================================================================================================-->
<!-- Activates @Transactional for DefaultImageDatabase -->
<tx:annotation-driven transaction-manager="transactionManager" />



jpa配置文件persistence.xml

<persistence-unit name="jpatest" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
       
         <!--
            使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
            需要在tomcat作一些特殊配置.具体参考手册
            注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
        
      <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value="root"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpatest?useUnicode=true&amp;characterEncoding=UTF-8"/>
         -->
       
         <property name="hibernate.max_fetch_depth" value="3"/>
        
         <!--         
           说明:这里所指的创建和更新都是要数据库已经存在的情况下,即它只是去创建和更新表结构,而不会创建数据库
          
            validate           加载hibernate时,验证创建数据库表结构
create             每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop        加载hibernate时创建,退出是删除表结构
update             加载hibernate自动更新数据库结构(更新过程中,删除JavaBean属性时时不会把数据库的字段也一同删除,当添加一个JavaBean时属性时总是会把数据库表中这个的这个字段设计成可以为空)

总结:
1.请慎重使用此参数,没必要就不要随便用。
2.如果发现数据库表丢失,请检查hibernate.hbm2ddl.auto的配置 可设置 <property name="hibernate.hbm2ddl.auto" value="none" />
          -->
         <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="true"/>
     <property name="hibernate.format_sql" value="true"/>
      </properties>
  </persistence-unit>
</persistence>

原文出处:http://dev.firnow.com/course/3_program/java/javajs/20100719/459273.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics