【沉浸式解决问题】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
*/