Skip to content

Commit

Permalink
调整获取注入方法.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Mar 28, 2024
1 parent 77e1bc5 commit a9a35c6
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import java.util.Map;

/**
* <p>
* </p>
* SqlRunner执行接口
*
* @author yuxiaobin
* @since 2018/2/7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.apache.ibatis.mapping.MappedStatement;

/**
* 批量执行方法
*
* @author nieqiurong
* @since 3.5.4
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

Expand All @@ -47,11 +49,15 @@ public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapp
if (!mapperRegistryCache.contains(className)) {
TableInfo tableInfo = TableInfoHelper.initTableInfo(builderAssistant, modelClass);
List<AbstractMethod> methodList = this.getMethodList(mapperClass, tableInfo);
// 兼容旧代码
if (CollectionUtils.isEmpty(methodList)) {
methodList = this.getMethodList(builderAssistant.getConfiguration(), mapperClass, tableInfo);
}
if (CollectionUtils.isNotEmpty(methodList)) {
// 循环注入自定义方法
methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass, tableInfo));
} else {
logger.debug(mapperClass.toString() + ", No effective injection method was found.");
logger.debug(className + ", No effective injection method was found.");
}
mapperRegistryCache.add(className);
}
Expand All @@ -64,9 +70,27 @@ public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapp
* </p>
*
* @param mapperClass 当前mapper
* @param tableInfo 表信息
* @return 注入的方法集合
* @since 3.1.2 add mapperClass
* @deprecated 3.5.6 {@link #getMethodList(Configuration, Class, TableInfo)}
*/
public abstract List<AbstractMethod> getMethodList(Class<?> mapperClass,TableInfo tableInfo);
@Deprecated
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
return getMethodList(tableInfo.getConfiguration(), mapperClass, tableInfo);
}

/**
* 获取注入的方法
*
* @param configuration 配置对象
* @param mapperClass 当前mapper
* @param tableInfo 表信息
* @return 注入方法集合
* @since 3.5.6
*/
public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
return new ArrayList<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.baomidou.mybatisplus.core.injector.methods.*;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import org.apache.ibatis.session.Configuration;

import java.util.List;
import java.util.stream.Stream;
Expand All @@ -35,8 +36,8 @@
public class DefaultSqlInjector extends AbstractSqlInjector {

@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
GlobalConfig.DbConfig dbConfig = GlobalConfigUtils.getDbConfig(tableInfo.getConfiguration());
public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
GlobalConfig.DbConfig dbConfig = GlobalConfigUtils.getDbConfig(configuration);
Stream.Builder<AbstractMethod> builder = Stream.<AbstractMethod>builder()
.add(new Insert(dbConfig.isInsertIgnoreAutoIncrementColumn()))
.add(new Delete())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ public interface ISqlInjector {
* @param mapperClass mapper 接口的 class 对象
*/
void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass);

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig g
* 测试注入自定义方法
*/
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
public List<AbstractMethod> getMethodList(org.apache.ibatis.session.Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
// methodList.add(new LogicDeleteByIdWithFill());
methodList.add(new AlwaysUpdateSomeColumnById(t -> t.getFieldFill() != FieldFill.INSERT));
methodList.add(new InsertBatchSomeColumn(t -> !(t.getFieldFill() == FieldFill.UPDATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource, ResourceLoader
* 测试注入自定义方法
*/
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
public List<AbstractMethod> getMethodList(org.apache.ibatis.session.Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
methodList.add(new LogicDeleteByIdWithFill());
methodList.add(new AlwaysUpdateSomeColumnById(t -> t.getFieldFill() != FieldFill.INSERT));
methodList.add(new InsertBatchSomeColumn(t -> !(t.getFieldFill() == FieldFill.UPDATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteBatchByIds;
import com.baomidou.mybatisplus.test.BaseDbTest;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -127,8 +128,8 @@ public void updateFill(MetaObject metaObject) {
});
globalConfig.setSqlInjector(new DefaultSqlInjector() {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
methodList.add(new LogicDeleteBatchByIds("testDeleteBatch"));
return methodList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public GlobalConfig globalConfig() {
* 注入自定义全局方法
*/
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
public List<AbstractMethod> getMethodList(org.apache.ibatis.session.Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
methodList.add(new Upsert());
return methodList;
}
Expand Down

0 comments on commit a9a35c6

Please sign in to comment.