Unsatisfied dependency expressed through field 'baseMapper',XXXMapper包掃描不到
-
當(dāng)你看到這樣的報錯,你會怎么解決呢:
Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.memory.memoryiconbackend.mapper.WallpaperMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
-
這個報錯信息大致意思是,未掃描到你的XXXMapper包,項目啟動失敗
-
這個問題可謂最常見了,剛剛我就又被這個問題惡心到了,網(wǎng)上查了半天,感覺他們都是一知半解
-
那么我是怎么解決這個問題的呢?思路如下:
XXXMapper.xml配置錯誤
-
檢查resource下的XXXMapper.xml配置,檢查實體類掃描和mapper掃描路徑是否正確:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
? ? ? ?PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
? ? ? ?"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.memory.memoryiconbackend.mapper.WallpaperMapper">
?
? ?<resultMap id="BaseResultMap" type="com.memory.memoryiconbackend.model.Wallpaper">
? ? ? ? ? ?<result property="id" column="id" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="name" column="name" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="url" column="url" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="type" column="type" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="tags" column="tags" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="createTime" column="create_time" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="updateTime" column="update_time" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="isDelete" column="is_delete" jdbcType="VARCHAR"/>
? ? ? ? ? ?<result property="userId" column="user_id" jdbcType="VARCHAR"/>
? ?</resultMap>
?
? ?<sql id="Base_Column_List">
? ? ? id,name,url,
? ? ? type,tags,create_time,
? ? ? update_time,is_delete,user_id
? ?</sql>
</mapper>
-
確保XXXMapper包的掃描路徑正確后,再繼續(xù)排查:
XXXMapper上添加@Mapper注解
-
檢查XXXMapper上是否添加了@Mapper注解:
文章來源:http://www.zghlxwxcb.cn/news/detail-718524.html
@Mapper public interface WallpaperMapper extends BaseMapper<Wallpaper> { ? }
-
如果這兩部還沒有解決你的問題,請一定繼續(xù)往下看:
開啟@MapperScan注解
-
@MapperScan注解是干嘛的呢?它是用來在項目啟動后,掃描你的XXXMapper所在路徑,用法如下:
@SpringBootApplication @MapperScan("com.memory.memoryiconbackend.mapper.WallpaperMapper") public class MemoryIconBackendApplication { ? ?public static void main(String[] args) { ? ? ? ?SpringApplication.run(MemoryIconBackendApplication.class, args); ? } }
-
那這個注解跟上面提到的@Mapper注解,功能不是一樣的嗎?都是將XXXMapper標(biāo)注為一個Bean,交給Spring管理
-
沒錯,這兩個注解的作用是可以說是一摸一樣的,無非就是寫的位置不一樣
-
正是因為這兩個注解作用是一樣的,所以在開發(fā)過程中,這兩個注解寫一個就行,而且只能寫一個,否則會報錯
-
網(wǎng)上總會有蠢蛋,說在XXXMapper上,添加了@Mapper注解之后,一定不要忘了在啟動類上添加@MapperScan注解
-
這種方法肯定解決不了問題,是大錯特錯的
-
所以,如果你已經(jīng)在XXXMapper上添加了@Mapper注解,一定記得刪除啟動類上的@MapperScan注解
-
如果到這里,你已經(jīng)按照上面的方法解決了問題,成功啟動了項目,恭喜你;如果仍舊報錯,請繼續(xù)往下看:
MybatisPlusConfig配置
-
我們在項目中,導(dǎo)入了MybatisPlus依賴之后,總會寫一個MybatisPlusConfig的分頁配置:
/** * mybatis-plus 分頁配置類 */ @Configuration @MapperScan("com.memory.memoryiconbackend.mapper.WallpaperMapper") public class MybatisPlusConfig { ? ?@Bean ? ?public MybatisPlusInterceptor mybatisPlusInterceptor() { ? ? ? ?MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); ? ? ? ?interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); ? ? ? ?return interceptor; ? } }
-
如果你的問題沒有解決,一定是因為在這個配置類上邊,寫上了@MapperScan注解
文章來源地址http://www.zghlxwxcb.cn/news/detail-718524.html
-
而這個注解的作用,跟啟動類上的@MapperScan注解的作用是一模一樣的,刪除它就好了
總結(jié)
-
如果你已經(jīng)在XXXMapper上添加了@Mapper注解,請把啟動類和MybatisPlusConfig配置類上的@MapperScan注解刪除
-
如果你已經(jīng)在啟動類和MybatisPlusConfig配置類上添加了@MapperScan注解,請把XXXMapper上的@Mapper注解刪除
- 希望這篇文章對你有幫助,感謝您的支持!??
-
到了這里,關(guān)于解決啟動SpringBoot項目報錯:Unsatisfied dependency expressed through field ‘baseMapper‘.....問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!