当前位置: 首页 > news >正文

【沉浸式解决问题】baseMapper can not be null

目录

  • 一、问题描述
  • 二、场景还原
    • 1. 测试类
  • 三、原因分析
  • 四、解决方案

一、问题描述

写了个简单类测试Mybatis Plus的IService的方法,结果报错baseMapper是空的

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: baseMapper can not be null

在这里插入图片描述


二、场景还原

1. 测试类

@SpringBootTest
class DemoApplicationTests {@Testvoid test4() {List<User> userList = new ArrayList<>();for (int i = 0; i < 5; i++) {User user = new User();user.setName("姓名"+i);userList.add(user);}UserServiceImpl userService = new UserServiceImpl();userService.saveBatch(userList,2);}
}

其他的实体类、Mapper接口类、Mapper.xml、IService接口类、IService实现类一应俱全


三、原因分析

baseMapper的文件什么都没有,肯定没有错,那就是获取出了问题,查看UserServiceImpl 看到了它的service注解,想到它依赖的basemapper是由spring注入的,而你直接new创建一个对象,肯定是没有机会注入了,这个对象需要的basemapper就是空了

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
}

四、解决方案

改成用注解注入,记得要注入接口类

    @AutowiredIUserService userService;@Testvoid test4() {List<User> userList = new ArrayList<>();for (int i = 0; i < 5; i++) {User user = new User();user.setName("姓名"+i);userList.add(user);}userService.saveBatch(userList,2);}

喜欢的点个关注吧><!祝你永无bug!

/*_ooOoo_o8888888o88" . "88(| -_- |)O\  =  /O____/`---'\____.'  \\|     |//  `./  \\|||  :  |||//  \/  _||||| -:- |||||-  \|   | \\\  -  /// |   || \_|  ''\---/''  |   |\  .-\__  `-`  ___/-. /___`. .'  /--.--\  `. . __."" '<  `.___\_<|>_/___.'  >'"".| | :  `- \`.;`\ _ /`;.`/ - ` : | |\  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^佛祖保佑       永无BUG
*/
http://www.dtcms.com/a/254788.html

相关文章:

  • 【世纪龙科技】智能网联汽车自动驾驶虚拟实训软件
  • JS红宝书笔记 8.2 创建对象
  • Mybatis之Integer类型字段为0,入库为null
  • Spring-创建第一个SpringBoot项目
  • html实现登录与注册功能案例(不写死且只使用js)
  • Ubuntu编译ffmpeg解决错误:ERROR: avisynth/avisynth_c.h not found
  • Kafka性能压测报告撰写
  • Vue3中使用 Vue Flow 流程图方法
  • 103. 2017年蓝桥杯省赛 - 日期问题(困难)- 暴力枚举
  • (哈希)128. 最长连续序列
  • 华为ModelArts详解
  • 使用 mysql2/promise 模块返回以后,使用 await 返回数据总结
  • 时序数据库概念及IoTDB特性详解
  • C++位图
  • FPGA基础 -- Verilog 命名事件
  • Debian配置Redis主从、哨兵
  • Rsync+sersync实现数据实时同步(小白的“升级打怪”成长之路)
  • C++实现异步(重叠)管道通信
  • GameFormer论文阅读
  • 46道Jenkins高频题整理(附答案背诵版)
  • 什么是Nacos?
  • Docker+Jenkins+git实现Golang项目自动部署
  • Git(三):分支管理
  • 深入理解Zephyr Manifest:现代嵌入式开发的项目管理利器
  • 华为云Flexus+DeepSeek征文|基于华为云Flexus云服务的CCE容器高可用部署Dify-LLM应用开发平台
  • 车载学习(8)——CAPL与诊断相关
  • Ubuntu 安装Telnet服务
  • C#的泛型和匿名类型
  • 语音情感识别:CNN-LSTM 和注意力增强 CNN-LSTM 模型的比较分析
  • 在 Windows 上使用 Docker Desktop 快速搭建本地 Kubernetes 环境(附详细部署教程)