SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決
1、初始環(huán)境
配置類(lèi)
package com.sofwin.yygh.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @packageName: com.sofwin.yygh.config
* @author: wentao
* @date: 2022/12/12 17:34
* @version: 1.0
* @email 1660420659@qq.com
* @description: 數(shù)據(jù)字典的配置類(lèi)
*/
@Configuration
//發(fā)現(xiàn)映射器--可以動(dòng)態(tài)生產(chǎn)mapper的實(shí)現(xiàn)類(lèi)
@MapperScan(basePackages = "com.sofwin.yygh.mapper")
public class CmnConfig {
/**
* 分頁(yè)插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// paginationInterceptor.setLimit(你的最大單頁(yè)限制數(shù)量,默認(rèn) 500 條,小于 0 如 -1 不受限制);
return paginationInterceptor;
}
}啟動(dòng)類(lèi)
package com.sofwin.yygh;
import com.sofwin.yygh.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.sofwin")
public class ServicecmnApplication {
public static void main(String[] args) {
SpringApplication.run(ServicecmnApplication.class, args);
}
}mapper接口
package com.sofwin.yygh.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sofwin.yygh.model.cmn.Dict;
import com.sofwin.yygh.model.hosp.HospitalSet;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
/**
* @packageName: com.sofwin.yygh.mapper
* @author: wentao
* @date: 2022/12/12 17:17
* @version: 1.0
* @email 1660420659@qq.com
* @description: Dict的mapper接口
*/
@Repository
public interface DictMapper extends BaseMapper<Dict> {
}目錄

2、測(cè)試
Test1
自動(dòng)注入DictMapper
package com.sofwin.yygh;
import com.sofwin.yygh.mapper.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @packageName: com.sofwin.yygh
* @author: wentao
* @date: 2022/12/21 10:56
* @version: 1.0
* @email 1660420659@qq.com
* @description: TODO
*/
@Component
public class Test1 {
@Autowired
private DictMapper dictMapper;
public void test() {
System.out.println(dictMapper.selectList(null));
}
}Test2
自動(dòng)注入DictMapper
package com.sofwin.yygh;
import com.sofwin.yygh.mapper.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @packageName: com.sofwin.yygh
* @author: wentao
* @date: 2022/12/21 10:56
* @version: 1.0
* @email 1660420659@qq.com
* @description: TODO
*/
@Component
public class Test2 {
@Autowired
private DictMapper dictMapper;
public void test() {
System.out.println(dictMapper.selectList(null));
}
}測(cè)試類(lèi)
package com.sofwin.yygh;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @packageName: com.sofwin.yygh
* @author: wentao
* @date: 2022/12/21 10:57
* @version: 1.0
* @email 1660420659@qq.com
* @description: TODO
*/
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class MapperTest {
@Test
public void test() {
//使用new的話 dictMapper是為空的
Test1 test1 = new Test1();
test1.test();
}
@Autowired
private Test2 test2;
@Test
public void test2() {
//使用自動(dòng)注入的話 dictMapper不為空
test2.test();
}
}結(jié)果


第一個(gè)test失敗 空指針異常
第二個(gè)test成功
3、總結(jié)
原因:
第一個(gè)test使用new創(chuàng)建的Test1,不是使用spring容器中給創(chuàng)建的Test,
因此沒(méi)有自動(dòng)注入DictMapper,所以出現(xiàn)空指針異常
所以:當(dāng)類(lèi)中有自動(dòng)注入的屬性的時(shí)候,不要使用new創(chuàng)建對(duì)象,要使用自動(dòng)注入的方式,才不會(huì)出現(xiàn)mapper為空的情況
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作,結(jié)合實(shí)例形式分析了spring使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)相關(guān)配置、接口設(shè)置與使用技巧,需要的朋友可以參考下2020-01-01
解決maven導(dǎo)入依賴失敗,說(shuō)找不到依賴的問(wèn)題
本文介紹了在多模塊開(kāi)發(fā)項(xiàng)目中遇到Maven依賴導(dǎo)入失敗的問(wèn)題,以及解決該問(wèn)題的步驟,作者通過(guò)手動(dòng)下載jar包并使用Maven命令將其安裝到本地倉(cāng)庫(kù),解決了項(xiàng)目中出現(xiàn)的依賴問(wèn)題2025-11-11
使用maven實(shí)現(xiàn)有關(guān)Jsoup簡(jiǎn)單爬蟲(chóng)的步驟
這篇文章主要介紹了使用maven實(shí)現(xiàn)有關(guān)Jsoup簡(jiǎn)單爬蟲(chóng)的步驟,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
使用SpringBoot集成Redis實(shí)現(xiàn)CRUD功能
本文主要介紹了使用SpringBoot集成Redis實(shí)現(xiàn)CRUD功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-10-10
Java簡(jiǎn)單使用redis-zset實(shí)現(xiàn)排行榜
這篇文章主要介紹了Java簡(jiǎn)單使用redis-zset實(shí)現(xiàn)排行榜,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Java+Freemarker實(shí)現(xiàn)根據(jù)XML模板文件生成Word文檔
這篇文章主要為大家詳細(xì)介紹了Java如何使用Freemarker實(shí)現(xiàn)根據(jù)XML模板文件生成Word文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-11-11
Java 實(shí)戰(zhàn)項(xiàng)目基于遺傳算法學(xué)校排課系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實(shí)現(xiàn)一個(gè)基于遺傳算法的學(xué)校排課系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件
這篇文章主要介紹了java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目
這篇文章主要介紹了Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面
這篇文章主要介紹了jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

