您的位置:首页 > 编程语言 > Java开发

spring hibernate 配置文件模板

2015-07-30 23:41 387 查看
<?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 classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
			 http://www.springframework.org/schema/context classpath:/org/springframework/context/config/spring-context-3.2.xsd
			 http://www.springframework.org/schema/aop classpath:/org/springframework/aop/config/spring-aop-3.2.xsd
			 http://www.springframework.org/schema/tx classpath:/org/springframework/transaction/config/spring-tx-3.2.xsd">

	<!-- 使用注解扫描所有的业务组件 com.hsj包下面的数据 -->
	<context:annotation-config />
	<!-- 扫描相关的bean对象 使用注解 @Service或者dao层的@Repository -->
	<context:component-scan base-package="com.hsj" />
    <!-- 连接池配置  -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<!-- 链接驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<!-- 最好加上utf8编码,避免数据库乱码 -->
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/forestry?useUnicode=true&characterEncoding=utf-8" />
		<property name="user" value="root" />
		<property name="password" value="root" />
		
		<!-- oracle数据库配置 -->
				<!-- 
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
		<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
		<property name="user" value="system" />
		<property name="password" value="root" />	
		-->
		<!--
		<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
		<property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;DatabaseName=forestry" />
		<property name="user" value="sa" />
		<property name="password" value="123456" />
		-->
		
		<property name="acquireIncrement" value="3" />
		<property name="idleConnectionTestPeriod" value="120" />
		<property name="initialPoolSize" value="3" />
		<property name="minPoolSize" value="3" />
		<property name="maxPoolSize" value="15" />
		<property name="numHelperThreads" value="3" />
		<property name="preferredTestQuery" value="select 1" />
	</bean>
		
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="hibernateProperties">
			<props>
			    <!-- 数据库方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<!-- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
				<!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> -->
				<!-- 打印sql语句控制台 -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 配置二级缓存 -->
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<!-- 配置ehcache缓存  -->
				<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
				<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
				<!-- 可以配置 update,create等等,create每次都会删除表在重新创建表接口,update只会更新已有的表结构,但是不会创建表结构 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="javax.persistence.validation.mode">none</prop>
			</props>
		</property>
		<!-- 注入数据源给hibernate  -->
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<!-- 使用注解配置hibernate 的关系映射,domain对象都放在com.hsj.model下面,其他包下面的实体不会扫描 使用注解@Entity-->
				<value>com.hsj.model</value>  
			</list>
		</property>
	</bean>

     <!-- 配置事务管理器,使用spring管理事务,一般放在service层管理事务  -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 使用注解配置事务,也可以使用表达式通配符 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
    <!-- 使用xml表达式直接配置事务,不只用注解 -->
    <!-- 
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="interceptorPointCuts"
            expression="execution(* com.hsj.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="interceptorPointCuts" />        
    </aop:config> 
     -->
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: