加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何修复CrudRepository.save(java.lang.Object)是springboot中

发布时间:2020-12-15 05:13:50 所属栏目:Java 来源:网络整理
导读:我正在考虑这个 springboot教程并在我的项目中使用spring数据,我试图将数据添加到数据库.使用以下内容.当我试图这样做时,我得到一个错误说 Invoked method public abstract java.lang.Object org.springframework.data.repository.CrudRepository.save(java.
我正在考虑这个 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";
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读