qq_33204081
默默Silence
采纳率100%
2020-05-07 18:16

Springboot,使用@Autowired疑似注入失败,报空指针错误

项目结构:
图片说明

User类:

package com.test.mp.mp1111.entity;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
public class User {

//    @TableId(type = IdType.AUTO)

    private Long id;

    private Integer age;

    private String name;

    private String email;
}

UserMapper:

package com.test.mp.mp1111.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.mp.mp1111.entity.User;
import lombok.Data;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component
public interface UserMapper extends BaseMapper<User> {

}

Mp1111ApplicationTests:

package com.test.mp.mp1111;

import com.test.mp.mp1111.entity.User;


import com.test.mp.mp1111.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;


@SpringBootTest
class Mp1111ApplicationTests {



    @Autowired
    private UserMapper userMapper;

    @Test
    public void testUpdateById() {

        if (userMapper == null) {
            System.out.println("空指针");
        }
    }
}

项目pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test.mp</groupId>
    <artifactId>mp1111</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mp1111</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--lombok用来简化实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我运行Mp1111ApplicationTests中的testUpdateById方法,
显示userMapper为空指针,这是为什么呀

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • LM5463640 请叫我七点起床 1年前

    @Component改成@Mapper或者在springboot的启动类上加@MapperScan("包路径")注解试试

    点赞 1 评论 复制链接分享