笔者尝试了网上的各种方法,mapper映射也没问题,注解写的也没问题,最后败在了依赖冲突上,原因是用SpringBoot3.x.x 搭配了 MyBatis2.x.x 使用,谨以此文章告诫自己不要再犯这种低级错误。
已纠正:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
另:注意在启动类前加上 @MapperScan 注解
@MapperScan("com.coder.community.dao")
原错误警告见下:
2025-03-30T02:19:18.668+08:00 WARN 25416 --- [community] [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'userMapper' defined in file [D:\Java\toWork\niu\community\target\classes\com\coder\community\dao\UserMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)