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 IO复用_动力节点Java学院整理

    Java IO复用_动力节点Java学院整理

    这篇文章主要介绍了Java IO复用的相关知识,非常不错,具有参考借鉴价值,需要的的朋友参考下吧
    2017-05-05
  • idea运行jsp文件的时候显示404问题及解决

    idea运行jsp文件的时候显示404问题及解决

    这篇文章主要介绍了idea运行jsp文件的时候显示404问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • Spring的@Scope注解作用解析

    Spring的@Scope注解作用解析

    这篇文章主要介绍了Spring的@Scope注解作用解析,@Scope注解用于设置实例的作用域,默认值是单实例,即当IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取,需要的朋友可以参考下
    2023-11-11
  • Java实时监控日志文件并输出的方法详解

    Java实时监控日志文件并输出的方法详解

    这篇文章主要给大家介绍了关于Java实时监控日志文件并输出的方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • 利用Java的MyBatis框架获取MySQL中插入记录时的自增主键

    利用Java的MyBatis框架获取MySQL中插入记录时的自增主键

    这篇文章主要介绍了利用Java的MyBatis框架获取MySQL中插入记录的自增长字段值,其中大家可以看到MyBatis支持普通SQL语句所带来的遍历,需要的朋友可以参考下
    2016-06-06
  • Spring Boot整合RabbitMQ开发实战详解

    Spring Boot整合RabbitMQ开发实战详解

    这篇文章主要介绍了Spring Boot整合RabbitMQ开发实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • IDEA引入本地jar包的几种方法

    IDEA引入本地jar包的几种方法

    本文主要介绍了IDEA引入本地jar包的几种方法,文中通过图文结合的方式码介绍的非常详细,对大家的学习或工作有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2024-01-01
  • IDEA 搭建maven 安装、下载、配置的图文教程详解

    IDEA 搭建maven 安装、下载、配置的图文教程详解

    这篇文章主要介绍了IDEA 搭建maven 安装、下载、配置,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Spring AOP使用接口方式实现

    Spring AOP使用接口方式实现

    本文主要介绍了Spring AOP使用接口方式实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • ZooKeeper开发实际应用案例实战

    ZooKeeper开发实际应用案例实战

    这篇文章主要为大家介绍了ZooKeeper开发的实际应用案例实战,文中附含详细开发应用源码,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-01-01

最新评论