mybatis plus的插件扩展

mybatis plus的插件扩展

 

一、jdbc的一般模式

1、建立连接

2、创建statement

3、设置参数并执行

4、解析结果

5、释放资源

 

个性化的部分是  sql语句 参数映射 结果映射

 

二、mybatis的映射器

MappedStatement = SqlCommandType(CRUD的类别)+SqlSource+ParameterMap+ResultMap+BoundSql

SqlSource:原始xml中设置的sql语句,还没有解释loop等语义

BoundSql: loop、while等语义处理完之后的语句,一般带?和参数等信息

BoundSql = sql(语句)+ parameterObject(入参) + ParameterMapping(参数映射)

 

三、mybatis的4大对象

Executor=StatementHandler(预编译、设置?、执行)+ParameterHandler(塞参数)+ResultHandler(结果映射)

 

四、mybatis的插件机制

1、interceptor抽象(插件)

核心是一层一层的包装,在invoke的时候进行hack,代理的时候是从最外层到最内层的

 

public interface Interceptor {

  // 代理的具体逻辑

  Object intercept(Invocation invocation) throws Throwable;

 

  // 生成代理对象

  default Object plugin(Object target) {

    return Plugin.wrap(target, this);

  }

 

  // 初始化属性

  default void setProperties(Properties properties) {

    // NOP

  }

 

}

 

2、MetaObject 操作对象的属性

3、interceptor拦截的是4大对象之一

 

五、mybatis plus的插件机制

1、自己抽象了InnerInterceptor

2、手动创建MybatisPlusInterceptor这个bean,并设置相关的InnerInterceptor

InnerInterceptor调用流程:

一、Executor的plugin

select语句

1》 willDoQuery

2》 beforeQuery

 

update语句

1》willDoUpdate

2》beforeUpdate

 

二、StatementHandler的prepare

 

例子:

@Bean

@ConditionalOnMissingBean(value = { OptimisticLockerInnerInterceptor.class })

public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {

 

OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();

 

return optimisticLockerInnerInterceptor;

}

 

// 租户隔离

@Bean

@ConditionalOnMissingBean(value = { TenantLineInnerInterceptor.class })

public TenantLineInnerInterceptor tenantLineInnerInterceptor() {

 

TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();

TenantLineHandler handler = new TenantLineHandler() {

 

public Expression getTenantId() {

return null;

}

 

    public String getTenantIdColumn() {

        return "tenant_id";

    }

 

    public boolean ignoreTable(String tableName) {

        return false;

    }

};

 

tenantLineInnerInterceptor.setTenantLineHandler(

handler

 

);

 

return tenantLineInnerInterceptor;

}

 

// 总集成

@Bean

public MybatisPlusInterceptor mybatisPlusInterceptor() {

MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor());

interceptor.addInnerInterceptor(blockAttackInnerInterceptor());

interceptor.addInnerInterceptor(dataPermissionInterceptor());

interceptor.addInnerInterceptor(illegalSQLInnerInterceptor());

interceptor.addInnerInterceptor(paginationInnerInterceptor());

interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());

interceptor.addInnerInterceptor(tenantLineInnerInterceptor());

return interceptor;

}

 

select语句的语法

https://dev.mysql.com/doc/refman/8.0/en/select.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值