springboot整合mybatis-plus(解决引用jar包配置类不起作用问题)

文档地址 http://mp.baomidou.com/guide/#特性
完整代码示例 https://github.com/baomidou/mybatisplus-spring-boot.git
这里使用H2数据库做测试

添加依赖

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
<dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${version}</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>${version}</version>
        <scope>runtime</scope>
    </dependency>

配置

application.yml 配置文件中添加 H2 数据库的相关配置:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema-h2.sql
    data: classpath:db/data-h2.sql
    url: jdbc:h2:mem:test
    username: root
    password: test

在 Spring Boot 启动类中添加@MapperScan 注解,扫描 Mapper 文件夹:
调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory:

@Bean
    @ConfigurationProperties(prefix = "mybatis")
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        //分页插件
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		 Interceptor[] plugins = {paginationInterceptor};
        sqlSessionFactoryBean.setPlugins(plugins);
        return sqlSessionFactoryBean;
    }

分页插件可以使用上面的方式,或者下面这种方式:

package com.zkt.datasource.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
public class MybatisPlusConfig {

    /**
     * mybatis-plus分页插件<br>
     * 文档:http://mp.baomidou.com<br>
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        System.out.println("注入分页插件");
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

如果将该配置类放在了公共jar包中,直接引用该jar包会发现分页插件不起作用,可以采用下面的方式:

...
@Autowired
private PaginationInterceptor paginationInterceptor;

@Bean
@ConfigurationProperties(prefix = "mybatis")
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() {
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());
    //关键代码
    Interceptor[] plugins = {paginationInterceptor};
    sqlSessionFactoryBean.setPlugins(plugins);
    return sqlSessionFactoryBean;
}
...

配置 MapperScan 注解:

@SpringBootApplication
@MapperScan("com.xx.xx")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }

}

使用mybatis-plus时,需要特别指定(可以不配置mybatis的该属性,但是只配置mybatis的该属性是不行的):

mybatis-plus:
  mapper-locations: classpath*:mybatis/*.xml

需要特别指出的是,mybatis-plus分页插件和pageHelper分页插件是可以同时在一个项目中使用的(此时需要在ymlproperties配置文件中同时配置mybatis-plus.mapper-locationsmybatis.mapper-locations两个属性)。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页