期望效果:在xml里面写sql时自动追加deleted逻辑删除字段。比如SELECT count(0) FROM aaa。 执行时最终为 SELECT count(0) FROM aaa WHERE deleted = '0'
实现代码如下:
@Bean
public TenantLineInnerInterceptor tenantLineInnerInterceptor(com.broker.framework.tenant.config.TenantProperties properties, MybatisPlusInterceptor interceptor) { TenantLineInnerInterceptor inner2 = new TenantLineInnerInterceptor(new LogicDeleteDatabaseInterceptor(properties)); MyBatisUtils.addInterceptor(interceptor, inner2, 0); return inner2; }
public class LogicDeleteDatabaseInterceptor implements TenantLineHandler { private final Set<String> ignoreTables = new HashSet<>(); //忽略表,如果不需要,可以去除掉 public LogicDeleteDatabaseInterceptor(TenantProperties properties) { // 不同 DB 下,大小写的习惯不同,所以需要都添加进去 properties.getIgnoreTables().forEach(table -> { ignoreTables.add(table.toLowerCase()); ignoreTables.add(table.toUpperCase()); }); // 在 OracleKeyGenerator 中,生成主键时,会查询这个表,查询这个表后,会自动拼接 TENANT_ID 导致报错 ignoreTables.add("DUAL"); } @Override public Expression getTenantId() { return new LongValue(0); } @Override public boolean ignoreTable(String tableName) { return false; // // 全局忽略多租户 || 忽略多租户的表 ,如果有特殊表没有deleted字段,可以增加忽略配置 // return TenantContextHolder.isIgnore() // || CollUtil.contains(ignoreTables, tableName); } @Override public String getTenantIdColumn() { return "deleted";//这里写死为delete字段 } }