自定义Mybatis LanguageDriver性能优化

场景:高并发情况下mybatis 动态sql 解析 锁问题优化

  • 优化前 并发测试 XMLLanguageDriver 类 的 createSqlSource 方法有锁 而且 每次执行时都会走该方法
    自定义Mybatis LanguageDriver性能优化_第1张图片

  • 自定义Mybatis LanguageDriver性能优化_第2张图片

  • 优化前 : 线程有Block
    自定义Mybatis LanguageDriver性能优化_第3张图片

  • 优化后的 LanguageDriver

public class CustomXMLLanguageDriver implements LanguageDriver {


    private final Map sqlSourceCache = new ConcurrentHashMap<>();

    @Override
    public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
        return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
    }

    @Override
    public SqlSource createSqlSource(Configuration configuration, XNode script, Class parameterType) {
        XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
        return builder.parseScriptNode();

    }


    @Override
    public SqlSource createSqlSource(Configuration configuration, String script, Class parameterType) {
        if (script.startsWith("
                    
                    

你可能感兴趣的:(mybatis,性能优化,Mybatis,sql动态解析)