hibernate指定jdbc配置文件

hibernatge如何指定jdbc配置文件呢?

jdbc配置文件型如:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://182.92.94.71:3306/test
jdbc.username=root
jdbc.password=123456

在hibernate的配置文件中有两种方式指定jdbc配置文件

方式一:

<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>

方式二:

<context:property-placeholder location="classpath:jdbc.properties" />

beans.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.2.xsd
            http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
	default-lazy-init="false">

	<context:annotation-config /> 
	<context:component-scan base-package="com,oa" 
		></context:component-scan>
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!--initialSize: 初始化连接-->  
		<property name="initialSize" value="1"/>
		<!--maxActive: 最大连接数量-->  
		<property name="maxActive" value="2"/>    
	</bean>
	<!-- <bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean> -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!--<property name="packagesToScan"> <list> <value>com.pass.bean</value> 
			</list> </property> -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
				org.hibernate.dialect.MySQL5Dialect
				</prop>
				<!-- org.hibernate.dialect.PostgreSQLDialect
				
				 -->
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">none</prop>
				<prop key="hibernate.use_sql_comments">true</prop>
				<prop key="current_session_context_class">thread</prop>
				<prop key="javax.persistence.validation.mode">none</prop>
			</props>
		</property>
		
		<property name="packagesToScan">
			<list>
				<value>com.entity</value>
				<value>oa.entity</value>
			</list>
		</property>
	</bean>


	<bean id="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 事务的注解,如 @Transactional(readOnly=true)
	 <tx:annotation-driven transaction-manager="txManager" />  -->


	<aop:config>
		<aop:pointcut id="bussinessService"
			expression="execution(public 
		* oa.dao..*.*(..)) ||execution(public 
		* com.dao..*.*(..)) || execution(public 
		* com.common.dao.generic..*.*(..))" />
		<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />


	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="count*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="test*" read-only="true" />
			<tx:method name="is*" read-only="true" />
			<tx:method name="show*" read-only="true" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="set*" propagation="REQUIRED" />
			<tx:method name="change*" propagation="REQUIRED" />
			<tx:method name="to*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="verify*" read-only="true" />
			<tx:method name="list*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	
	
<!-- 支持上传文件 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
	
	

</beans>

猜你喜欢

转载自hw1287789687.iteye.com/blog/2247453
今日推荐