早わかりSpring Data JDBC / jsug-2019-01

9b8f304ce234a4a8e38708247ffd68e0?s=47 Ryo Shindo
January 31, 2019

早わかりSpring Data JDBC / jsug-2019-01

JSUG勉強会 2019その1 Spring Data JDBC正式リリース記念!データアクセス特集
https://jsug.doorkeeper.jp/events/86027

9b8f304ce234a4a8e38708247ffd68e0?s=128

Ryo Shindo

January 31, 2019
Tweet

Transcript

  1. 1.

    . 1 . , 11 . , . JSUG 

    20191 Acroquest Technology  
  2. 2.

    profile: name: Ryo Shindo twitter: @shindo_ryo company: Acroquest Technology Inc.

    ide: IntelliJ IDEA favorites: - Spring Framework - JUnit 5 - Ace Combat 7 . . 2 .,. . ,, 2 2
  3. 7.

    l JPA04:> Ø @?%! N+167 Ø '!(#,$% Ø "*$/%-#&*$ Ø

    Entity)%+38   ;21/=A l JPA 04:<5 .9 ⇒ Spring Data JDBC . 7 . .,. . ,,
  4. 8.

    8 1. Spring Data JDBC 2.   3. 

     4.  . . .,. . ,,
  5. 9.
  6. 10.

    0 , , AB l Spring Data, 02= RDB!#('1"-12 Ø

    20185106 1.0.0 GA22' Ø Spring Data JPARepository JPAD GE8F  Ø %4.3  $4(.*  JPA?9> :@  - DDD2/&*2+)4BC;  opinionated1"-12JdbcTemplate A 7< 01 1 0 . 01 .
  7. 11.

    1 A l Spring Data JDBC ≠ Spring Data JPA

    Ø !"-+.&,    Ø #POJOOK l Spring Data JDBC ≠ Spring JDBC Ø JdbcTemplate)*/(   Ø One-To-Many  l Spring Data JDBC ≠ Spring Data JDBC Extension Ø %$ 0  JDBC Extention '  1 , . ., 1 . . .
  8. 12.

    B 2 - - D C n Getting Started •

    Spring Boot 2.1.1.RELEASE • Spring Data JDBC 1.0.4.RELEASE 2 A 1 ,1 2 2 1 1 1 . . . 0 0 1 0/ 4. 0 1/ 2 - - . 1/ <. 1 <. 1 . . . 4 >4
  9. 13.

    D 3 E n Getting Started B 3 C @

    . 3B , 1 3 A 1 C ; ; AA B 1 B ) B B AB B B ; AB BB A BB ,( ( )( ; E B E E CB B AB + /)) ; AB + /)) B A ,
  10. 14.

    J B > 4D4 , n Getting Started SRSL ,

    B D B AE CD > < , D << B DC B C B .) . < 1 C D B D > C ,BE 1 C D B . < > ( ; . " ( , < 1 C D B C4 < . < 1 C D B ;4 4
  11. 17.

    n Spring Data JDBC5> JdbcTemplate4 .') Ø Repository7<JdbcTemplate"&%  

    !,$"(-'*2#" /381$'" @  n JdbcTemplateMyBatis6?=: Ø XML+098  Repository;  AJdbcTemplateL C 1 , 2 , . .. , 7
  12. 18.

    AC n l JPA8G).29A Ø @Entity<> Ø =$- 2,@Id 

    Ø Spring Data JPA4H Ø ?@D &3'+1%(9E Ø ?@D &3'+1%( 756;: C *-#2+&3'+1%(75@PersistenceConstructor   C Ø setter<>"3) ) !/0(.2   FB 18 , 2 , . 8 .. ,
  13. 19.

    C e n Ti @ 2 1 A , 9

    . .,1 @ 2 1 . . . ; , , @.. . / . / ) / ( . . / ) ( . ; , , .. / ) / ( . ) ( . . ; .= .. ; / ( . ( . . .. ) / ( . ) ( . / ( . / ( . ( . ( . ; , .. 2 1 . ; .= .. / ( . ( . . . oa Ch 2 I dfC hI c i lI E L i g m 2 a Ch j 2 1. np @..
  14. 20.

    AC E n L L Ø CrudRepository extends Repository Ø

    CrudRepository CRUD 0 , <. .,0 0 . . . .. . .> . < . .. .. . 2
  15. 21.

    C A n Ø CrudRepository 2, 2 1 . 2,

    2 2 save(T entity) T findById(ID id) Optional<T> existsById(ID id) boolean findAll() Iterable<T> findAllById(Iterable<ID> ids) Iterable<T> count() long deleteById(ID id) void delete(T entity) void
  16. 22.

    N OQ RT n WS Ø @Query P L N

    F E * EL = M ) F H C A =L * . : )AA = H H : <@ EA L E H L > ( @ <;> ; B ) < ;B " ; C (2 L . * " , EA L H C H > ) < ;B B > , > , > , EA L E H L @
  17. 23.

    L N T n S R Ø E LJ L

    A C E I ( C BF E 2 .E ( C E C C < , >A A E C F E > . E >A ) , C E 3> . < EC C E 3> ,;; >A A E C F E > >A >A > E >A A E C F E > >,< , / , , < / ,< / ,< NC >A A E C F E > >A ;3 3 < , >A A E C < /; / ; < >A A E C F E > >A A E C ;3 3 >A A E C F E > ;3 3
  18. 24.

    A C n 1.0.4.RELEASE 17 1. '!,&#% (PagingAndSortingRepository) Ø 

    +<B> /58 2. )#$&:+.206 3= Spring Data JPAfindByLastName )#$&:+.206  3. D-4 4. 9;@=*" (%JPA@Embeddable Ø converterA?C@= 2, 4 2 4 . 4 2, 4 2 2
  19. 26.

    n Spring Data JDBC   '!*"  Ø One-To-One,

    One-To-Many   Ø Many-To-One, Many-To-Many   n Spring JDBC) MyBatis) #$&(  Ø  Spring JDBC)  +% 3 , 3 26 , . .. 3 , 3 3
  20. 27.

    A C n Spring Data JDBCDDD*)#'*($, FI ;  l

    (Aggregate) - nD",%!%! P - @60C.9 5 &$ N2 M< - HE?-3O - @60",%!%!- 81 BQ  @6+'K l A A (Repository) - @67=L:4/J >G  - @60",%!%!A@6+' BQ  3 , 3 2 , . .. 3 , 3 37
  21. 28.

    A n One-To-Many 3 . 8 3 8 ( 23

    3. 3 8, . 8 , 3 , ) 3 , , 8 ) ) ) C
  22. 29.

    ef cg n One-To-Many AIC;9 L C D 3 =

    9I == C;9 D C D C ; / .== C D C C ; @. . ) ; @. . . (. (. ; @. 2 C C, ;=) ; >D ( H D 2 ) ; / = . , .= . (. (. , .= (. ; @. , .= . (. (. = & = (. & (. ; . > ; ; I 2 C; 9 ; > ; >D C C, ;= ; I ; > ij ga a 2 pn g d lm ; >D h TOPS o C D C C <
  23. 30.

    A C n One-To-Many 3 . 3 , , .

    3 3 3 . , , , . / / == , , @ ) @ ; @ , . / = / ( ; , / ( ; / D @ ( ; , = & = ; & ; = & D D , , 0
  24. 31.

    C n One-To-Many PurchaseOrder OrderDetail A , 1 3 .

    , 3 3 5LI ;J 4I= I I= I . 5LI ;J 4I= I 3 ; 0; BD - I= I ;==1 D " 2; ; - I= I . I= I JB IO J; I= I - I= I ;==1 D " SPT R IB - I= I . I= I JB IO J; I= I - 5LI ;J 4I= I B=. " I= I0; BD . ( , (, ) ) " B DJ.94I= I0 ;B B=. " L; B O. " B D.2; ; " 4I= I0 ;B B=. " L; B O. " B D.SPT R IB :
  25. 32.

    L n One-To-Many PurchaseOrderC OrderDetail A , . ., 3

    2 ( 22 . . ;. 5 ; :I=4 = = . 5 ; :I=4 = 3 ;:B0:J= = D - = : 1J= " 2:L:T - = . . ). ;. . = : 1J= " OSP D J - = . . ). ;. . 5 ; :I=4 = . " = 0:J=J =. ( , (, ) ) " J= I. 4 = 0=J: B . " :DJ J . " J= .2:L:T " 4 = 0=J: B . " :DJ J . " J= . OSP D J 9
  26. 33.

    T RS n (1) ? D H ?AE CD 3

    >?=? ? -D == DC C F H = D D ) E ? ( ( D E AD D AD D =? ) H = D D ) E ? ( ( AD D = = I = ? D E AD D ) H = D D ) E ? - 3 . ? D = E C ? H = D D ) E ? , D E AD D = AD D =? = H = D D ) E ? ( ( AD D = = I = ? D E AD D = ) H = D D ) E ? ( ( AD D = = I = ? D E AD D ) WNM, 3O LI - 3 , 3
  27. 34.

    L n T R (2) Q. PurchaseOrder deleteC OrderDetail A

    (4 , 4 . ., 34 4 (4 ) . . ;. 4 . . 4 4 . . . 4 .
  28. 35.

    RA n (2) Q. PurchaseOrder delete L C OrderDetail L

    A. OrderDetail T DeleteC L ( 5 , . ., 3 ( ) . . ;. . .5 . . . . ? = ? = ? = ? =
  29. 36.

    n -     -   

     ⇒ A ID C L 3- , 3 6 , . .. 3- , 3 3 T T
  30. 37.

    1. One-To-Many R2 > A 7 C H )3A> D

    C 3 =>;>7 > .C );; A 7 C A AE AC< =C @ ;/ . ( . ;/ ; / ;/ C ,< ;> ( < ;> = C ( . ..& // & // / // @ / // .. / & //)/ / // /; . ; ; ; / /;;/ ,< ;> R S L ac T AC< =C E
  31. 38.

    EL 1. One-To-Many 2 ( C @ A @ ,

    8 C ( " @ 88 @ ,. 8 . @ @ 8 C ( .) ) ) 8 C 3 . 8 C ( .) . ; ( .) ; ; ) 8 C ) 8 C 3 . .
  32. 39.

    E C 1. One-To-Many 2 3 ., 3 9 3

    3, 3 ., 3 / / ; 3 A 3
  33. 40.

    1. One-To-Many 2 ; , AE C 2 > <

    0 ,<< ; C C := := ( := , " := := ( := ) 2; " 2 34< ).= < C3 .= < C = < 1 C; C3 ,<< 0;C = < = < ( =2 : ; =2 : ; ( =2 : ; D. ; 2B := 2 =2 : ; 2 := 3 = > 3 = > 1 C; C3 3 = > ( =2 : ; ( " ;2: ( B =" := ( := := ( " := := ( 1 .= < TIab R .= < 1 C; L
  34. 42.

    BC n Events Ø Repository S E O C B

    T@ > 2 A - < -2 4 > . 44 2 . . ; 4 -, , .) ) . . . ; ( . . = . . . = . = . . = . = . = . = . = . = .)
  35. 43.

    BC n Events Ø Repository S E O C B

    T@ > 3 A - < - 4 > . 44 3 . . ; 43-, 3 3 , .) ) . . . ; ( . . = . . . = . = . . = . = . = . = . = . = .)
  36. 44.

    n Auditing Ø  ()  . . 4 .,.

    . ,, public class Employee { // AuditorAware!   @CreatedBy private String createdBy; @CreatedDate private LocalDateTime createdDate; }
  37. 46.

    l Spring Data JDBC    Ø Repository0 JPA0

     Ø */  Ø ')!%+,1 l Spring Data JDBC    Ø *.-/  Ø (#)43$#"5  Ø 62 &&#"  , 4 4 . 4 , 4 6
  38. 47.

    1.   https://docs.spring.io/spring-data/jdbc/docs/1.0.4.RELEASE/reference/html/ 2. Spring Data JDBC, References, and

    Aggregates https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates 3. Spring Data JDBC 1.0.0.BUILD-SNAPSHOT(-> 1.0.0.RELEASE)  https://qiita.com/kazuki43zoo/items/bd63d28dc2348aa21719 7 , 4 4 . 7 4 , 4