Springcloud多模块整合mybatis-plus

最近打算搭一个spring-cloud的框架,并打算整合mybatis-plus的插件。然后却遇到了一个消耗了我十几个钟的问题。

出现的问题是:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bestlmc.lihuamao.services.service.ITMenuService.getTest

这里显示的是绑定的问题,然后我查了一下官方文档,一般是以下几个问题导致:

https://mp.baomidou.com/guide/faq.html#出现-invalid-bound-statement-not-found-异常

  • 检查是不是引入 jar 冲突

  • 检查 Mapper.java 的扫描路径

    • 方法一:在 Configuration 类上使用注解 MapperScan

    @Configuration
    @MapperScan("com.yourpackage.*.mapper")
    public class YourConfigClass{
    ...
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
       MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
       //可以通过环境变量获取你的mapper路径,这样mapper扫描可以通过配置文件配置了
       scannerConfigurer.setBasePackage("com.yourpackage.*.mapper");
       return scannerConfigurer;
    }
  • 检查是否指定了主键?如未指定,则会导致 selectById 相关 ID 无法操作,请用注解 @TableId 注解表 ID 主键。当然 @TableId 注解可以没有!但是你的主键必须叫 id(忽略大小写)

  • SqlSessionFactory不要使用原生的,请使用MybatisSqlSessionFactory

  • 检查是否自定义了SqlInjector,是否复写了getMethodList()方法,该方法里是否注入了你需要的方法(可参考DefaultSqlInjector)

也有人说配置文件中,mybatis要改成mybatis-plus:

mybatis-plus:
mapper-locations: classpath*:/mapper/*Mapper.xml
type-aliases-package: com.bestlmc.lihuamao.commons.been    # 所有Entity别名类所在包

诸如此类,终究还是没有效果。

但是我自己用springboot但模块整合mybatis-plus使用的话还是正常的。然而,这跟我想要的效果不一样。出于解耦的思想,我想要将service层和mapper层的代码分离出来,作为一个公用的模块。这样便可以减少代码的冗余。

只是按照这样的模块搭建,能成功运行,访问接口的时间就会报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 这样的错误。

最终检查多次之后,发现了问题出在启动类中:

@SpringBootApplication
@EnableOpenApi
@MapperScan(basePackages = {
       "com.bestlmc.lihuamao.admin.config",
       "com.bestlmc.lihuamao.admin.controller",
       "com.bestlmc.lihuamao.services.service",
       "com.bestlmc.lihuamao.commons.config"})
public class AdminApplication {
   public static void main(String[] args) {
       SpringApplication.run(AdminApplication.class,args);
  }
}

@MapperScan只是扫描mapper的实现类

这里应该使用的是@ComponentScan,@ComponentScan适合扫描各类的been。

最终将MapperScan更换为ComponentScan,解决问题。

@SpringBootApplication
@EnableOpenApi
@ComponentScan(basePackages = {
       "com.bestlmc.lihuamao.admin.config",
       "com.bestlmc.lihuamao.admin.controller",
       "com.bestlmc.lihuamao.services.service",
       "com.bestlmc.lihuamao.commons.config"})
public class AdminApplication {
   public static void main(String[] args) {
       SpringApplication.run(AdminApplication.class,args);
  }
}

详细代码可以查看我的开源项目https://gitee.com/bestlmc/lihuamao_blog

 

posted @ 2021-06-21 22:07  小白很爱吃  阅读(1178)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end