如何修复 Springboot 中的 CrudRepository.save(java.lang.Object) is no accessor 方法? [英] how to fix the CrudRepository.save(java.lang.Object) is no accessor method in springboot?
问题描述
我参考了这个 springboot
教程,我在我的项目中使用了 spring data
,我试图将 data 添加到数据库
.使用以下 .
bt 当我尝试这样做时,我收到一个错误说
im reffering to this springboot
tutorial and im using spring data
in my project, im trying to add data to database
. using the following .
bt when im trying to do that i get an error saying
调用方法 public abstract java.lang.Objectorg.springframework.data.repository.CrudRepository.save(java.lang.Object)不是访问器方法!
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
除了 spring 文档
im new to springboot
and can someone tellme what am i doing wrong,
appreciate if someone could provide me a link to get to know about springdata
other than spring documentation
推荐答案
更改控制器代码,使 ModeRepository 成为私有的自动装配字段.
Change your controller code so that the ModeRepository is a private autowired field.
@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 中的 CrudRepository.save(java.lang.Object) is no accessor 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!