如何在Spring Data Rest流程中使用POST方法?

huangapple go评论62阅读模式
英文:

how to use a post method with a spring data rest process?

问题

我正在使用存储库REST资源服务,目前我需要实现一种方法,将数据从Excel文件导入到我的数据库中。我不清楚如何在Spring存储库REST资源中实现此操作,因为它是一个接口。我应该使用RestController,还是有其他同时实现两者的方法?

英文:

I'm using a repostiroy rest resource service and currently I need to implement a method to import the data from a excel file into my database. I don't see how I can do with spring repository rest resource? since it's an interface. do I use restController or does it have a way to do both?

答案1

得分: 1

标准流程如下:

  1. 创建一个仓库文件
@Repository
public interface RepositoryClass extends JpaRepository<T, ID> {}
  • 其中 T 是类的类型,ID 是 ID 的数据类型(例如:Long、Integer 等)

JpaRepository 已经拥有 save() 和 saveAll() 方法(1

  1. 在服务中创建一个方法,读取数据并将其保存在数据库中
@Service
public class ServiceClass {

   // ...一些代码

   public void importData(// ... 参数) {
       // 打开 Excel 文件并将数据导入 ArrayList 中,例如:

       repositoryClass.saveAll(arrayWithData);
   }
}
  1. 最后一步是在控制器中创建一个端点,调用服务中编写的方法。
英文:

The standard flow would be:

  1. Create a repository file

    @Repository
    public interface RepositoryClass extends JpaRepository&lt;T, ID&gt;{}
    
  • where T is the class and ID is the data type of the ID (ex: Long, Integer etc.)

The JpaRepository already have save() and saveAll() methods(1)

  1. In service create a method that read the data and saves it in the database

    @Service
    public class ServiceClass{
    
    // ...some code
    
       public void importData(// ... parameters){
            // open the excel file and import the data in an ArrayList for exemple
    
            repositoryClass.saveAll(arrayWithData);
       }
    }
    
  2. The last step is to create a endpoint in the controller which calls the method that is written in service.

答案2

得分: 0

那个接口有一个名为 save() 的方法。您可以像这样将给定的对象保存在数据库中: repository.save(myObject)

英文:

That interface have a method called save(). You can save the given object in database like: repository.save(myOjbect)

huangapple
  • 本文由 发表于 2020年10月23日 04:47:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64490323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定