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();
    }
}

相关推荐
2601_962065259 小时前
2.启动mysql容器
java·数据库·mysql
2601_9621741710 小时前
Spring 核心技术解析【纯干货版】- XI:Spring 数据访问模块 Spring-Oxm 模块精讲
数据库·python·spring
HanhahnaH10 小时前
垂直分库和水平分库分别解决了什么问题
数据库
吠品10 小时前
SQL Server 版本查询的几种实用方法
数据库·网络协议·ssl
Omics Pro10 小时前
整合多组学分析+生物医学发现!对话式多智能体AI
数据库·人工智能·mysql·机器学习·自然语言处理
严同学正在努力10 小时前
Oracle Data Guard 容灾实战:从搭建到切换,一篇讲透
数据库·ai·oracle
oradh11 小时前
Oracle CTAS+rename 表重建方法操作总结
数据库·oracle·ctas·oracle表重建方法
Dovis(誓平步青云)11 小时前
同一条路线在手机和平板上怎样换一种排版
android·开发语言·数据库·智能手机·音视频
czhc114007566311 小时前
EF Core 数据访问三件套:池化 · 仓储 · 不跟踪
数据库
whcyhhh11 小时前
头歌实践教学平台:大数据存储2023(十三1)
大数据·数据库·python