添加三个类即可实现
1.ExBaseMapper 2.ExDefaultSqlInjector 3.UpdateAllById
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import org.apache.ibatis.annotations.Param; public interface ExBaseMapper<T> extends BaseMapper<T> { /** * 通过ID更新数据,包括NULL和空串 * @param entity */ int updateAllById(@Param(Constants.ENTITY) T entity); }
import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector; import com.baomidou.mybatisplus.core.injector.methods.*; import org.springframework.stereotype.Component; import java.util.List; import java.util.stream.Stream; import static java.util.stream.Collectors.toList; @Component public class ExDefaultSqlInjector extends AbstractSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { return Stream.of( new Insert(), new Delete(), new DeleteByMap(), new DeleteById(), new DeleteBatchByIds(), new Update(), new UpdateById(), new SelectById(), new SelectBatchByIds(), new SelectByMap(), new SelectOne(), new SelectCount(), new SelectMaps(), new SelectMapsPage(), new SelectObjs(), new SelectList(), new SelectPage(), new UpdateAllById() // 注入新增的方法 ).collect(toList()); } }
import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; import java.util.Objects; import static java.util.stream.Collectors.joining; public class UpdateAllById extends AbstractMethod { /** * 参考updateAllById中的injectMappedStatement方法 * */ @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { boolean logicDelete = tableInfo.isLogicDelete(); final String additional = optlockVersion() + tableInfo.getLogicDeleteSql(true, false); String sql = String.format("<script>\nUPDATE %s %s WHERE %s=#{%s} %s\n</script>", tableInfo.getTableName(), sqlSet(logicDelete, false, tableInfo, false, ENTITY, ENTITY_DOT), tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return addUpdateMappedStatement(mapperClass, modelClass, "updateAllById", sqlSource); } /** * 重写AbstractMethod的sqlSet方法 * * @see AbstractMethod#sqlSet(boolean, boolean, TableInfo, boolean, String, String) */ protected String sqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, String alias, String prefix) { String sqlScript = getAllSqlSet(table, logic, prefix); if (judgeAliasNull) { sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true); } if (ew) { sqlScript += NEWLINE; sqlScript += SqlScriptUtils.convertIf(SqlScriptUtils.unSafeParam(U_WRAPPER_SQL_SET), String.format("%s != null and %s != null", WRAPPER, U_WRAPPER_SQL_SET), false); } sqlScript = SqlScriptUtils.convertSet(sqlScript); return sqlScript; } /** * 修改TableInfo中的getAllSqlSet方法 * 目的是忽略set sql片段中的if包裹 * * @see TableInfo#getAllSqlSet(boolean, String) */ public String getAllSqlSet(TableInfo table, boolean ignoreLogicDelFiled, final String prefix) { final String newPrefix = prefix == null ? EMPTY : prefix; return table.getFieldList().stream().map(i -> i.getSqlSet(true, newPrefix)).filter(Objects::nonNull).collect(joining(NEWLINE)); }