Browse Source

调整ActiveRecord日志对象初始化.

https://github.com/baomidou/mybatis-plus/issues/3262
nieqiurong 4 years ago
parent
commit
c0a9ed1c5e

+ 7 - 8
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java

@@ -22,7 +22,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.*;
 import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
-import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
 import org.apache.ibatis.session.SqlSession;
 import org.mybatis.spring.SqlSessionUtils;
@@ -47,7 +46,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
 
     private static final long serialVersionUID = 1L;
 
-    private final transient Log log = LogFactory.getLog(getClass());
+    private final transient Class<?> entityClass = this.getClass();
 
     /**
      * 插入(字段选择插入)
@@ -196,7 +195,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param queryWrapper 实体对象封装操作类(可以为 null)
      */
     public T selectOne(Wrapper<T> queryWrapper) {
-        return SqlHelper.getObject(log, selectList(queryWrapper));
+        return SqlHelper.getObject(() -> LogFactory.getLog(this.entityClass), selectList(queryWrapper));
     }
 
     /**
@@ -238,14 +237,14 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * 执行 SQL
      */
     public SqlRunner sql() {
-        return new SqlRunner(getClass());
+        return new SqlRunner(this.entityClass);
     }
 
     /**
      * 获取Session 默认自动提交
      */
     protected SqlSession sqlSession() {
-        return SqlHelper.sqlSession(getClass());
+        return SqlHelper.sqlSession(this.entityClass);
     }
 
     /**
@@ -264,14 +263,14 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      */
     protected String sqlStatement(String sqlMethod) {
         //无法确定对应的mapper,只能用注入时候绑定的了。
-        return SqlHelper.table(getClass()).getSqlStatement(sqlMethod);
+        return SqlHelper.table(this.entityClass).getSqlStatement(sqlMethod);
     }
 
     /**
      * 主键值
      */
     protected Serializable pkVal() {
-        return (Serializable) ReflectionKit.getFieldValue(this, TableInfoHelper.getTableInfo(getClass()).getKeyProperty());
+        return (Serializable) ReflectionKit.getFieldValue(this, TableInfoHelper.getTableInfo(this.entityClass).getKeyProperty());
     }
 
     /**
@@ -280,6 +279,6 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param sqlSession session
      */
     protected void closeSqlSession(SqlSession sqlSession) {
-        SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(getClass()));
+        SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(this.entityClass));
     }
 }

+ 6 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java

@@ -35,6 +35,7 @@ import java.util.Objects;
 import java.util.function.BiConsumer;
 import java.util.function.BiPredicate;
 import java.util.function.Consumer;
+import java.util.function.Supplier;
 
 /**
  * SQL 辅助类
@@ -121,9 +122,14 @@ public final class SqlHelper {
      * @return ignore
      */
     public static <E> E getObject(Log log, List<E> list) {
+        return getObject(() -> log, list);
+    }
+
+    public static <E> E getObject(Supplier<Log> supplier, List<E> list) {
         if (CollectionUtils.isNotEmpty(list)) {
             int size = list.size();
             if (size > 1) {
+                Log log = supplier.get();
                 log.warn(String.format("Warn: execute Method There are  %s results.", size));
             }
             return list.get(0);