多模块工程中Controller中注入Service报错的问题

问题

2024-06-05 22:05:12,241 ERROR [http-nio-8888-exec-1][DirectJDKLog.java:175] - Servlet.service() for servlet [dispatcherServlet] in context with path [/content] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): org.chz.service.content.service.CourseBaseInfoService.queryCourseBaseList] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): org.chz.service.content.service.CourseBaseInfoService.queryCourseBaseList
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:51)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:111)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at com.baomidou.mybatisplus.core.toolkit.CollectionUtils.computeIfAbsent(CollectionUtils.java:117)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:98)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)

SpringBoot启动类默认扫描路径是:当前Application类所在的包及其子包。

在多模块工程中,Service接口在其他module中,不能被扫描到。

这是为了使:

    @Autowired
    CourseBaseInfoService courseBaseInfoService;

注入不报错,可能会使用

    @Resource
    CourseBaseInfoService courseBaseInfoService;

或者在启动类上添加@MapperScan

@SpringBootApplication(scanBasePackages = "org.chz")
@MapperScan("xxx.xxx")
public class ApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }

}

MapperScan的作用:扫描指定的包,包里面的所有接口在编译之后都会生成相应的实现类。
也就是说,在controller使用service时,获取到了一个service的实现类的实例,而没有真正继承service接口,这时运行就会报错。

解决方法

@SpringBootApplictaion(scanBasePackages="com.example"

其中:Service 及 ServiceImpl均需在com.example包下

@ComponentScan="com.example"也可以

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值