最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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)文章

最新評(píng)論

渝中区| 红桥区| 东城区| 彩票| 闵行区| 五华县| 华亭县| 浑源县| 新田县| 越西县| 芒康县| 大理市| 台中县| 综艺| 奈曼旗| 库伦旗| 额敏县| 平顺县| 峨边| 抚宁县| 平定县| 安宁市| 南部县| 容城县| 楚雄市| 华容县| 安图县| 大厂| 昌宁县| 南召县| 宜春市| 高阳县| 哈巴河县| 土默特右旗| 晋江市| 乌兰察布市| 鹤庆县| 襄垣县| 上蔡县| 垣曲县| 宁津县|