* 默认字段名叫: tenant_id * * @return 租户字段名 */ @Override public String getTenantIdColumn() { return "car_color"; } /** * 根据表名判断是否忽略拼接多租户条件 *
* 默认都要进行解析并拼接多租户条件
*
* @param tableName 表名
* @return 是否忽略, true:表示忽略,false:需要解析并拼接多租户条件
*/
@Override
public boolean ignoreTable(String tableName) {
if ("role".equals(tableName.toString())){
//举例:-如果是role表,忽略租户,不拼接条件
return true;
}
return false;
}
});
return tenantLineInnerInterceptor;
}
```
二、使用
在进行sql查询时,会在where句段自动添加tenant_id=?。
### mybatis-plus禁止全表更新与删除插件
一、配置类拦截器中添加BlockAttackInnerInterceptor插件
```java
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor//添加乐观锁插件
.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
interceptor//禁止全局更新删除插件
.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
```
二、使用
```java
@RequestMapping("/car/updateAll")
private int update(@RequestBody CarDTO carDTO){
return carService.getBaseMapper().update(carDTO,null);
}
```
拦截成功示意
:::info
Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletion
:::
三、注意
BlockAttackInnerInterceptor源码中提高了table.getWhere()。事实证明,mp全局更细拦截器通过判断where子句来判断是否更具更新。
1.所以当存在@Version乐观锁(原理是添加where version,set version。)等时。该拦截器失效。
2.当删除语句为truncate,拦截器失效。为delete,update时可用。
3.不过@TableLogic(添加where 标志位)是可用的,因为源码添加了逻辑删除字段判断。
```java
/**
* 获取表名中的逻辑删除字段
*
* @param tableName 表名
* @return 逻辑删除字段
*/
private String getTableLogicField(String tableName)
```