Skip to content

Commit

Permalink
调整ActiveRecord日志对象初始化.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Jan 18, 2021
1 parent cc6a205 commit c0a9ed1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

/**
* 插入(字段选择插入)
Expand Down Expand Up @@ -196,7 +195,7 @@ public List<T> selectList(Wrapper<T> queryWrapper) {
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
public T selectOne(Wrapper<T> queryWrapper) {
return SqlHelper.getObject(log, selectList(queryWrapper));
return SqlHelper.getObject(() -> LogFactory.getLog(this.entityClass), selectList(queryWrapper));
}

/**
Expand Down Expand Up @@ -238,14 +237,14 @@ public Integer selectCount(Wrapper<T> 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);
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 辅助类
Expand Down Expand Up @@ -121,9 +122,14 @@ public static int retCount(Integer result) {
* @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);
Expand Down

1 comment on commit c0a9ed1

@nieqiurong
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.