今天在测试mybatis-plus服务层接口时发现,IService中的方法用自动装配的对象可以调用成功,但是用我自己new 出来的对象来调用会报下面的错误
java.lang.NullPointerException: Cannot invoke "com.baomidou.mybatisplus.core.mapper.BaseMapper.selectCount(com.baomidou.mybatisplus.core.conditions.Wrapper)" because the return value of "com.baomidou.mybatisplus.extension.service.IService.getBaseMapper()" is null
请问有大佬知道是为啥吗,感谢🙏
相应代码如下:
//继承mybatis-plus服务层的接口
public interface UserService extends IService<User> {
}
//接口的实现类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
@SpringBootTest
class UserServiceTest {
UserService userService = new UserServiceImpl(); //使用自己new的对象会报错
// @Autowired //自动装配正常输出
// private UserService userService;
@Test
public void testGetCount(){
//查询总记录数
//执行的SQL为:SELECT COUNT( * ) FROM user
long count = userService.count();
System.out.println("总记录数:" + count);
}
}