今天启动项目时报错Error starting ApplicationContext. To display the conditions report re–run your app
具体报错信息如下
rror starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-02-05 13:46:49.050 ERROR 4332 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field userMapper in cn.jczb.service.jczbserviceprovider.service.UserService required a bean of type 'cn.jczb.service.jczbserviceprovider.mapper.UserMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'cn.jczb.service.jczbserviceprovider.mapper.UserMapper' in your configuration. Disconnected from the target VM, address: '127.0.0.1:12912', transport: 'socket' Process finished with exit code 1
启动失败,原因就是依赖中有mapper启动器,却没有加Mapper注解,导致没有找到Mapper
解决方案
在Application启动类中加
@MapperScan(“cn.jczb.service.jczbserviceprovider.mapper”)
或者在Mapper类中增加@Mapper
这样就启动成功了
原来是因为没有扫描到自己的Mapper,所以要在启动类上加上@MapperScan注解
总结
在SpringBoot中集成MyBatis,可以在mapper接口上添加@Mapper注解,将mapper注入到Spring,但是如果每一给mapper都添加@mapper注解会很麻烦,这时可以使用@MapperScan注解来扫描包。