环境搭建
Employee对象与employee表(id int,name String,gender int,email String)
IEmpMapper接口和EmpMapper.xml文件
mybatis-config.xml配置文件和JUnit4.13-rc-2测试
当需要为一个程序添加日志记录时,比如要记录下面方法的输入输出。
1 | public class Calculator {//计算器对象 |
2 | public int add(int i, int j) {//加法 |
3 | System.out.println(i+","+j); |
4 | int result=i+j; |
5 | System.out.println("结果:"+result); |
6 | return result; |
7 | } |
8 | } |
9 | @Test |
10 | public void testC(){ |
11 | Calculator cal=new Calculator(); |
12 | cal.add(2,2); |
13 | } |
假装以sout()来记录,这样的代码将日志记录与业务逻辑写在一起,耦合度太高,而且,当方法很多时,要一个一个加记录,很不方便,所以使用动态代理的方式来解决这一问题。
前提:加上名称空间(xmlns:context),开启注解扫描
1 | <beans xmlns="http://www.springframework.org/schema/beans" |
2 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
3 | xmlns:context="http://www.springframework.org/schema/context" |
4 | xsi:schemaLocation="http://www.springframework.org/schema/beans |
5 | https://www.springframework.org/schema/beans/spring-beans.xsd |
6 | http://www.springframework.org/schema/context |
7 | https://www.springframework.org/schema/context/spring-context.xsd"> |
8 | <context:component-scan base-package="com.czw"></context:component-scan> |