目录
3.1 方案1,继承 AbstractMethod拼接SQL语句
3.2. 方案2,继承 AbstractMethod拼接SQL语句
6.1 业务mapper继承自定义的CustomBaseMapper
6.2 调用方法selectIgnoreLogicDelete
6.3 调用方法selectIgnoreLogicDeleteByMap
Mybatis-plus使用@TableLogic注解进行逻辑删除数据后,在某些场景下,又需要查询该数据时,又不想写SQL。
自定义Mybatis-plus的SQL注入器一劳永逸的解决该问题
- public class SelectIgnoreLogicDeleteByMap extends AbstractMethod {
-
- @Override
- public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
-
- String sqlBase= "";
- String sqlScript = this.sqlWhereByMap(tableInfo);
- String sql = String.format(sqlBase, this.sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlScript);
- SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Map.class);
- return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDeleteByMap", sqlSource, tableInfo);
- }
-
- /**
- * 拼接where条件根据map参数
- *
- * @param table 表
- * @return sql
- */
- protected String sqlWhereByMap(TableInfo table) {
- String sqlScript;
- sqlScript = SqlScriptUtils.convertChoose("v == null", " ${k} IS NULL ", " ${k} = #{v} ");
- sqlScript = SqlScriptUtils.convertForeach(sqlScript, "cm", "k", "v", "AND");
- sqlScript = SqlScriptUtils.convertWhere(sqlScript);
- sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and !%s", "cm", "cm.isEmpty"), true);
- return sqlScript;
- }
- }
- public class SelectIgnoreLogicDelete extends AbstractMethod {
-
- @Override
- public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
-
- String sqlBase = "";
-
- String sql = String.format(sqlBase, this.sqlFirst(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlOrderBy(tableInfo), this.sqlComment());
- SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
- return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDelete", sqlSource, tableInfo);
- }
-
- /**
- * 拼接where条件
- *
- * @param newLine 新行
- * @param table 表
- * @return sql
- */
- protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
- String sqlScript = table.getAllSqlWhere(false, true, "ew.entity.");
- sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew.entity"), true);
- sqlScript = sqlScript + "\n";
- sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", String.format("%s and %s", "ew.nonEmptyOfEntity", "ew.nonEmptyOfNormal"), false) + " ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.nonEmptyOfWhere"), true);
- sqlScript = SqlScriptUtils.convertWhere(sqlScript) + "\n";
- sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(" ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.emptyOfWhere"), true);
- sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew"), true);
- return newLine ? "\n" + sqlScript : sqlScript;
- }
- public class CustomSqlInjector extends DefaultSqlInjector {
-
- @Override
- public List
getMethodList(Class> mapperClass, TableInfo tableInfo) { - List
methodList = super.getMethodList(mapperClass, tableInfo); - methodList.add(new SelectIgnoreLogicDeleteByMap());
- methodList.add(new SelectIgnoreLogicDelete());
- return methodList;
- }
- }
- public interface CustomBaseMapper
extends BaseMapper { -
- /**
- * 根据map条件查询数据,并忽略逻辑删除
- *
- * @param columnMap 查询条件
- * @return 结果信息
- */
- List
selectIgnoreLogicDeleteByMap(@Param("cm") Map columnMap) ; -
- /**
- * 根据条件查询数据,并忽略逻辑删除
- *
- * @param queryWrapper 查询条件
- * @return 结果信息
- */
- List
selectIgnoreLogicDelete(@Param("ew") Wrapper queryWrapper) ; - }
- @Mapper
- public interface UserMapper extends CustomBaseMapper
{ -
-
- }
- @Override
- public List
getIgnoreDeleteById(Long userId) { - LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(User::getId,userId);
- return this.baseMapper.selectIgnoreLogicDelete(queryWrapper);
- }
- @Override
- public List
getIgnoreDeleteById(Long userId) { - Map
columnMap = new HashMap<>(2); - columnMap.put("id", userId);
- return this.baseMapper.selectIgnoreLogicDeleteByMap(columnMap);
- }