SQL server使用MybatisPlus查询SQL加上WITH (NOLOCK)

文章目录

  • [WITH (NOLOCK) 是什么?](#WITH (NOLOCK) 是什么?)
  • 代码示例

WITH (NOLOCK) 是什么?

WITH (NOLOCK) 也叫 未提交读(READ UNCOMMITTED),是 SQL Server 中的一种表提示(Table Hint),本质是让查询语句忽略目标表上的共享锁和排他锁,直接读取数据,核心目的是提升查询性能、避免查询被阻塞。

SQL Server 默认的隔离级别是 READ COMMITTED(已提交读),即查询会等待目标数据上的锁释放后再读取,且只能读取已提交的数据;而 NOLOCK 会强制查询使用 READ UNCOMMITTED 隔离级别,打破这个规则。

代码示例

java 复制代码
package com.qf.config;


import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
@Component
public class MybatisPlusSqlInterceptor implements Interceptor {


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        // 先判断是不是SELECT操作 不是直接跳过
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
            return invocation.proceed();
        }
        String id = mappedStatement.getId();
        String methodName = id.substring(id.lastIndexOf(".") + 1, id.length());
        //只有Mybatis plus的selectOne和selectList做特殊处理
        if (!methodName.equals("selectOne") && !methodName.equals("selectList") && !methodName.equals("selectMaps")) {
            return invocation.proceed();
        }
        BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
        // 执行的SQL语句
        String originalSql = boundSql.getSql().replace("\n", "");
        String tableName = extractFromClause(originalSql);
        log.info("要执行的sql:{}", originalSql);
        log.info("正则表达式获取要执行的sql的表名:{}", tableName);
        if (StringUtils.isNotEmpty(tableName)) {
            String finalSql = originalSql.replace(tableName, tableName + " WITH(NOLOCK)");
            log.info("处理后的sql:{}", finalSql);
            metaObject.setValue("delegate.boundSql.sql", finalSql);
        }
        return invocation.proceed();
    }

    public static String extractFromClause(String query) {
        String table = "";
        Pattern p = Pattern.compile("FROM \\[(.*?)\\]");
        Matcher m = p.matcher(query);
        if (m.find()) {
            table = m.group();
        }
        return table.trim();
    }
}

相关推荐
Turnip120219 小时前
深度解析:为什么简单的数据库"写操作"会在 MySQL 中卡住?
后端·mysql
爱可生开源社区19 小时前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1771 天前
《从零搭建NestJS项目》
数据库·typescript
加号32 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏2 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐2 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再2 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
WeiXin_DZbishe2 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
tryCbest2 天前
数据库SQL学习
数据库·sql
jnrjian2 天前
ORA-01017 查找机器名 用户名 以及library cache lock 参数含义
数据库·oracle