zeyfra

1、SqlSessionFactory不要使用原生的,使用MybatisSqlSessionFactory。

@Bean("sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Autowired @Qualifier("dataSource") DataSource dataSource) throws Exception {

        // MybatisPlus内部过滤器
        // 为自动分页插件设置DB类型
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        // MybatisConfiguration
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        // 添加自定义拦截器
        mybatisConfiguration.addInterceptor(mybatisPlusInterceptor);
        mybatisConfiguration.addInterceptor(new UpdateInterceptor());

        //使用MybatisSqlSessionFactoryBean
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();

        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);

        // 添加设置
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);

        // 设置xml路径
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:zeyfra/com/modules/**/*.xml"));

        //其他配置
        //Properties properties = new Properties();

        //设置方言:oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby
        //properties.setProperty("helperDialect", "sqlserver");

        //默认值为false,当该参数设置为 true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。
        //properties.setProperty("offsetAsPageNum", "true");

        //默认值为false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为true时,使用 RowBounds 分页会进行 count 查询。
        //properties.setProperty("rowBoundsWithCount", "true");

        //分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
        //properties.setProperty("reasonable", "false");

        //添加设置
        //interceptor.setProperties(properties);

        //也可直接通过以下方式直接添加拦截器或Interceptor数组
        //sqlSessionFactoryBean.setPlugins(new Interceptor[] {interceptor});

        return sqlSessionFactoryBean.getObject();

    }

注意:

1、使用MybatisSqlSessionFactory之后配置应重新在此注入相应的拦截器

2、在3.4.0过后已弃用PaginationInterceptor,应使用MybatisPlusInterceptor添加自动分页插件PaginationInnerInterceptor

3、应在自动分页插件设置DB类型,否则容易出现分页查询后total为0的问题

4、pageHelper依赖会与自动分页插件产生冲突,也会出现分页查询后total为0的问题

相关文章: