项目场景:
使用mybatis-plus写一个入门查询代码
问题描述
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.sarkura.MybatisPlusDemoApplicationTests': Unsatisfied dependency expressed through field 'userDao': No qualifying bean of type 'com.sarkura.Mapper.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
找的到bean但注入失败
//Mapper代码
@Mapper
public interface UserDao extends BaseMapper<User> {
}
//测试代码
@SpringBootTest
class MybatisPlusDemoApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void testAllSelect() {
System.out.println(userDao.selectList(null));
}
}
解决方案:
更换springboot版本为2.7.x
网上找遍了也没找到解决方法,@Repository替换@Mapper,启动类加@ComponentScan,依赖重新加载都没用,最后还是版本的问题,一开始用的3.1.1版本,可能有时候用低版本更兼容吧,建议使用mybatis-plus的springboot版本用2.7.x的版本