# H2Databaseの問い合わせと戻り値について ## DB接続の@Repoository(アノテーション)について @Repositoryを使う理由は、簡単に言うとメインのクラスからDB接続をするのは好ましくないので、DB操作クラスとして使うものに@Repositoryを指定します。 以前自分が紹介したDBを使ったWebアプリのコードをもう1回使って説明します。 ``` package com.example.demo.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.demo.model.Comment; @Repository public interface CommentRepository extends JpaRepository <Comment, Long> { } ``` 自分が使っているJPAで使う際は基本的にこのように@Repositoryを指定する場合は単体でクラスを作成します。 また、コードをみると public interface CommentRepository extends JpaRepository <Comment, Long> とありますが、普通は今回のinterfaceとあるところはclassで指定すると思います。interfaceとは簡単に言うと、できることだけを定義して具体的なことは実装クラスに任せます。その結果使う側は中で実装されたものを気にせず使うことができます。 またinterfaceの後はこのファイルの名前で、extendsはその後ろにあるクラスを継承して作っています。今回継承しているのは、JpaRepositoryというクラスです。 その後ろの<>で囲まれているところは左側にエンティティのクラス名を、右側にはIDのフィールドタイプを記入します。 たったこれだけでメインクラスからメソッドとしてSQLを実行することができます。 ``` package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org. springframework.validation. annotation. Validated; import org. springframework.web.bind.annotation.GetMapping; import org. springframework.web.bind.annotation.ModelAttribute; import org. springframework.web.bind.annotation.PostMapping; import com.example.demo.model.Comment; import com.example.demo.repository.CommentRepository; @Controller public class CommentController { private final CommentRepository repository; //@Autowired ← コンストラクタが1つの場合、@Autowiredは省略できます public CommentController(CommentRepository repository) { this.repository = repository; } @GetMapping("/") public String getAllComments(@ModelAttribute Comment comment, Model model) { // COMMENTテーブル:レコード全件取得 model.addAttribute("comments", repository.findAll()); return "list"; } @PostMapping("/add") public String addComment(@Validated @ModelAttribute Comment comment, BindingResult result, Model model) { model.addAttribute("comments", repository.findAll()); if (result.hasErrors()) { return "list"; } // COMMENTテーブル:コメント登録 repository.save(comment); // ルートパス("/") にリダイレクトします return "redirect:/"; } @GetMapping("/delete") public String deleteComment(@Validated @ModelAttribute Comment comment, BindingResult result, Model model) { model.addAttribute("comments", repository.findAll()); // COMMENTテーブル:レコード削除 repository.deleteById(comment.getId()); // ルートパス("/") にリダイレクトします return "redirect:/"; } } ``` これのaddAttributeをみるとfindAll()(SQL文の代わりとなるメソッドの1つ)の前にrepositoryがあります。これは@Autowiredの下のコンストラクタでRepositoryのクラスから呼び出しているのでfindAll等は全部JpaRepositoryクラスの中に入っています。 なのでまとめるとJPArepositoryを他のクラスで継承しておいてメインのクラスでそのクラスを呼び出すことによってDBからデータを受け渡しできます。 ## SQL文の代わりに使うメソッドについて 先ほど例に挙げたようにJPAはSQL文を直接かくのではなく、メソッドとして呼び出しています。 よく使うメソッドを表にだしておきます  [引用元](https://dev.classmethod.jp/articles/use_spring-boot_jpql-query-create/) ## メソッドだけのだと自分がやりたいようなDBの操作ができない場合 JPQLやSQL文をエンティティに指定する方法もあるがまだ詳しく調べてないため今回は割愛 ## まとめ 難しい 以上 まとまった時間が欲しい
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up