SpringBoot关于自动注入mapper为空的坑及解决

 更新时间:2023年07月15日 08:40:06   作者:Tokey_W  
这篇文章主要介绍了SpringBoot关于自动注入mapper为空的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

1、初始环境

配置类

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: 数据字典的配置类
 */
@Configuration
//发现映射器--可以动态生产mapper的实现类
@MapperScan(basePackages = "com.sofwin.yygh.mapper")
public class CmnConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // paginationInterceptor.setLimit(你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制);
        return paginationInterceptor;
    }
}

启动类

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

自动注入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

自动注入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() {
        //使用自动注入的话 dictMapper不为空
        test2.test();
    }
}

结果

第一个test失败  空指针异常

第二个test成功  

3、总结 

原因:

第一个test使用new创建的Test1,不是使用spring容器中给创建的Test,

因此没有自动注入DictMapper,所以出现空指针异常

所以:当类中有自动注入的属性的时候,不要使用new创建对象,要使用自动注入的方式,才不会出现mapper为空的情况

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

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

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

    这篇文章主要介绍了Java Collections.sort()排序代码案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • java Jersey框架初体验

    java Jersey框架初体验

    本篇主要是Jersey体验,你将在不做任何编码的情况下,体验Jersey框架的神气魅力!本文还假定你在eclipse里安装了Maven插件
    2016-07-07
  • SpringMVC设置全局异常处理器的步骤

    SpringMVC设置全局异常处理器的步骤

    在项目中我们有需求做一个全局异常处理,来规范所有出去的异常信息,这篇文章主要介绍了SpringMVC设置全局异常处理器,需要的朋友可以参考下
    2024-03-03
  • java 重载(overload)与重写(override)详解及实例

    java 重载(overload)与重写(override)详解及实例

    这篇文章主要介绍了java 重载(overload)与重写(override)详解及实例的相关资料,并附实例代码,需要的朋友可以参考下
    2016-10-10
  • Mybatis-plus多条件筛选分页的实现

    Mybatis-plus多条件筛选分页的实现

    本文主要介绍了Mybatis-plus多条件筛选分页,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • java获取redis日志信息与动态监控信息的方法

    java获取redis日志信息与动态监控信息的方法

    这篇文章主要给大家介绍了关于java如何获取redis日志信息与动态监控信息的方法,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04
  • Java之并行流(Parallel Stream)使用详解

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

    Java并行流(ParallelStream)通过多线程并行处理集合数据,利用Fork/Join框架加速计算,适用于大规模数据集和计算密集型任务,并行流主要通过集合的parallelStream()方法或现有流的parallel()方法创建,适用于数据量大、计算复杂且无状态操作的场景
    2025-03-03
  • Java+swing+Mysql实现商品销售管理系统

    Java+swing+Mysql实现商品销售管理系统

    基础扎不扎实只有在实战中才能显现,本篇文章手把手带你用Java+swing+Mysql实现商品销售管理系统,大家可以在过程中查缺补漏,提升水平
    2022-01-01
  • SpringBoot使用前缀树过滤敏感词的方法实例

    SpringBoot使用前缀树过滤敏感词的方法实例

    Trie也叫做字典树、前缀树(Prefix Tree)、单词查找树,特点:查找效率高,消耗内存大,这篇文章主要给大家介绍了关于SpringBoot使用前缀树过滤敏感词的相关资料,需要的朋友可以参考下
    2022-01-01
  • Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用

    Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用

    前几天Spring Boot 2.5.0发布了,其中提到了关于Datasource初始化机制的调整,有读者私信想了解这方面做了什么调整。那么今天就要详细说说这个重新设计的配置内容,并结合实际情况说说我的理解和实践建议
    2021-05-05

最新评论