mybatisplus3.5.1 租户插件过滤某个方法

 重写

TenantLineInnerInterceptor#beforeQuery
@Configuration
@MapperScan("com.fs.zhnm.edge.data.center.accountauth.dao")
public class MybatisPlusConfig {


    @Autowired
    private RedisFeignAPI redisFeignAPI;

    public static final String TENANT_CASE_KEY = "TENANT_CASE_KEY";

    /**
     * 忽略添加租户ID的表
     */
    private static List<String> IGNORE_TABLE_NAMES = Lists.newArrayList(
            "account_employee_relation","account_org_relation","account_role_relation","application_info","default_application"
            ,"operate_auth","operate_auth_app","operation_account_info","role_operate_auth_relation","tenement_application_relation","tenement_info"
    );

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new TenantLineHandler()
        {
            @Override
            public Expression getTenantId() {
                HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
                String token = request.getHeader("token");
                RetResult<Object> value = redisFeignAPI.getValue(token);
                AccountInfoVo accountInfoVo = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(new Gson().toJson(value.getData()), AccountInfoVo.class);
                if (null != accountInfoVo) {
                    Long tenementId = accountInfoVo.getTenementId();
                    if (null == tenementId) {
                        return new NullValue();
                    }
                    //租户Id,可以从缓存或者cookie,token等中获取
                    return new LongValue(accountInfoVo.getTenementId());
                }
                return new NullValue();
            }

            /**
             * 获取租户字段名(数据库的租户ID字段名)
             *
             * @return
             */
            @Override
            public String getTenantIdColumn() {
                return "tenement_id";
            }

            /**
             * 根据表名判断是否忽略拼接多租户条件
             *
             * @param tableName
             * @return
             */
            @Override
            public boolean ignoreTable(String tableName) {
                return IGNORE_TABLE_NAMES.contains(tableName);
            }
        }));
        PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        interceptor.addInnerInterceptor(innerInterceptor);
        return interceptor;
    }
}
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {

    public MyTenantLineInnerInterceptor() {
        super();
    }

    public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }

    private static List<String> IGNORE_STATEMENT_NAMES = Lists.newArrayList(
            "com.fs.zhnm.edge.data.center.accountauth.dao.AccountInfoDao.getLoginAccountRole"
    );

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        // 如果statementid存在,则忽略方法
        if (IGNORE_STATEMENT_NAMES.contains(ms.getId())) return;
        super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }
}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 3.5.1 版本的分页件是一个非常有用的功能,它可以帮助我们在 MyBatis 中进行分页查询。 首先,在 pom.xml 文件中添加 MyBatis-Plus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> ``` 然后,在你的实体类中定义分页查询的参数: ```java import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @TableName("your_table_name") public class YourEntity { // your entity fields here // getter and setter methods // other methods } ``` 接下来,在你的 Mapper 接口中添加方法来进行分页查询: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.ibatis.annotations.Mapper; @Mapper public interface YourMapper extends BaseMapper<YourEntity> { Page<YourEntity> selectPage(Page<YourEntity> page); } ``` 最后,在你的 Service 类中调用 Mapper 中的方法来实现分页查询: ```java import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class YourService { @Autowired private YourMapper yourMapper; public Page<YourEntity> getPageData(int pageNo, int pageSize) { Page<YourEntity> page = new Page<>(pageNo, pageSize); return yourMapper.selectPage(page); } } ``` 这样就完成了 MyBatis-Plus 3.5.1 版本的分页件的配置和使用。你可以根据需要在分页查询中添加查询条件、排序等操作。希望对你有所帮助!如果你有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值