MyBatis-Plus | selectById查询 NullPointerException: null 问题

目录

问题产生

问题分析

Yaml配置分析

控制层代码分析

Dao(Mapper)分析

实体类分析

启动类分析

Maven


问题产生

问题分析

Yaml配置分析

检查数据源和MyBatis-Plus配置: 确保Spring Boot应用的application.propertiesapplication.yml文件中正确配置了数据源和MyBatis-Plus相关配置。、

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: 数据库用户名
    password: 数据库密码

mybatis-plus:
  configuration:
    #这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    call-setters-on-nulls: true
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
      id-type: auto

控制层代码分析

    public Users SelectUserAll(Integer userId) {
        Users users = userMapper.selectById(userId);
        return users;
    }

Dao(Mapper)分析

检查Mapper接口定义: 确保UsersMapper接口正确地继承了BaseMapper<Users>。这将自动为你的Users实体类提供selectById方法。

@Mapper
public interface UsersMapper extends BaseMapper<Users> { }

实体类分析

检查实体类和字段注解: 确保Users实体类的id字段上有@TableId注解,这是selectById方法识别主键所必需的。

package com.takem.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;


@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName("users")
public class Users {
    // 这是关键 只要认识到主要id才行
    @TableId
    @TableField(value = "user_pid")
    private Integer userPid;

    @TableField(value = "user_name")
    private String userName;

    @TableField(value = "user_password")
    private String userPassword;

    @TableField(value = "user_role")
    private String userRole;

    @TableField(value = "user_register_time")
    private String userRegisterTime;

}

启动类分析

检查包扫描: 确认在Spring Boot应用的启动类上使用了@MapperScan注解来指定UsersMapper所在的包。

package com.takem;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// 这里很是关键
@MapperScan("com.takem.mapper")
public class TakeMApplication {

    public static void main(String[] args) {
        SpringApplication.run(TakeMApplication.class, args);
    }

}

Maven

清理并重新构建项目: 在IDE中进行项目清理(Clean Project)和重新构建(Rebuild),以确保所有更改都被正确编译和加载。

(到底啦~可关注 vx 公棕号 : wmcode 帮你解决更多问题)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小Mie不吃饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值