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

问题描述 投票:3回答:2

我正在考虑这个qazxsw poi教程,我在我的项目中使用qazxsw poi,我试图添加springboot。使用以下spring data bt当我试图这样做时,我得到一个错误说

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

这是我的代码,

data to database

我是.的新手,有人可以告诉我我做错了什么,感谢有人能给我一个链接来了解//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

java spring spring-mvc spring-boot spring-data
2个回答
12
投票

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

springdata

1
投票

我今天偶然发现了这个错误。 IntelliJ IDEA告诉我,不鼓励直接进样,这在某种程度上是有道理的。您还可以在@Controller上使用构造函数注入。可能看起来像头顶,但我认为它更干净。

spring documentation
© www.soinside.com 2019 - 2024. All rights reserved.