spring官方文档总提示要进行SpringJunit测试必须先配置两个信息:
1、使用Spring IOC功能配置
2、配置正确的JDBC或ORM框架连接数据库
下面进行spring3和hibernate4测试平台基础配置说明:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cml.*">
</context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jpa" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="3" />
<property name="maxPoolSize" value="30" />
<property name="initialPoolSize" value="3" />
<property name="maxIdleTime" value="25000" />
<property name="acquireIncrement" value="1" />
<property name="acquireRetryAttempts" value="30" />
<property name="acquireRetryDelay" value="1000" />
<property name="testConnectionOnCheckin" value="true" />
<property name="idleConnectionTestPeriod" value="18000" />
<property name="checkoutTimeout" value="3000" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:/hibernate.cfg.xml">
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
1、注意如果你使用的是hibernate3则需要将org.springframework.orm.hibernate4.HibernateTransactionManage改为org.springframework.orm.hibernate3.HibernateTransactionManage
2、此处使用出c3p0连接池,可以根据实际情况更改
3、hibernate配置文件为:
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
需要制定数据库方言