IDEA中spring boot+mybatis+freemarker时单元测试遇到的filed to load问题
系统报错代码
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.example.demo.ServiceImp.UserServiceImpl required a bean of type 'com.example.demo.Mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.Mapper.UserMapper' in your configuration.
1.问题由来
在中IDEA进行测试的时候会报上述代码的错误,翻译成中文就是
com.example.demo.serviceImpl.userserviceimpl需要一种类型的“com.example.demo.mapper.usermapper”,这是无法找到的
意思是spring boot启动的时候没有找到com.example.demo.mapper.usermapper.
2.原因
其实在很多次测试中都出现了这种问题或类似的问题,大概就是springboot启动找不到你创建的bean,也就是没扫描到对应的包
3.解决方法
3.1调整包结构,把@SpringBootApplication 所在类放在其他包的平行层:
如图
把启动类放到与其他包平行的文件位置,这样启动类才能扫描到Mapper里面的内容
3.2在启动类的前面添加@MapperScan(value = “com.example.demo.Mapper”)
如图
添加注解是为了让手动让启动类扫描指定的包,启动类就会去找注解里面value对应的包
4.总结
这种启动类的错误很常见,我在写demo的过程中多次遇见,遂记下博客.
引用
微微风簇浪,散作满河星。