用springboot开发web服务端,同时使用mybatis、swagger。项目目录如下:
首先是DAO包中的mapper文件无法扫描到,mybatis-config中已经相关配置:
<mappers>
<mapper resource="Mapper/*.xml" />
<package name="com.storm.datemanager.DAO"/>
</mappers>
运行失败后报如下错误:
Description:
Field userInfoMapper in com.storm.datemanager.Service.UserInfoService required a bean of type 'com.storm.datemanager.DAO.UserInfoMapper' 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 'com.storm.datemanager.DAO.UserInfoMapper' in your configuration.
其次,在通过swagger显示controller时为空,url访问controller时报404错误,应该也是没有扫描到Controller文件夹controller代码如下
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员模块")
public class AdminController {
@Autowired
private UserInfoService userInfoService;
@ApiOperation(value = "获取所有用户")
@GetMapping("/fetchAllUsers")
public Result<List<UserInfo>> fetchAllUsers(@ApiParam("管理员ID")@RequestParam String Id){
return userInfoService.fetchAllUser(Id);
}
}
后想再次使用componentscan时,仍然报错找不到DAO中的mapper
@SpringBootApplication
@EnableSwagger2
@ComponentScans(value = {
@ComponentScan("com.storm.datemanager.DAO"),
@ComponentScan("com.storm.datemanager.Controller")
})
public class DatemanagerApplication {
public static void main(String[] args) {
SpringApplication.run(DatemanagerApplication.class, args);
}
}
据说springboot的包扫描机制是扫描启动类所在的包以及其子包的所有文件,那么理论上来说这样的目录结构是不需要ComponentScan的,可是为何ComponentScan mapper文件可行,而ComponentScans两个文件夹就不可以?
求大佬解惑