这篇博客记录了我刚学mybatis时遇到的一些错误和解决方法
第一个问题:mysql驱动包没用
解决方法:
在pom文件中指定jar包的版本号
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.31</version> <scope>runtime</scope> </dependency>
第二个问题:没法注解
原因:
@Mapper注解是Mybatis提供的,而@Autowried注解是Spring提供的,可能idea没法识别mybatis的注解。
解决方法:
将@Autowried改为@Resource
因为@Resource注解是J2EE提供的,这样就不会出现警告了。
第三个问题:没有扫描到mapper包
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.dc.utils.Demo1ApplicationTests': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dc.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
解决方法:
在测试的主方法上添加扫描mapper包的注解。
@MapperScan("com.dc.mapper")