Spring3+JAP2.0(hibernate3)+Spring MVC+Druid集成

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
	<persistence-unit name="customermanager">
    <!-- 实体bean集合名字  -->
    <!-- JPA驱动提供者  -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
		<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>	
		<property name="hibernate.hbm2ddl.auto" value="validate"/>
		<property name="hibernate.max_fetch_depth" value="3"/>
		<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>



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: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-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- 将一个业务逻辑实体交给Spring管理有两种方式:注解+扫描、 配置 -->
	<!-- 扫描以cn.yj开头的包,以及子包都会扫描的到 -->
	<context:component-scan base-package="com.zyios.customermanager" />
	
	<!-- 数据源<strong>配置</strong> -->  
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
        init-method="init" destroy-method="close">  
        <!-- 驱动名称 -->  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <!-- JDBC连接串 -->  
        <property name="url"  
            value="jdbc:mysql://localhost:3306/customermanager?useUnicode=true&amp;characterEncoding=UTF-8" />  
        <!-- 数据库用户名称 -->  
        <property name="username" value="root" />  
        <!-- 数据库密码 -->  
        <property name="password" value="123456" />  
        <!-- 连接池最大使用连接数量 -->  
        <property name="maxActive" value="20" />  
        <!-- 初始化大小 -->  
        <property name="initialSize" value="5" />  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="60000" />  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="2" />  
        <!-- 逐出连接的检测时间间隔 -->  
        <property name="timeBetweenEvictionRunsMillis" value="3000" />  
        <!-- 最小逐出时间 -->  
        <property name="minEvictableIdleTimeMillis" value="300000" />  
        <!-- 测试有效用的SQL Query -->  
        <property name="validationQuery" value="SELECT 'x'" />  
        <!-- 连接空闲时测试是否有效 -->  
        <property name="testWhileIdle" value="true" />  
        <!-- 获取连接时测试是否有效 -->  
        <property name="testOnBorrow" value="false" />  
        <!-- 归还连接时是否测试有效 -->  
        <property name="testOnReturn" value="false" />  
    </bean>  
  
  
    <!-- JPA实体工厂<strong>配置</strong> -->  
    <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="persistenceUnitName" value="customermanager" />
        <!-- 扫描实体路径
        <property name="packagesToScan" value="com.zyios.customermanager.model" />
		-->		
		 <!-- 运行时植入 
		<property name="loadTimeWeaver">	
			<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
		</property>
		-->
        <property name="jpaVendorAdapter">  
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
                <property name="showSql" value="true" />  
                <property name="generateDdl" value="false" />
                <property name="database" value="MYSQL" />
            </bean>  
        </property>
    </bean>  
	
	<!-- bean post-processor for JPA annotations
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
	 -->

	<!-- 事务管理器,Spring提供的使用JPA进行开发的事务管理器 -->
	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	
	
	<!-- 两种使用事务的配置方式:xml、注解 -->
	<tx:annotation-driven transaction-manager="txManager" />
</beans>

网上淘来的,网址:http://www.verydemo.com/demo_c146_i25712.html

<?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"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"

	xsi:schemaLocation="http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-3.1.xsd   
          http://www.springframework.org/schema/aop   
          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
          http://www.springframework.org/schema/tx    
          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
          http://www.springframework.org/schema/cache 
          http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
          http://www.springframework.org/schema/data/jpa
          http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">


	<!-- 数据源<strong>配置</strong> -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- 驱动名称 -->
		<property name="DriverClassName" value="com.mysql.jdbc.Driver" />
		<!-- JDBC连接串 -->
		<property name="url"
			value="jdbc:mysql://192.168.7.7:3306/tmc_db?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;transformedBitIsBoolean=true" />
		<!-- 数据库用户名称 -->
		<property name="username" value="root" />
		<!-- 数据库密码 -->
		<property name="password" value="root" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 初始化大小 -->
		<property name="initialSize" value="5" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="2" />
		<!-- 逐出连接的检测时间间隔 -->
		<property name="timeBetweenEvictionRunsMillis" value="3000" />
		<!-- 最小逐出时间 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<!-- 测试有效用的SQL Query -->
		<property name="validationQuery" value="SELECT 'x'" />
		<!-- 连接空闲时测试是否有效 -->
		<property name="testWhileIdle" value="true" />
		<!-- 获取连接时测试是否有效 -->
		<property name="testOnBorrow" value="false" />
		<!-- 归还连接时是否测试有效 -->
		<property name="testOnReturn" value="false" />
	</bean>


	<!-- JPA实体工厂<strong>配置</strong> -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 扫描实体路径 -->
		<property name="packagesToScan" value="com.spinfosec.dao.entity" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="generateDdl" value="false" />
			</bean>
		</property>
		<property name="jpaProperties">
			<props>
				<!--设置外连接抓取树的最大深度 -->
				<prop key="hibernate.max_fetch_depth">3</prop>
				<prop key="hibernate.jdbc.fetch_size">18</prop>
				<prop key="hibernate.jdbc.batch_size">10</prop>
				<!-- 自动建表类型 validate|create|create-drop|update -->
				<prop key="hibernate.hbm2ddl.auto">validate</prop>
				<!-- 是否显示SQL -->
				<prop key="hibernate.show_sql">false</prop>
				<!-- 显示SQL是否格式化 -->
				<prop key="hibernate.format_sql">false</prop>
				<!-- 关闭二级缓存 -->
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
				<!-- 关闭实体字段映射校验 -->
				<prop key="javax.persistence.validation.mode">none</prop>
			</props>
		</property>
	</bean>

	<!-- <strong>配置</strong>事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="dataSource" ref="dataSource" />
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<!-- <strong>配置</strong>事务传播特性 -->
	<tx:advice id="txAdvice">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<!-- <strong>配置</strong>参与事务的类 -->
	<aop:config>
		<aop:pointcut id="dao-tx" expression="execution(* *..dao.impl..*(..))" />
		<aop:pointcut id="service-tx" expression="execution(* *..service.impl..*(..))" />
		<aop:advisor pointcut-ref="dao-tx" advice-ref="txAdvice" />
		<aop:advisor pointcut-ref="service-tx" advice-ref="txAdvice" />
	</aop:config>

	<!-- 事务注解 -->
	<tx:annotation-driven />

</beans>




转载于:https://my.oschina.net/u/140714/blog/186759

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值