springtest整合junit并结合logback打印mybatis执行详细信息

本文介绍了如何在JUnit测试中利用SpringTest管理配置,并结合Logback来详细打印Mybatis的执行信息,以便于观察事务处理的效果。通过调整logback.xml配置,可以在控制台获取到所需的日志输出。
摘要由CSDN通过智能技术生成

解决的问题:1.使用Junit进行单元测试时,加载spring配置文件交由junit单独管理。我只管@Test下的方法。

      2.junit进行mybatis事务方法我想打印详细信息,通过日志查看事务是否起作用。 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:cn/rjx/spring/sm/bean.xml"}) //junit启动时加载配置文件   
@Component
public class EmpAction  extends AbstractJUnit4SpringContextTests  {
	
	@Autowired
	EmpService service;
	@Test
	public void test01(){
		Emp emp = service.selectById("1");
		System.out.println(emp);
	}
	@Test
	public void test02(){
		service.accountChange();
	}

}

@Service
public class EmpService{
	//org.log4j包
	Logger logger=LoggerFactory.getLogger(EmpService.class);
	@Autowired
	EmpDao empDao;
	public Emp selectById(String id){
		return empDao.selectById(id);
	}
	@Transactional()
	public void accountChange() {
		logger.info("-------add---------");
		empDao.addSalary("1");
		int a= 10/0;
		logger.info("-------update---------");
		empDao.updateSalary("2");
	}
}


@Repository
public interface EmpDao {
	Emp selectById(String id);
	void addSalary(String id);
	void updateSalary(String id);
}


Mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.rjx.spring.sm.EmpDao">
	<select id="selectById"  resultType="cn.rjx.spring.sm.Emp">
		select * from emp where id=#{id}
	</select>
	<update id="addSalary">
		update emp set salary='6000' where id=#{id}
	</update>
	<update id="updateSalary">
		update emp set salary='4000' where id=#{id}
	</update>
</mapper>



bean.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值