Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义注入方法名优化(不兼容). #4159

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.baomidou.mybatisplus.core.injector;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
Expand Down Expand Up @@ -55,7 +55,18 @@ public abstract class AbstractMethod implements Constants {
protected Configuration configuration;
protected LanguageDriver languageDriver;
protected MapperBuilderAssistant builderAssistant;


/**
* 方法名称
* @since 3.4.4
*/
protected final String name;

protected AbstractMethod(String name) {
Assert.notNull(name,"方法名不能为空");
this.name = name;
}

/**
* 注入自定义方法
*/
Expand Down Expand Up @@ -364,15 +375,5 @@ protected MappedStatement addMappedStatement(Class<?> mapperClass, String id, Sq
* @return MappedStatement
*/
public abstract MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo);

/**
* 获取自定义方法名,未设置采用默认方法名
* https://gitee.com/baomidou/mybatis-plus/pulls/88
*
* @return method
* @author 义陆无忧
*/
public String getMethod(SqlMethod sqlMethod) {
return sqlMethod.getMethod();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@
* @since 2018-04-06
*/
public class Delete extends AbstractMethod {


public Delete() {
super("delete");
}

/**
* @param name 方法名
* @since 3.4.4
*/
public Delete(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sql;
Expand All @@ -38,14 +50,14 @@ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> mode
sqlWhereEntityWrapper(true, tableInfo),
sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return addUpdateMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource);
return addUpdateMappedStatement(mapperClass, modelClass, this.name, sqlSource);
} else {
sqlMethod = SqlMethod.DELETE;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
sqlWhereEntityWrapper(true, tableInfo),
sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addDeleteMappedStatement(mapperClass, getMethod(sqlMethod), sqlSource);
return this.addDeleteMappedStatement(mapperClass, this.name, sqlSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@
* @since 2018-04-06
*/
public class DeleteBatchByIds extends AbstractMethod {


public DeleteBatchByIds() {
super("deleteBatchIds");
}

/**
* @param name 方法名
* @since 3.4.4
*/
public DeleteBatchByIds(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sql;
Expand All @@ -43,7 +55,7 @@ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> mode
COLLECTION, null, "item", COMMA),
tableInfo.getLogicDeleteSql(true, true));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, tableInfo.getKeyType());
return addUpdateMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource);
return addUpdateMappedStatement(mapperClass, modelClass, this.name, sqlSource);
} else {
sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
Expand All @@ -52,7 +64,7 @@ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> mode
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
COLLECTION, null, "item", COMMA));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, tableInfo.getKeyType());
return this.addDeleteMappedStatement(mapperClass, getMethod(sqlMethod), sqlSource);
return this.addDeleteMappedStatement(mapperClass, this.name, sqlSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@
* @since 2018-04-06
*/
public class DeleteById extends AbstractMethod {


public DeleteById() {
super("deleteById");
}

/**
* @param name 方法名
* @since 3.4.4
*/
public DeleteById(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sql;
Expand All @@ -57,13 +69,13 @@ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> mode
tableInfo.getLogicDeleteSql(true, true));
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, tableInfo.getKeyType());
return addUpdateMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource);
return addUpdateMappedStatement(mapperClass, modelClass, this.name, sqlSource);
} else {
sqlMethod = SqlMethod.DELETE_BY_ID;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
tableInfo.getKeyProperty());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, tableInfo.getKeyType());
return this.addDeleteMappedStatement(mapperClass, getMethod(sqlMethod), sqlSource);
return this.addDeleteMappedStatement(mapperClass, this.name, sqlSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,32 @@
* @since 2018-04-06
*/
public class DeleteByMap extends AbstractMethod {


public DeleteByMap() {
super("deleteByMap");
}

/**
* @param name 方法名
* @since 3.4.4
*/
public DeleteByMap(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sql;
SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_MAP;
if (tableInfo.isWithLogicDelete()) {
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), sqlWhereByMap(tableInfo));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
return addUpdateMappedStatement(mapperClass, Map.class, getMethod(sqlMethod), sqlSource);
return addUpdateMappedStatement(mapperClass, Map.class, this.name, sqlSource);
} else {
sqlMethod = SqlMethod.DELETE_BY_MAP;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlWhereByMap(tableInfo));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
return this.addDeleteMappedStatement(mapperClass, getMethod(sqlMethod), sqlSource);
return this.addDeleteMappedStatement(mapperClass, this.name, sqlSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@
*/
@SuppressWarnings("serial")
public class Insert extends AbstractMethod {


public Insert() {
super(SqlMethod.INSERT_ONE.getMethod());
}

/**
* @param name 方法名
* @since 3.4.4
*/
public Insert(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
Expand All @@ -56,14 +68,14 @@ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> mode
keyColumn = tableInfo.getKeyColumn();
} else {
if (null != tableInfo.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(getMethod(sqlMethod), tableInfo, builderAssistant);
keyGenerator = TableInfoHelper.genKeyGenerator(this.name, tableInfo, builderAssistant);
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
}
}
}
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource, keyGenerator, keyProperty, keyColumn);
return this.addInsertMappedStatement(mapperClass, modelClass, this.name, sqlSource, keyGenerator, keyProperty, keyColumn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@
* @since 2018-04-06
*/
public class SelectBatchByIds extends AbstractMethod {


public SelectBatchByIds() {
super(SqlMethod.SELECT_BATCH_BY_IDS.getMethod());
}

/**
* @param name 方法名
* @since 3.4.4
*/
public SelectBatchByIds(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_BATCH_BY_IDS;
SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), tableInfo.getKeyColumn(),
SqlScriptUtils.convertForeach("#{item}", COLLECTION, null, "item", COMMA),
tableInfo.getLogicDeleteSql(true, true)), tableInfo.getKeyType());
return addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
return addSelectMappedStatementForTable(mapperClass, this.name, sqlSource, tableInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@
* @since 2018-04-06
*/
public class SelectById extends AbstractMethod {


public SelectById() {
super(SqlMethod.SELECT_BY_ID.getMethod());
}

/**
* @param name 方法名
* @since 3.4.4
*/
public SelectById(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_BY_ID;
SqlSource sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(),
sqlSelectColumns(tableInfo, false),
tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
tableInfo.getLogicDeleteSql(true, true)), tableInfo.getKeyType());
return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
return this.addSelectMappedStatementForTable(mapperClass, this.name, sqlSource, tableInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,25 @@
* @since 2018-04-06
*/
public class SelectByMap extends AbstractMethod {


public SelectByMap() {
super(SqlMethod.SELECT_BY_MAP.getMethod());
}

/**
* @param name 方法名
* @since 3.4.4
*/
public SelectByMap(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_BY_MAP;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false),
tableInfo.getTableName(), sqlWhereByMap(tableInfo));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
return this.addSelectMappedStatementForTable(mapperClass, this.name, sqlSource, tableInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@
* @since 2018-04-08
*/
public class SelectCount extends AbstractMethod {


public SelectCount() {
super(SqlMethod.SELECT_COUNT.getMethod());
}

/**
* @param name 方法名
* @since 3.4.4
*/
public SelectCount(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(), tableInfo.getTableName(),
sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Long.class);
return this.addSelectMappedStatementForOther(mapperClass, this.name, sqlSource, Long.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@
* @since 2018-04-06
*/
public class SelectList extends AbstractMethod {


public SelectList() {
super(SqlMethod.SELECT_LIST.getMethod());
}

/**
* @param name 方法名
* @since 3.4.4
*/
public SelectList(String name) {
super(name);
}

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(),
sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
return this.addSelectMappedStatementForTable(mapperClass, this.name, sqlSource, tableInfo);
}
}
Loading