diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java index 10b417ab48..183af16566 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java @@ -22,7 +22,6 @@ 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> 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 List selectList(Wrapper queryWrapper) { * @param queryWrapper 实体对象封装操作类(可以为 null) */ public T selectOne(Wrapper queryWrapper) { - return SqlHelper.getObject(log, selectList(queryWrapper)); + return SqlHelper.getObject(() -> LogFactory.getLog(this.entityClass), selectList(queryWrapper)); } /** @@ -238,14 +237,14 @@ public Integer selectCount(Wrapper queryWrapper) { * 执行 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 @@ protected String sqlStatement(SqlMethod sqlMethod) { */ 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 @@ protected Serializable pkVal() { * @param sqlSession session */ protected void closeSqlSession(SqlSession sqlSession) { - SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(getClass())); + SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(this.entityClass)); } } diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java index 4d6ba907e9..6ae5381edf 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java @@ -35,6 +35,7 @@ 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 static int retCount(Integer result) { * @return ignore */ public static E getObject(Log log, List list) { + return getObject(() -> log, list); + } + + public static E getObject(Supplier supplier, List 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);