`
周英能
  • 浏览: 185907 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

	<context:component-scan base-package="com.zyn"></context:component-scan>
	<!-- 定义受环境影响易变的变量 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<!-- 本地开发环境配置 -->
				<value>classpath*:/application.local.properties</value>

				<!-- 集群中节点配置 -->
				<value>classpath*:/application.cluster.properties</value>

				<!-- 标准配置 -->
				<value>classpath*:/application.properties</value>

				<!-- 服务器生产环境配置 -->
				<!-- <value>file:/var/myapp/application.server.properties</value> -->
			</list>
		</property>
	</bean>

	<!-- c3p0 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
		<property name="driverClassName" value="${jdbc.driver}"> </property> <property 
		name="url" value="${jdbc.url}"> </property> <property name="username" value="${jdbc.username}"> 
		</property> <property name="password" value="${jdbc.password}"> </property> 
		</bean> -->

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />

		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="current_session_context_class">thread</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.auto}</prop>
				<prop key="hibernate.connection.provider_class">{hibernate.connection.provider_class}</prop>
				<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
				<prop key="hibernate.search.default.indexBase">F:/temp/index</prop>
			</props>
		</property>

		<property name="annotatedClasses">
			<list>
				<value>com.zyn.ssh.pojo.Student</value>
			</list>
		</property>
	</bean>
	<!-- 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />
</beans>
 application.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/zyn?useUnicode=true&amp;characterEncoding=utf-8
hibernate.dialect=org.hibernate.dialect.MySQLDialect
jdbc.username=root
jdbc.password=admin
pool.initialSize=20
pool.maxActive=40
pool.maxIdle=5
pool.maxWait=600
pool.poolPreparedStatements=false
pool.defaultAutoCommit=false
pool.timeBetweenEvictionRunsMillis=3600000
pool.minEvictableIdleTimeMillis=3600000
#hibernate c3p0 settings
c3p0.acquireIncrement=1 
c3p0.maxIdleTime=60 
c3p0.maxPoolSize=10 
c3p0.minPoolSize=2 
c3p0.initialPoolSize=5 
#hibernate settings
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.auto=update
hibernate.ehcache_config_file=ehcache.xml
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.jdbc.batch_size=20
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics