java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot关于自动注入mapper为空的坑

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

作者: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为空的情况

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

您可能感兴趣的文章:
阅读全文