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

一、问题描述

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

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

在这里插入图片描述


二、场景还原

1. 测试类

@SpringBootTest
class DemoApplicationTests {
    @Test
    void 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 {
}

四、解决方案

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

    @Autowired
    IUserService userService;

    @Test
    void 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_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荔枝吻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值