Spring、Hibernate整合注意事项

1、Spring Jar包:需要额外加入commons-poolcommons-dbcp,若包含spring自带的测试,还需要引入spring-test

2Spring beans.xml:如果使用了Spring annotation,则需要加入以下两项配置:前提是已经导入了beanscontext这两项以xsd为后缀的schemapreference-->xml catlog-->add

注意:最后一项需紧跟着beans的后括号,不能换行或空格,否则不会提示context相关项。

	<!-- Spring Annotation -->
	<context:annotation-config />
	<!-- component包扫描 -->
	<context:component-scan base-package="cn.cuit" />

3、以下是两种对于数据库连接的配置,各有利弊:

①:

	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/spring" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>

②:

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 寻找xml配置文件的路径 -->
		<property name="locations" value="classpath:jdbc.properties" />
	</bean>

	<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}" />
	</bean>

4、接下来是对于连接池的配置,也就是Hibernate里的sessionFactory:其中红色的部分是经我测试发现hibernate3hibernate4中的不同之处,hibernate3中应该不需要这一项,而hibernate4不配置则会出错,目前原因不明。

	<!-- 配置sessionFactory数据库连接池 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 注入dataSource -->
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>cn.cuit.entity.Member</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="javax.persistence.validation.mode">none</prop>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
	</bean>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值