方案1,继承 AbstractMethod拼接SQL语句
publicclassSelectIgnoreLogicDeleteByMapextendsAbstractMethod { publicMappedStatementinjectMappedStatement(Class<?>mapperClass, Class<?>modelClass, TableInfotableInfo) { StringsqlBase="<script>SELECT %s FROM %s %s\n</script>"; StringsqlScript=this.sqlWhereByMap(tableInfo); Stringsql=String.format(sqlBase, this.sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlScript); SqlSourcesqlSource=this.languageDriver.createSqlSource(this.configuration, sql, Map.class); returnthis.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDeleteByMap", sqlSource, tableInfo); } /*** 拼接where条件根据map参数** [url=home.php?mod=space&uid=952169]@Param[/url] table 表* [url=home.php?mod=space&uid=155549]@Return[/url] sql*/protectedStringsqlWhereByMap(TableInfotable) { StringsqlScript; 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); returnsqlScript; } }
方案2,继承 AbstractMethod拼接SQL语句
publicclassSelectIgnoreLogicDeleteextendsAbstractMethod { publicMappedStatementinjectMappedStatement(Class<?>mapperClass, Class<?>modelClass, TableInfotableInfo) { StringsqlBase="<script>%s SELECT %s FROM %s %s %s %s\n</script>"; Stringsql=String.format(sqlBase, this.sqlFirst(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlOrderBy(tableInfo), this.sqlComment()); SqlSourcesqlSource=this.languageDriver.createSqlSource(this.configuration, sql, modelClass); returnthis.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDelete", sqlSource, tableInfo); } /*** 拼接where条件** @param newLine 新行* @param table 表* @return sql*/protectedStringsqlWhereEntityWrapper(booleannewLine, TableInfotable) { StringsqlScript=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); returnnewLine?"\n"+sqlScript : sqlScript; }
自定义SQL注入器,注册上述自定义的方法
publicclassCustomSqlInjectorextendsDefaultSqlInjector { publicList<AbstractMethod>getMethodList(Class<?>mapperClass, TableInfotableInfo) { List<AbstractMethod>methodList=super.getMethodList(mapperClass, tableInfo); methodList.add(newSelectIgnoreLogicDeleteByMap()); methodList.add(newSelectIgnoreLogicDelete()); returnmethodList; } }
自定义基础mapper,声明注册的方法
publicinterfaceCustomBaseMapper<T>extendsBaseMapper<T> { /*** 根据map条件查询数据,并忽略逻辑删除** @param columnMap 查询条件* @return 结果信息*/List<T>selectIgnoreLogicDeleteByMap( ("cm") Map<String, Object>columnMap); /*** 根据条件查询数据,并忽略逻辑删除** @param queryWrapper 查询条件* @return 结果信息*/List<T>selectIgnoreLogicDelete( ("ew") Wrapper<T>queryWrapper); }
2.使用声明的方法
业务mapper继承自定义的CustomBaseMapper
publicinterfaceUserMapperextendsCustomBaseMapper<User> { }
调用方法selectIgnoreLogicDelete
publicList<User>getIgnoreDeleteById(LonguserId) { LambdaQueryWrapper<User>queryWrapper=newLambdaQueryWrapper<>(); queryWrapper.eq(User::getId,userId); returnthis.baseMapper.selectIgnoreLogicDelete(queryWrapper); }
调用方法selectIgnoreLogicDeleteByMap
publicList<User>getIgnoreDeleteById(LonguserId) { Map<String, Object>columnMap=newHashMap<>(2); columnMap.put("id", userId); returnthis.baseMapper.selectIgnoreLogicDeleteByMap(columnMap); }