@Repository publicclassMyBatisAngelWangimplementsIRepository{ @Autowired privateGeneralDAO generalDAO; public<T extendsBase> T get(Class<T> clz,Long id){ HashMap hashMap = generalDAO.getLogically(clz, id); T ret =this.convert(hashMap, clz); return ret; } }
import org.testng.annotations.Test; //@DatabaseSetup(value= "/dbunitData/TestAngelEntity.xml")
publicclassMyBatisAngelWangTestextendsAbstractRollbackTest{ @Autowired privateMyBatisAngelWang myBatisAngelWang; @Test(enabled =false) publicvoid testGet(){ } }
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; @ContextConfiguration(locations ={"classpath:spring-datasource-dbunit.xml", "classpath*:spring-services.xml"}) @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionDbUnitTestExecutionListener.class,TransactionalTestExecutionListener.class}) @Transactional publicclassAbstractRollbackTestextendsAbstractTestNGSpringContextTests{ }
@ContextConfiguration("/config/Spring-db.xml") @Transactional @ActiveProfiles("test") publicclassMyBatisAngelWangTestextends AbstractTransactionalTestNGSpringContextTests{}
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="byName">
<description>spring-datasource-configuration</description>
<beanclass="com.angel.context.ApplicationContextAwareHelper"/>
<!-- 定義事務管理器(聲明式的事務) -->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>classpath*:props/datasource_dev.properties</value>
</list>
</property>
</bean>
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="${jdbc.driver}"/>
<propertyname="url" value="${jdbc.dbunit.url}"/>
<propertyname="username"value="${jdbc.user}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean>
<!-- MyBatis 配置 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.angel.*.dao"/>
<propertyname="sqlSessionFactoryBeanName"value="xSqlSessionFactory"/>
</bean>
<beanid="xSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="typeAliasesPackage"value="com.angel.*.entities"/>
<propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml"/>
<propertyname="mapperLocations"value="classpath:/com/angel/dao/*.xml"/>
<propertyname="plugins">
<array>
<!--page interceptor-->
<beanclass="com.angel.orm.db.QueryInterceptor"/>
</array>
</property>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
</beans>
這樣你們測試的數據庫都是同一個了,也不會有任何的相互影響了。由於事務回滾了,不信的話能夠提交一條Insert測試哦,執行完後查看數據庫中並無插入任何數據。然而,在一個單元測試中,先Insert再get是能夠取到數據的,神奇吧?!html
publicclassApplicationContextAwareHelperimplementsApplicationContextAware{ privatestaticApplicationContext context; @Override publicvoid setApplicationContext(ApplicationContext applicationContext){ context = applicationContext; } publicstaticApplicationContext getContext(){ return context; } }
DruidDataSource dataSource =ApplicationContextAwareHelper.getBean("dataSource_"+ dataSources[i]);
固然,這不屬於單元測試的範疇了,有點跑題,可是蠻有用的,在這裏記一下。java