最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解

 更新時(shí)間:2023年03月09日 10:54:38   作者:Hi,all  
這篇文章主要給大家介紹了關(guān)于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1  需求

Mybatis-plus使用@TableLogic注解進(jìn)行邏輯刪除數(shù)據(jù)后,在某些場景下,又需要查詢該數(shù)據(jù)時(shí),又不想寫SQL。

2  解決方案

自定義Mybatis-plus的SQL注入器一勞永逸的解決該問題

3  方案:

3.1  方案1,繼承 AbstractMethod拼接SQL語句

public class SelectIgnoreLogicDeleteByMap extends AbstractMethod {
 
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
 
        String sqlBase= "<script>SELECT %s FROM %s %s\n</script>";
        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條件根據(jù)map參數(shù)
     *
     * @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;
    }
}

3.2. 方案2,繼承 AbstractMethod拼接SQL語句

public class SelectIgnoreLogicDelete extends AbstractMethod {
 
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
 
        String sqlBase = "<script>%s SELECT %s FROM %s %s %s %s\n</script>";
 
        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;
    }

4.  自定義SQL注入器,注冊上述自定義的方法

public class CustomSqlInjector extends DefaultSqlInjector {
 
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new SelectIgnoreLogicDeleteByMap());
        methodList.add(new SelectIgnoreLogicDelete());
        return methodList;
    }
}

5.  自定義基礎(chǔ)mapper,聲明注冊的方法

public interface CustomBaseMapper<T> extends BaseMapper<T> {
 
    /**
     * 根據(jù)map條件查詢數(shù)據(jù),并忽略邏輯刪除
     *
     * @param columnMap 查詢條件
     * @return 結(jié)果信息
     */
    List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap);
 
    /**
     * 根據(jù)條件查詢數(shù)據(jù),并忽略邏輯刪除
     *
     * @param queryWrapper 查詢條件
     * @return 結(jié)果信息
     */
    List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper);
}

6. 使用聲明的方法

6.1  業(yè)務(wù)mapper繼承自定義的CustomBaseMapper

@Mapper
public interface UserMapper extends CustomBaseMapper<User> {
}

6.2 調(diào)用方法selectIgnoreLogicDelete

    @Override
    public List<User> getIgnoreDeleteById(Long userId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userId);
        return this.baseMapper.selectIgnoreLogicDelete(queryWrapper);
    }

6.3 調(diào)用方法selectIgnoreLogicDeleteByMap

    @Override
    public List<User> getIgnoreDeleteById(Long userId) {
		Map<String, Object> columnMap = new HashMap<>(2);
        columnMap.put("id", userId);
        return this.baseMapper.selectIgnoreLogicDeleteByMap(columnMap);
    }

總結(jié)

到此這篇關(guān)于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Mybatis-plus查詢@TableLogic邏輯刪除后數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目

    基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目

    這篇文章主要介紹了基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯

    Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯

    這篇文章主要為大家介紹了Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java 處理高并發(fā)負(fù)載類優(yōu)化方法案例詳解

    Java 處理高并發(fā)負(fù)載類優(yōu)化方法案例詳解

    這篇文章主要介紹了Java 處理高并發(fā)負(fù)載類優(yōu)化方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 利用spring的攔截器自定義緩存的實(shí)現(xiàn)實(shí)例代碼

    利用spring的攔截器自定義緩存的實(shí)現(xiàn)實(shí)例代碼

    這篇文章主要介紹了利用spring的攔截器自定義緩存的實(shí)現(xiàn)實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • java substring(a)與substring(a,b)的使用說明

    java substring(a)與substring(a,b)的使用說明

    這篇文章主要介紹了java substring(a)與substring(a,b)的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • MyBatis實(shí)現(xiàn)自定義MyBatis插件的流程詳解

    MyBatis實(shí)現(xiàn)自定義MyBatis插件的流程詳解

    MyBatis的一個(gè)重要的特點(diǎn)就是插件機(jī)制,使得MyBatis的具備較強(qiáng)的擴(kuò)展性,我們可以根據(jù)MyBatis的插件機(jī)制實(shí)現(xiàn)自己的個(gè)性化業(yè)務(wù)需求,本文給大家介紹了MyBatis實(shí)現(xiàn)自定義MyBatis插件的流程,需要的朋友可以參考下
    2024-12-12
  • feign開啟日志Logger.Level?feignLoggerLevel()中Level爆紅的解決

    feign開啟日志Logger.Level?feignLoggerLevel()中Level爆紅的解決

    這篇文章主要介紹了feign開啟日志Logger.Level?feignLoggerLevel()中Level爆紅的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Spring Boot 中使用 Mybatis Plus的操作方法

    Spring Boot 中使用 Mybatis Plus的操作方法

    本文介紹了如何在 Spring Boot 項(xiàng)目中集成 Mybatis Plus,Spring Boot 與 MyBatis Plus 的集成非常簡單,通過自動配置和簡潔的 API,可以大大減少開發(fā)中常見的數(shù)據(jù)庫操作代碼,需要的朋友參考下吧
    2024-12-12
  • Zookeeper ZkClient使用介紹

    Zookeeper ZkClient使用介紹

    ZkClient是Github上?個(gè)開源的zookeeper客戶端,在Zookeeper原生API接口之上進(jìn)行了包裝,是?個(gè)更易用的Zookeeper客戶端,同時(shí),zkClient在內(nèi)部還實(shí)現(xiàn)了諸如Session超時(shí)重連、Watcher反復(fù)注冊等功能
    2022-09-09
  • java大話之創(chuàng)建型設(shè)計(jì)模式教程示例

    java大話之創(chuàng)建型設(shè)計(jì)模式教程示例

    這篇文章主要為大家介紹了java大話之創(chuàng)建型設(shè)計(jì)模式教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評論

屏东市| 西乡县| 鹤庆县| 镇巴县| 石家庄市| 无极县| 迭部县| 东城区| 山丹县| 孟州市| 融水| 德昌县| 信宜市| 静乐县| 信宜市| 海兴县| 栖霞市| 曲沃县| 左云县| 项城市| 邢台市| 虹口区| 绍兴县| 宜春市| 彰化市| 增城市| 潜山县| 宾阳县| 周宁县| 衡水市| 沙雅县| 当阳市| 新化县| 隆昌县| 大余县| 邓州市| 治县。| 大悟县| 乌拉特前旗| 丰镇市| 瑞丽市|