mybatis自定义日志实现类

在使用mybatis,有时候,我们会选择自己的日志类,那么针对mybatis日志选择,如果要使用我们自定义的。

1、实现org.apache.ibatis.logging.Log接口。

public class TestLog implements org.apache.ibatis.logging.Log {


    public TestLog(){
        System.out.println("wucan");
    }

    Logger logger = null;
    public TestLog(String sring){
        logger = Logger.getLogger(sring);
    }




    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
        return false;
    }

    @Override
    public void error(String s, Throwable e) {
        logger.info("LuBan:trace:{}".concat(s));
    }

    @Override
    public void error(String s) {
        logger.info("LuBan:error".concat(s));
    }

    @Override
    public void debug(String s) {
        logger.info("LuBan:debug".concat(s));
    }

    @Override
    public void trace(String s) {
        logger.info("LuBan:trace".concat(s));
    }

    @Override
    public void warn(String s) {
        logger.info("LuBan:warn".concat(s));
    }
}

第二步:在spring-mybatis整合时,我们要通过sqlSessionFactory来设置mybatis的日志选择

public static void main(String[] args) {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Appconfig.class);
    SqlSessionFactory sqlSessionFactory = annotationConfigApplicationContext.getBean("sqlSessionFactory", SqlSessionFactory.class);
    sqlSessionFactory.getConfiguration().setLogImpl(TestLog.class);
}

通过拿到SessionFactoryFactory,然后拿到Configuration,里面包含了很多配置mybatis属性的东西。在set日志实现类。

 

注意:isDebugEnabled()和TestLog(String strin)方法,这两个很重要,当在选择打印Debug级别日志的时候,mybatis在打印debug的时候,会调用isDebugenabled方法判断是否开启了Debug日志。下面贴行mybatis源码中的一个打印debug日志的判断逻辑

if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("SqlSession [" + session + "] was not registered for synchronization because synchronization is not active");
      }

所以,如果要打印debug日志,就要把方法返回值改成true。

需要注意的第二个点是,有参的构造方法。在mybatis选择日志是。

private static void setImplementation(Class<? extends Log> implClass) {
  try {
    Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
    Log log = candidate.newInstance(LogFactory.class.getName());
    if (log.isDebugEnabled()) {
      log.debug("Logging initialized using '" + implClass + "' adapter.");
    }
    logConstructor = candidate;
  } catch (Throwable t) {
    throw new LogException("Error setting Log implementation.  Cause: " + t, t);
  }
}

会通过上面代码执行,在获取无参构造方式是,如果没有会报错,就无法执行下去了,最终导致日志设置失败。

通过自定义日志,我们可以随意选择日志体系,设置日志格式等等。

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis提供了自定义拦截器的功能,可以在执行SQL语句前后进行一些自定义的操作,比如日志记录、权限校验等。要实现自定义拦截器,你需要按照以下步骤进行操作: 1. 创建一个Java实现`Interceptor`接口。这个接口定义了MyBatis拦截器的方法。 ```java public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 在执行SQL语句前后进行一些自定义操作 // 这里可以写你的逻辑代码 return invocation.proceed(); // 继续执行下一个拦截器或目标对象的方法 } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); // 使用当前拦截器包装目标对象 } @Override public void setProperties(Properties properties) { // 设置一些属性值 } } ``` 2. 在MyBatis配置文件(比如`mybatis-config.xml`)中配置自定义拦截器。 ```xml <configuration> <!-- 其他配置 --> <plugins> <plugin interceptor="com.example.MyInterceptor"> <!-- 可以设置一些属性值 --> </plugin> </plugins> </configuration> ``` 注意,`com.example.MyInterceptor`是你自己实现的拦截器的全限定名。 通过以上步骤,你就可以实现自定义拦截器了。当MyBatis执行SQL语句时,会先调用你的拦截器的`intercept`方法,在该方法内部你可以编写你想要的逻辑。还可以通过`plugin`方法对目标对象进行包装,以实现多个拦截器的链式调用。 希望能帮到你!如有更多问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信仰_273993243

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值