简单的mybatis+spring+junit整合

使用的数据库是:oralce

建立一张TEST表,再建一个ID字段用于简单的测试

需要的包:spring的jar包,mybatis.jar,mybatis-spring.jar,commons-logging.jar,aopalliance.jar,aspectjweaver-1.6.9.jar,ojdbc6.jar,junit.jar,junit-dep.jar

简单的mybatis+spring+junit整合_第1张图片

spring配置文件:applicationContext.xml





    
    
    
        
        
        
        
    
    
    
        
    

        
    

    
    
	
    
    

    
    
	
    
    
        
        
    
    
	
    

    
     
        
     

在mybatis.mapper下建立mapper

TestMapper.java

package mybatis.mapper;

import java.util.List;

public interface TestMapper {
	public List selectAllInfo();
}
TestMapper.xml






  

建立模板类:Test.java

模板类需要映射数据库对应表TEST的所有字段

package mybatis.model;
/**
 * 实体类
 *
 */
public class Test {
	private Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
}
这时我们就构建了简单的mybatis+spring

然后建立junit测试类:

首先建立一个junit测试类的父类,这个父类中写了需要加载的spring配置文件,事务的控制,便于新的junit类来继承这个父类

package mybatis.junit;

import org.junit.runner.RunWith;  
import org.springframework.test.context.ContextConfiguration;  
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
import org.springframework.test.context.transaction.TransactionConfiguration;
/** 
 * Junit 基础类,加载环境  
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})  
@TransactionConfiguration(transactionManager="jdbcTransactionManager",defaultRollback=true)
public  class TestBase extends AbstractTransactionalJUnit4SpringContextTests {  

}  
建立测试类:@Autowired是spring注解的一种方式,@Test是junit测试找的执行方法

package mybatis.junit.test;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import mybatis.junit.TestBase;
import mybatis.mapper.TestMapper;

public class TestMybatis extends TestBase {
	@Autowired
	private TestMapper testMapper;
	
	@Test
	public void getCallCenterAccountByCrmAccount(){
		try {
			List list = testMapper.selectAllInfo();
			System.out.println(list.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
这样我们就将mybatis,spring,junit整合完毕了,这套整合主要用于平时的测试。

如果需要在web项目中启动还需要配置一个aciton层

web.xml文件:




     
     
     
	contextConfigLocation
	
		/WEB-INF/applicationContext*.xml
	
     
     
	webAppRootKey
	webApp.root
     
	
       
      
        CharacterEncodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
          
            encoding  
            utf-8  
          
          
            forceEncoding  
            true  
          
      
      
        CharacterEncodingFilter  
        /*  
      
    
    
    
    
	
		org.springframework.web.context.ContextLoaderListener
	
    
    
	 
    
    	index.jsp
    

再新增一个spring配置文件:applicationContextTaskConfig.xml




   
    
    

		
action类: TestAction.java

package mybatis.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import mybatis.mapper.TestMapper;

public class TestAction {
	@Autowired
	public TestMapper testMapper;
	
	public String selectAllInfo(){
		List test = testMapper.selectAllInfo();
		System.out.println(test.size());
		return "success";
	}
}










你可能感兴趣的:(mybatis,spring,junit)