SpringBoot在启动项目的时候遇到了以下情况:
APPLICATION FAILED TO START
***************************
Description:
Field sysUserDao in com.iamapsycho.service.impl.SysUserServiceImpl required a bean of type 'com.iamapsycho.dao.SysUserDao' that could not be found.
Action:
Consider defining a bean of type 'com.iamapsycho.dao.SysUserDao' in your configuration.
SpringBoot启动失败,告诉我Bean配置失败,为什么报错呢?
Controller:
package com.iamapsycho.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.iamapsycho.entity.SysUser;
import com.iamapsycho.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Controller
@Api(value = "sysUser接口")
@RequestMapping("/sysuser")
public class SysUserController {
@Autowired
SysUserService sysUserService;
@ResponseBody
@RequestMapping(value="/getList", method = { RequestMethod.GET, RequestMethod.POST })
@ApiOperation(value="获取用户列表", notes="用户列表")
public List<SysUser> getList(){
List<SysUser> list = sysUserService.getList();
return list;
}
}
Service:
package com.iamapsycho.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iamapsycho.dao.SysUserDao;
import com.iamapsycho.entity.SysUser;
import com.iamapsycho.service.SysUserService;
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
SysUserDao sysUserDao;
@Override
public List<SysUser> getList() {
return sysUserDao.getList();
}
}
Dao:
package com.iamapsycho.dao;
import java.util.List;
import com.iamapsycho.entity.SysUser;
public interface SysUserDao {
List<SysUser> getList();
}
在网上看到网友说要用@Mapper注解,这才把问题解决了 ,至于具体原因,需要通过文档来解释。
解决方案一:
Dao层
添加:@Mapper
package com.iamapsycho.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.iamapsycho.entity.SysUser;
@Mapper
public interface SysUserDao {
List<SysUser> getList();
}
解决方案二(强烈建议使用):
Application(启动类)
添加:@MapperScan(value = “com.iamapsycho.dao”)
package com.iamapsycho;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(value = "com.iamapsycho.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
作者:iamapsycho
来源:CSDN
原文:https://blog.csdn.net/ampsycho/article/details/86243817
版权声明:本文为博主原创文章,转载请附上博文链接!