当前位置:主页 > 软件编程 > JAVA代码 >

springboot集成mybatis-plus遇到的问题及解决方法

时间:2020-11-27 11:29:32 | 栏目:JAVA代码 | 点击:

在使用spring boot集成mybatis-plus的过程中遇到的问题

如图,

首先我放xml的包的是没问题的,而是引入的架包和配置问题,问题配置如下

解决方法:请将mybatis-plus改成mybatis,mybatis,mybtis,重要的说三遍,必要的架包如下

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring- boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>2.1.9</version>
</dependency>
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
  <version>1.3.2</version>
</dependency>

注意第一个是mybatis-spring-boot-start ,不是mybatisplus-spring-boot-start还要在mybatis配置文件里声明MybatisSqlSessionFactoryBean,至此问题解决

@Configuration
public class MybatisPlusConfig {
  @Autowired
  private DataSource dataSource;

  @Autowired
  private MybatisProperties properties;

  @Autowired
  private ResourceLoader resourceLoader = new DefaultResourceLoader();

  @Autowired(required = false)
  private Interceptor[] interceptors;

  @Autowired(required = false)
  private DatabaseIdProvider databaseIdProvider;

  /**
   *  mybatis-plus分页插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor page = new PaginationInterceptor();
    page.setDialectType("mysql");
    return page;
  }
  /**
   * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
   * 配置文件和mybatis-boot的配置文件同步
   * @return
   */
  @Bean
  public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    mybatisPlus.setDataSource(dataSource);
    mybatisPlus.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
      mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    mybatisPlus.setConfiguration(properties.getConfiguration());
    if (!ObjectUtils.isEmpty(this.interceptors)) {
      mybatisPlus.setPlugins(this.interceptors);
    }
    MybatisConfiguration mc = new MybatisConfiguration();
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
      mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
      mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
      mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
      mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    }
    return mybatisPlus;
  }
}

您可能感兴趣的文章:

相关文章