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