Search in sources :

Example 6 with PaginationInterceptor

use of com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor in project drug by xiabud.

the class MybatisPlusConfig method paginationInterceptor.

@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor page = new PaginationInterceptor();
    page.setDialectType("mysql");
    return page;
}
Also used : PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 7 with PaginationInterceptor

use of com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor in project loc-framework by lord-of-code.

the class LocMybatisAutoConfiguration method createSqlSessionFactory.

@Nullable
private SqlSessionFactory createSqlSessionFactory(ConfigurableListableBeanFactory configurableListableBeanFactory, String prefixName, MybatisProperties mybatisProperties) {
    DataSource dataSource = configurableListableBeanFactory.getBean(prefixName + "Ds", DataSource.class);
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    sqlSessionFactoryBean.setVfs(LocSpringBootVFS.class);
    Optional.ofNullable(mybatisProperties.getConfigLocation()).map(this.resourceLoader::getResource).ifPresent(sqlSessionFactoryBean::setConfigLocation);
    org.apache.ibatis.session.Configuration configuration = mybatisProperties.getConfiguration();
    if (configuration == null && !StringUtils.hasText(mybatisProperties.getConfigLocation())) {
        configuration = new org.apache.ibatis.session.Configuration();
    }
    sqlSessionFactoryBean.setConfiguration(configuration);
    Optional.ofNullable(mybatisProperties.getConfigurationProperties()).ifPresent(sqlSessionFactoryBean::setConfigurationProperties);
    Optional.ofNullable(mybatisProperties.getTypeAliasesPackage()).ifPresent(sqlSessionFactoryBean::setTypeAliasesPackage);
    Optional.ofNullable(mybatisProperties.getTypeHandlersPackage()).ifPresent(sqlSessionFactoryBean::setTypeHandlersPackage);
    if (!ObjectUtils.isEmpty(mybatisProperties.resolveMapperLocations())) {
        sqlSessionFactoryBean.setMapperLocations(mybatisProperties.resolveMapperLocations());
    }
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    sqlSessionFactoryBean.setPlugins(new Interceptor[] { paginationInterceptor });
    try {
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
        if (sqlSessionFactory == null) {
            log.error("sqlSessionFactoryBean get object is null");
            return null;
        }
        register(configurableListableBeanFactory, sqlSessionFactory, prefixName + "SessionFactory", prefixName + "Sf");
        if (!Strings.isNullOrEmpty(mybatisProperties.getBasePackage())) {
            createBasePackageScanner((BeanDefinitionRegistry) configurableListableBeanFactory, mybatisProperties.getBasePackage(), prefixName);
        } else {
            createClassPathMapperScanner((BeanDefinitionRegistry) configurableListableBeanFactory, prefixName);
        }
        return sqlSessionFactory;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) BeansException(org.springframework.beans.BeansException) DataSource(javax.sql.DataSource) Nullable(org.springframework.lang.Nullable)

Example 8 with PaginationInterceptor

use of com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor in project personManage by WHUTdk.

the class MybatisPlusConfig method paginationInterceptor.

/**
 * 分页插件
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    paginationInterceptor.setDialectType("mysql");
    return paginationInterceptor;
}
Also used : PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 9 with PaginationInterceptor

use of com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor in project javaBook-src by huifer.

the class MybatisPlusConfig method paginationInterceptor.

@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
    // paginationInterceptor.setOverflow(false);
    // 设置最大单页限制数量,默认 500 条,-1 不受限制
    // paginationInterceptor.setLimit(500);
    // 开启 count 的 join 优化,只针对部分 left join
    paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
    return paginationInterceptor;
}
Also used : JsqlParserCountOptimize(com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 10 with PaginationInterceptor

use of com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor in project dynamic_dataSource by tianliuzhen.

the class MybatisPlusConfig method paginationInterceptor.

/*  @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);



        List<ISqlParser> sqlParserList = new ArrayList<>();
        // 如果设置了全局逻辑删除、这里会失效,设置局部没关系已经测试
        // 攻击 SQL 阻断解析器、加入解析链
        sqlParserList.add(new BlockAttackSqlParser() {
            @Override
            public void processDelete(Delete delete) {
                // 如果你想自定义做点什么,可以重写父类方法像这样子
                if ("user_test".equals(delete.getTable().getName())) {
                    // 自定义跳过某个表,其他关联表可以调用 delete.getTables() 判断
                    log.info("跳过表:"+"user_test(这个表允许全部删除)");
                    return ;
                }
                super.processDelete(delete);
            }
        });
        paginationInterceptor.setSqlParserList(sqlParserList);

        return paginationInterceptor;
    }*/
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    /**
     * 1、【测试攻击 SQL】 阻断解析器、加入解析链
     *     自定义设置参考上面注释
     */
    List<ISqlParser> sqlParserList = new ArrayList<>();
    sqlParserList.add(new BlockAttackSqlParser());
    /**
     * 2、【测试多租户】 SQL 解析处理拦截器<br>
     * 传入的值一般都是配置文件 静态变量或者session中取出
     * 意思就是在你的所有的sql 加一个条件 即是 where  AND user.manager_id = 0
     */
    TenantSqlParser tenantSqlParser = new TenantSqlParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId(boolean where) {
            // 此判断用于支持返回多个租户 ID 场景,具体使用查看示例工程
            return new LongValue(0L);
        }

        @Override
        public String getTenantIdColumn() {
            // 多租户自定义字段
            return "manager_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            /*
            if ("user".equals(tableName)) {
                return true;
            }*/
            return false;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {

        @Override
        public boolean doFilter(MetaObject metaObject) {
            MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
            // 意思允许 UserMapper下的这个 getAll 不用加  AND user.manager_id = 0
            if ("com.aaa.mybatisplus.mapper.UserMapper.getAll".equals(ms.getId())) {
                return true;
            }
            return false;
        }
    });
    return paginationInterceptor;
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) MetaObject(org.apache.ibatis.reflection.MetaObject) ArrayList(java.util.ArrayList) BlockAttackSqlParser(com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser) ISqlParserFilter(com.baomidou.mybatisplus.core.parser.ISqlParserFilter) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) LongValue(net.sf.jsqlparser.expression.LongValue) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Bean(org.springframework.context.annotation.Bean)

Aggregations

PaginationInterceptor (com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor)19 Bean (org.springframework.context.annotation.Bean)16 ISqlParser (com.baomidou.mybatisplus.core.parser.ISqlParser)5 JsqlParserCountOptimize (com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize)5 ArrayList (java.util.ArrayList)5 MybatisSqlSessionFactoryBean (com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean)4 TenantHandler (com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler)3 TenantSqlParser (com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser)3 Expression (net.sf.jsqlparser.expression.Expression)3 LongValue (net.sf.jsqlparser.expression.LongValue)3 ISqlParserFilter (com.baomidou.mybatisplus.core.parser.ISqlParserFilter)2 BlockAttackSqlParser (com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 MybatisConfiguration (com.baomidou.mybatisplus.core.MybatisConfiguration)1 DataSource (javax.sql.DataSource)1 Environment (org.apache.ibatis.mapping.Environment)1 Interceptor (org.apache.ibatis.plugin.Interceptor)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)1