在实际开发测试中(这里指带有hibernate或其它orm工具的集成开发),如果数据库服务器连接不上了,怎么办?你可能会想到重新在自己的机器上安装数据库,实际上还有更好的方法:使用更小更方快捷的h2数据库,建库建表自动完成,单元测试,集成测试都很方便
版本环境
spring:3.1.1.RELEASE
spring-test:3.1.1.RELEASE
hibernate.version:3.5.6-Final
junit:4.8
1.Spring 配置文件applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS" >
<jdbc:script location="classpath:db-schema.sql"/>
<jdbc:script location="classpath:db-test-data.sql"/>
</jdbc:initialize-database>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file://D:\h2\testdb;USER=SA;"/>
</bean>
<!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
<context:component-scan base-package="domain,dao"/>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<!--sessionFactory的packagesToScan指定了需要扫描的包,只有在指定包下的类使用的注解才有效-->
<value>domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置注解实现管理事务(cglib:proxy-target-class="true") -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true"/>
<!--这是事务定义,而且是使用注解方式定义事务(@Transactional),proxy-target-class="true"表示采用动态代理类来管理事务,
</beans>
2.db-schema.sql和db-test-data.sql文件内容
#db-schema.sql文件
DROP TABLE IF EXISTS USERSE;
CREATE TABLE USERSE(ID INT PRIMARY KEY, NAME VARCHAR(255));
#db-test-data.sql
INSERT INTO USERSE VALUES(1, 'Hello');
3.单元测试类
@RunWith(SpringJUnit4ClassRunner.class)
// specifies the Spring configuration to load for this test fixture
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class UserDAOImplTest {
@Autowired
private UserDAO userDAO;
@Test
public void testGetList() throws Exception {
List dlist = userDAO.getList();
System.out.println("==========="+dlist.size());
System.in.read();
}
}
来源:oschina
链接:https://my.oschina.net/u/76280/blog/77462