亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決

 更新時(shí)間:2023年07月15日 08:40:06   作者:Tokey_W  
這篇文章主要介紹了SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1、初始環(huán)境

配置類

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ù)字典的配置類
 */
@Configuration
//發(fā)現(xiàn)映射器--可以動(dòng)態(tài)生產(chǎn)mapper的實(shí)現(xiàn)類
@MapperScan(basePackages = "com.sofwin.yygh.mapper")
public class CmnConfig {
    /**
     * 分頁插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // paginationInterceptor.setLimit(你的最大單頁限制數(shù)量,默認(rèn) 500 條,小于 0 如 -1 不受限制);
        return paginationInterceptor;
    }
}

啟動(dòng)類

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、測試

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));
    }
}

測試類

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,

因此沒有自動(dòng)注入DictMapper,所以出現(xiàn)空指針異常

所以:當(dāng)類中有自動(dòng)注入的屬性的時(shí)候,不要使用new創(chuàng)建對象,要使用自動(dòng)注入的方式,才不會(huì)出現(xiàn)mapper為空的情況

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java Collections.sort()排序代碼案例

    Java Collections.sort()排序代碼案例

    這篇文章主要介紹了Java Collections.sort()排序代碼案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • java Jersey框架初體驗(yàn)

    java Jersey框架初體驗(yàn)

    本篇主要是Jersey體驗(yàn),你將在不做任何編碼的情況下,體驗(yàn)Jersey框架的神氣魅力!本文還假定你在eclipse里安裝了Maven插件
    2016-07-07
  • SpringMVC設(shè)置全局異常處理器的步驟

    SpringMVC設(shè)置全局異常處理器的步驟

    在項(xiàng)目中我們有需求做一個(gè)全局異常處理,來規(guī)范所有出去的異常信息,這篇文章主要介紹了SpringMVC設(shè)置全局異常處理器,需要的朋友可以參考下
    2024-03-03
  • java 重載(overload)與重寫(override)詳解及實(shí)例

    java 重載(overload)與重寫(override)詳解及實(shí)例

    這篇文章主要介紹了java 重載(overload)與重寫(override)詳解及實(shí)例的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下
    2016-10-10
  • Mybatis-plus多條件篩選分頁的實(shí)現(xiàn)

    Mybatis-plus多條件篩選分頁的實(shí)現(xiàn)

    本文主要介紹了Mybatis-plus多條件篩選分頁,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java獲取redis日志信息與動(dòng)態(tài)監(jiān)控信息的方法

    java獲取redis日志信息與動(dòng)態(tài)監(jiān)控信息的方法

    這篇文章主要給大家介紹了關(guān)于java如何獲取redis日志信息與動(dòng)態(tài)監(jiān)控信息的方法,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Java之并行流(Parallel Stream)使用詳解

    Java之并行流(Parallel Stream)使用詳解

    Java并行流(ParallelStream)通過多線程并行處理集合數(shù)據(jù),利用Fork/Join框架加速計(jì)算,適用于大規(guī)模數(shù)據(jù)集和計(jì)算密集型任務(wù),并行流主要通過集合的parallelStream()方法或現(xiàn)有流的parallel()方法創(chuàng)建,適用于數(shù)據(jù)量大、計(jì)算復(fù)雜且無狀態(tài)操作的場景
    2025-03-03
  • Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng)

    Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng)

    基礎(chǔ)扎不扎實(shí)只有在實(shí)戰(zhàn)中才能顯現(xiàn),本篇文章手把手帶你用Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2022-01-01
  • SpringBoot使用前綴樹過濾敏感詞的方法實(shí)例

    SpringBoot使用前綴樹過濾敏感詞的方法實(shí)例

    Trie也叫做字典樹、前綴樹(Prefix Tree)、單詞查找樹,特點(diǎn):查找效率高,消耗內(nèi)存大,這篇文章主要給大家介紹了關(guān)于SpringBoot使用前綴樹過濾敏感詞的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Spring Boot 2.5.0 重新設(shè)計(jì)的spring.sql.init 配置有啥用

    Spring Boot 2.5.0 重新設(shè)計(jì)的spring.sql.init 配置有啥用

    前幾天Spring Boot 2.5.0發(fā)布了,其中提到了關(guān)于Datasource初始化機(jī)制的調(diào)整,有讀者私信想了解這方面做了什么調(diào)整。那么今天就要詳細(xì)說說這個(gè)重新設(shè)計(jì)的配置內(nèi)容,并結(jié)合實(shí)際情況說說我的理解和實(shí)踐建議
    2021-05-05

最新評論