如何修复CrudRepository.save(java.lang.Object)是springboot中的无法访问方法?

前端之家收集整理的这篇文章主要介绍了如何修复CrudRepository.save(java.lang.Object)是springboot中的无法访问方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在考虑这个 springboot教程并在我的项目中使用spring数据,我试图将数据添加数据库.使用以下内容.当我试图这样做时,我得到一个错误

Invoked method public abstract java.lang.Object
org.springframework.data.repository.CrudRepository.save(java.lang.Object)
is no accessor method!

这是我的代码,

//my controller

@RequestMapping("/mode")
    public String showProducts(ModeRepository repository){
        Mode m = new Mode();
        m.setSeats(2);
        repository.save(m); //this is where the error getting from
        return "product";
    }


//implementing crud with mode repository
@Repository
public interface ModeRepository extends CrudRepository<Mode,Long> {

}

 //my mode class
@Entity
@Table(name="mode")
public class Mode implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true,nullable=false)
    private int idMode; 


    @Column(nullable=false)
    private int seats;

    //assume that there are getters and setters
}

我是springboot的新手,有人能说出我做错了什么,
感谢有人能给我一个链接来了解springdata
弹簧文档除外

解决方法

更改控制器代码,以便ModeRepository是私有自动装配字段.

@Autowired //don't forget the setter
    private ModeRepository repository; 

    @RequestMapping("/mode")
    public String showProducts(){
        Mode m = new Mode();
        m.setSeats(2);
        repository.save(m); //this is where the error getting from
        return "product";
    }

猜你在找的Springboot相关文章