Spring BootのJPA周りとかでつまずいたところのメモ
メモ
- JPA /JPQLのつまずきポイント
- 
@QueryのFrom句は、ドメインクラスの@Table(name=xxx)の指定とは関係がなく、
 ドメインクラス名を指定する
 
 ****
 具体的には、
 ドメインクラスでこう指定し、
 @Table(name = "customers")
 
 リポジトリのJPAインターフェースを下記のように設定
 customersだとmappingできないと怒られる。public interface CustomerRepository extends JpaRepository { @Query("SELECT x FROM Customer x ORDER BY x.firstName, x.lastName") List findAllOrderByName(); } 
 ドキュメントをちゃんと読もう
- RequestMappingの設定確認
- 
@RestControllerをつけたクラスに、
 対応した@RequestMappingが付与する
 
 起動時の確認としては、ログにMapped "xxx" onto ....と出力されるかを確認するLooking for @ControllerAdvice: org.springframework.boot.context.embedded. AnnotationConfigEmbeddedWebApplicationContext@243c4f91 Mapped "{[/api/customers],methods=[GET]}" onto java.util.Listcom.example.api.CustomerRestController.getCustomers() ... 
- SQL / Primary key制約の解除
- > ALTER TABLE table_name DROP CONSTRAINT primary_key_name ;
- JPA / Pagingとsort
- 
Mapping(Controller)
 Service@GetMapping Page<Customer&t; getCustomers(@PageableDefault Pageable pageable) { Pagecustomers = customerService.findAll(pageable); return customers; } 
 Repositorypublic Page<Customer> findAll(Pageable pageable) { return customerRepository.findAll(pageable); }
 URLパラメータで表示数を変化できるpublic interface CustomerRepository extends JpaRepository<Customer, Integer&t; { @Query("SELECT x FROM Customer x ORDER BY x.firstName, x.lastName") Page<Customer&t; findAllOrderByName(Pageable pagealbe); }
 http://localhost:8080/project?page=1&size=3
 これ面白い。ちょうど検索数が多い場合の画面を実装どうしようと考えていたとこ。
 Pageの内容だとこんな感じのJSON
 {"content":[{"id":1,"firstName":"Hanamichi","lastName":"Sakuragi"}, {"id":2,"firstName":"Kaede","lastName":"Rukawa"}, {"id":3,"firstName":"Sho","lastName":"Sato"}], "last":false, "totalElements":5, "totalPages":2, "size":3, "number":0, "numberOfElements":3, "first":true," sort":null}
- Spring / 開発時にHTMLのキャッシュを無効化する
- 
デフォルトはパフォーマンス向上のため、HTMLはキャッシュされる設定です。
 そのため、HTMLキャッシュを開発時に無効化すると効率的にHTMLファイルの変更を確認できます。
 
 方法は2通り- application.propertiesで設定する
- Spring Dev Toolsを使用する
 
 
0 件のコメント:
コメントを投稿