1.引入所需jar包,spring-3.2*.jar、junit4.x.jar等;
2.配置sping-test.xml文件,并将文件放在源路径下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 9 <!-- zi dong sao mian qie zhi sao mian@Controller --> 10 <context:component-scan base-package="com.eastcom_sw" use-default-filters="false"> 11 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 12 </context:component-scan> 13 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 14 <property name="dataSource"> 15 <ref local="dataSource"/> 16 </property> 17 </bean> 18 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 19 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 20 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> 21 <property name="username" value="orcl" /> 22 <property name="password" value="orcl123" /> 23 </bean> 24 </beans>
3.编写单元测试类:
1 @RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 2 @ContextConfiguration(locations={"classpath*:spring-test.xml"})//加载spring文件 3 @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)//配置事务 4 public class MyWebTest{ 5 6 @Resource 7 private JdbcTemplate jdbcTemplate; //注入jdbctemplate类 8 9 @Test 10 public void test01(){ 11 List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from scott.emp"); 12 13 System.out.println(list.size()); 14 15 } 16 17 }