package com.zy.asrs.wcs.common.config; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.zy.asrs.wcs.system.entity.User; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.LongValue; import net.sf.jsqlparser.expression.NullValue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import java.util.Arrays; /** * MybatisPlus配置 * * @author vincent * @since 2018-02-22 11:29:28 */ @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { @Override public Expression getTenantId() { return getLoginUserTenantId(); } @Override public String getTenantIdColumn() { return "host_id"; } // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件 @Override public boolean ignoreTable(String tableName) { if (getTenantId() == null) { return true; }else { return Arrays.asList( "sys_host", "sys_user_role", "sys_role_menu", "wcs_bas_shuttle_err", "wcs_bas_lift_err" ).contains(tableName); } } })); // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor // 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(false); } // @Bean // public MybatisPlusInterceptor mybatisPlusInterceptor() { // MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // // TenantLineHandler tenantLineHandler = new TenantLineHandler() { // // @Override // public Expression getTenantId() { // return getLoginUserTenantId(); // } // // @Override // public String getTenantIdColumn() { // return "host_id"; // } // // @Override // public boolean ignoreTable(String tableName) { // if (getTenantId() == null) { // return true; // }else { // return Arrays.asList( // "sys_host", // "sys_user_role", // "sys_role_menu", // "wcs_bas_shuttle_err", // "wcs_bas_lift_err" // ).contains(tableName); // } // } // // }; // TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(tenantLineHandler); // interceptor.addInnerInterceptor(tenantLineInnerInterceptor); // // PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // interceptor.addInnerInterceptor(paginationInnerInterceptor); // // return interceptor; // } /** * 获取当前登录用户的租户id * * @return Integer */ public Expression getLoginUserTenantId() { try { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Object object = authentication.getPrincipal(); if (object instanceof User) { return new LongValue(((User) object).getHostId()); } } } catch (Exception e) { System.out.println(e.getMessage()); } return null; } }