10 Springboot 注解无效,报空指针错误。

发布于 2020-05-07 18:00:56

项目结构:
image.png

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;
}

image.png

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>

运行testUpdateById方法后,显示空指针错误,为什么userMapper是空指针呢

查看更多

关注者
0
被浏览
1k
zhangsan
zhangsan 2020-05-07

测试类是不是少了runwith注解 @RunWith(SpringRunner.class)

7 个回答
申来来
申来来 认证专家 2020-05-07
年轻是本钱,但不努力就不值钱

把UserMapper上的注解换成@Mapper,如果不可以,请留言,我到家后仔细看

857857
857857 2020-05-07

可以按照楼上的说法,不过我建议开发的话直接在@SpringBootApplication注解下加个@MapperScan(“你的mapper路径”)image.png

monster
monster 2020-05-07

可以换成@Repository注解试一下

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览