Spring-Hibernate的三种配置

            在实现类中使用HibernateTemplate 来进行和数据库的连接

 

第一种:直接在hibernateTemplate 的property配置sessionFactory ,在sessionFactory中指向hibernate配置文件

在applicationContext.xml的部分配置

	<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
		<property name="configLocations">
			<list>
				<value>hibernate.cfg.xml</value>
			</list>
		</property>
	</bean>
	
	
	<bean id="hibernateTemplate"  class="org.springframework.orm.hibernate3.HibernateTemplate" >
	 	<property name="sessionFactory" ref="sessionFactory" ></property>
	</bean>
	
	
	<bean id="userDao" class="com.gem.hbn.dao.impl.UserDaoImpl">
		<property name="hibernateTemplate"  ref="hibernateTemplate"></property>
	</bean>

 在hibernate.cfg.xml中的配置

<hibernate-configuration>
<session-factory>
	<property name="myeclipse.connection.profile">mysql</property>
	<property name="connection.url">
		jdbc:mysql://localhost:3306/spring
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">11111</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="show_sql">true</property>
	<mapping resource="com/gem/hbn/pojo/User.hbm.xml" />

</session-factory>
</hibernate-configuration>

 

第二种  hibernateTemplate ->sessionFactory ->,sessionFactory 指定dataSource-ref,mappingResources,hibernateProperties

通过属性来指定方言和显示sql语句  

hibernateProperties配置

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">${dialect}</prop>

<prop key="hibernate.show_sql">${show_sql}</prop>

</props>

 

propertyConfigurer的内容
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring
username=root
password=11111
dialect=org.hibernate.dialect.MySQLDialect
show_sql=true

 

 

 

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>source.properties</value>
			</list>
		</property>
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" p:driverClassName="${driverClassName}" p:url="${url}"
		p:username="${username}" p:password="${password}" />
	</beans>

 

 

第三种 类似第二种,不需要用dataSource来设置数据库信息,直接用hibernateProperties,mappingResources

指定属性和mapping地址

	<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
		  p:mappingResources="com/gem/hbn/xml/User.hbm.xml">
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.connection.driver_class">${driverClassName}</prop>
				<prop key="hibernate.connection.url">${url}</prop>
				<prop key="hibernate.connection.username">${username}</prop>
				<prop key="hibernate.connection.password">${password}</prop>
				<prop key="hibernate.dialect">${dialect}</prop>
				<prop key="hibernate.show_sql">${show_sql}</prop>
			</props>
		</property>
	</bean>

 

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>source.properties</value>
			</list>
		</property>
	</bean>

 propertyConfigurer的内容

 

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/spring

username=root

password=11111

dialect=org.hibernate.dialect.MySQLDialect

show_sql=true

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值