티스토리 뷰

In this post, i just made a super beginner project using JpaRepository.

Before we start, let me tell the version and dependency in this project.

 

🟩 Version and Dependency in this project

(Version)

  • Spring Boot : 4.0.1
  • java : 21
  • Packaging : Jar
  • Confiquration : YAML, Gradle-groovy

(Dependency)

  • Spring Web
  • Spring Data Jpa
  • H2 Database
  • Lombok 

🟩 Project Descirption

(It's really simple haha)

 

This is a project structure of this post.

I just want to build a blog application(?), so i tried this.

 

In this project, we will make 4 functions, saving article, fetch all article, updating article, deleting article.

 

🟩 Implementation project 

At first, create a Entity that named as Article. 

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotBlank
    private String title;

    @NotBlank
    private String content;

}

 

And, create a Repository.

@Repository
public interface ArticleRepository extends JpaRepository<Article, Long> {
    // 
}

 

In this post, i will not deep dive into "how to use JpaRepository".

I will get into it later!! for more bigger project soon!

 

 

 

And create Service. 

I seperate service code by using interface.

I think, when i add more Entity, i will use methods for that Entity same with Article Entiry.

So, i use interface so that i can lay the foundation.

@org.springframework.stereotype.Service
public interface Service {
    Article save(Article article);
    List<Article> fetchAll();
    Article update(Long id, Article article);
    void delete(Long id);
}
@org.springframework.stereotype.Service
public class ServiceImpl implements Service {

    private ArticleRepository articleRepository;

    @Autowired
    public ServiceImpl(ArticleRepository articleRepository) {
        this.articleRepository = articleRepository;
    }


    @Override
    public Article save(Article article) {
        return articleRepository.save(article);
    }

    @Override
    public List<Article> fetchAll() {
        return articleRepository.findAll();
    }

    @Override
    public Article update(Long id, Article articleForUpdate) {
        Article article = articleRepository.findById(id).get(); // FIXME
        article.setTitle(articleForUpdate.getTitle());
        article.setContent(articleForUpdate.getContent());
        return articleRepository.save(article);
    }

    @Override
    public void delete(Long id) {
        articleRepository.deleteById(id);
    }

}

 

 

 

And finally, Controller code.

@RestController
public class Controller {

    private Service service;

    @Autowired
    public Controller(Service service) {
        this.service = service;
    }

    @PostMapping("/article")
    public Article save(
            @Valid @RequestBody Article article
    ) {
        return service.save(article);
    }

    @GetMapping("/article")
    public List<Article> fetchAll() {
        return service.fetchAll();
    }

    @PutMapping("/article/{id}")
    public Article updateContent(
            @PathVariable Long id,
            @Valid @RequestBody Article article
    ) {
        return service.update(id, article);
    }

    @DeleteMapping("/article/{id}")
    public void delete(
            @PathVariable Long id
    ) {
        service.delete(id);
    }

}

 

 

okay. 

Let's execute it!

 

🟩 Request with Postman!!

🟦  Save a article

 

 

 

 

 

Now, we've saved three articles here.

 

🟦 fetch all articles

 

 

🟦 Update a article by id.

 

I updated the content, "eat proteins" from "try pushUp and pullUp", at the id-2 article.

 

🟦 Delete article by id

 

I deleted article, id-1.

 

 

 

That's it.

Of course, there is a lot of problems (Not use DTO, using getter at the service layer etc.....).

So, project, i will make a more upgraded project than this...

 

 

---------------------------------------

I'm an university student soon.........................!!!!! 

 

 

 

 

 

 

reference)

https://www.geeksforgeeks.org/springboot/spring-boot-jparepository-with-example/

 

Spring Boot JpaRepository with Example - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

www.geeksforgeeks.org

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
글 보관함