如何修复CrudRepository.save(java.lang.Object)在springboot中没有访问器方法?


问题内容

我参考本springboot教程,并spring data在我的项目中使用即时通讯,即时通讯试图添加data to database.我尝试这样做时使用以下bt我收到一条错误消息

调用的方法public abstract java.lang.Object
org.springframework.data.repository.CrudRepository.save(java.lang.Object)不是访问器方法!

这是我的代码,

//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和可有人Tellme公司什么我做错了,感激,如果有人能提供给我一个链接,更多地了解springdata
比其他spring documentation


问题答案:

更改您的控制器代码,以便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";
    }