`
caspar_du
  • 浏览: 4597 次
  • 性别: Icon_minigender_1
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

Spring+JPA+druid+hibernate配置

阅读更多
<?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">


	<!-- 数据源配置 -->
	<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实体工厂配置 -->
	<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>

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

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

	<!-- 配置参与事务的类 -->
	<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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics