查了百度很久,看过很多博客文章终于在一位的文章上看到解决办法
参考文章:https://blog.csdn.net/ypp91zr/article/details/82713358
错误信息如下
Description: Field userMapper in com.zhku.demo.service.UserServiceImpl required a bean of type 'com.zhku.demo.dao.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 'com.zhku.demo.dao.UserMapper' in your configuration. Process finished with exit code 11、检查dao层的接口类是不是没有加@mapper注解
@Mapper public interface UserMapper { List<User> findAll(); }或者在springBoot的启动类里面添加@MapperScan(value=“”)
@MapperScan("com.zhku.demo.dao") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }2、检查pom.xml的jar包依赖 我的错误就是这个,弄了一下午,我的原因是一开始@Mapper报错,因为没导入jar包,然后我是按下图那样添加jar包依赖 然后idea自动导入的依赖是(错误):
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.1</version> </dependency>上面的jar包是不对的,应该把上面的jar包依赖替换为下面的,即可正确运行:
<!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>注意,不论是上面还是下面的jar包,@Mapper注解需要导入的类都是,所以我花了很多时间都没发现问题所在
import org.apache.ibatis.annotations.Mapper;