开发者社区 问答 正文

usersMapper会???? ?

1.在springboot项目下的UsersServiceImpl如果去掉@Autowired注解usersMapper变红,不加@Autowired注解 usersMapper为null
2.

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
private UsersMapper usersMapper;
    @Override
    public void addUser(Users users) {
        this.usersMapper.insertUser(users);
    }
    @Override
    public List<Users> findUserAll() {
        return this.usersMapper.selectUsersAll();
    }
}

展开
收起
爱吃鱼的程序员 2020-08-21 15:14:24 389 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    在privateUsersMapperusersMapper加上@Autowired注解,在IDEA编译器中usersMapper会报红线,如下所示

    这只是idea自身检测对你的提示,并不影响程序运行,因为本身是没有错误的。
    解决办法:1.在你的UserMapper类上声明注解@Repository
    2.去除idea报红线的方法,参考:https://blog.csdn.net/qq_35387940/article/details/90233004

    privateUsersMapperusersMapper;这个写在这里,啥都不做,为什么不为null?你也没有给它创建对象呢,如果你希望从spring拿这个对象,你也要告诉他你要拿,spring才会帮你传进来对象的。你要先明白这个@Autowired注解是干嘛的。推荐你先去学spring的基础知识。

    是加了@Autowired,usersMapper注入失败了?换成@Resouce注入试试,再检查一下有没有扫描mapper.xml的映射文件,或者在UsersMapper接口文件上加@Mapper注解

    你的UserMapper应该是加了注解或者配置托管给spring容器的,那么在使用UserMapper的时候,如果要用到托管给Spring的对象,那么就需要在前面加@Autowired注解,不加这个注解的话,privateUsersMapperusersMapper这句话就相当于只是申明了一个UserMapper对象,并没有实例化,所以userMapper自然就为空,直接使用的时候也就会提示错误,以红色显示。

    2020-08-21 15:22:39
    赞同 展开评论
问答分类:
问答地址: