0
實作資料存取 的抽象化           Cd Chen http://www.niceStudio.com.tw/
陳永昇 (Cd Chen)經歷:• 乃師實業技術總監• 恆逸資訊講師• 聯成電腦講師• 碩誠資訊研發部經理專長:• Unix / Linux 系統管理與開發• Java / Python / Perl / Objective-C /C / C+...
• 全部都是 Java• 完全不遵守 JSR• 包含可能影響身 心健康的內容
世間永遠不會改變的是...不斷改變的  需求
給我寫⼀一隻  “指令模式”統計訂單金額的程式
public static void main(String[] args){    System.out.println(“開始統計”);    List<Order> orders = // ...;    double sum = 0.0...
給我寫⼀一隻  “指令模式”  “視窗介面”統計訂單金額的程式
http://i.mtime.com/3176183/blog/5709802/
內聚 & 耦合
ControllerView                Model
PresentationBusiness Logic Data Access
單⼀一責任原則
ConsoleUI Presentation OrderServiceBusiness LogicOrderRepository Data Access
給我寫⼀一隻  “指令模式”  “網頁介面”統計訂單金額的程式
ConsoleUI   HtmlUI Presentation OrderServiceBusiness LogicOrderRepository Data Access
針對介面而非實作
<<use>>                               Main                            Presentation     UserInterface     +showHint()+showP...
public interface UserInterface {                      void showHint();                      void showPrice(double price); ...
Data Access Layer??
關聯式資料庫             NoSQL        其他•Native Driver   •MongoDB   •File•JDBC            •CouchDB   •Web Service•Hibernate     ...
資料儲存在  “檔案資料庫” “關聯式資料庫”“NoSQL 資料庫”     ...
<<use>>                             Main                          Data Access   OrderRepository +findAll(): List<Order>    ...
public interface OrderRepository {                      List<Order> findAll();                 }                 public cl...
Spring Data
Spring Data降低 DAL 耦合            •減少 DAL 開發            •簡化 DAL 實作            •支援 QueryDSL
IoC & AOP
// OrderRepositorypublic interface OrderRepository {     List<Order> findAll();}                               注入實作細節,// M...
Base            Commons關聯式資料庫          JDBC / JPABig Data        Apache HadoopData-Grid       GemFireHTTP            RESTD...
Repository                             +save(entity: T): T                             +findOne(id: ID): T                 ...
Example:Spring Data JPA
減少 DAL 的開發
public class OrderRepositoryJpaImpl    implements OrderRepository {    List<Order> findAll() {      EntityManager em =    ...
簡化 DAL 的實作
Method Name Keywords
KeywordfindByFFFKKK    欄位名稱
public interface OrderJpaRepository    extends JpaRepository<Order, Long>, OrderRepository {    List<Order> findByDate(Dat...
Logic          KeywordAFTER          After / IsAfterBEFORE         Before / IsBeforeCONTAINING     Containing / IsContaini...
Logic              KeywordIS NULL            Null / IsNullLESS THAN          LessThan / IsLessThanLESS THAN EQUALS LessTha...
@Query Annotation
// Order@Entity@NamedQuery(    name=”Order.findByProfitThan100”,    query=”select o from Order o where price - cost > 100”...
Thank you.http://slidesha.re/Oa2LYT
Spring Data for KSDG 2012/09
Spring Data for KSDG 2012/09
Upcoming SlideShare
Loading in...5
×

Spring Data for KSDG 2012/09

853

Published on

這是 Cd Chen 在 2012/09 於 KSDG 的技術分享之投影片。

Published in: Technology
2 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total Views
853
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
13
Comments
2
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Spring Data for KSDG 2012/09"

    1. 1. 實作資料存取 的抽象化 Cd Chen http://www.niceStudio.com.tw/
    2. 2. 陳永昇 (Cd Chen)經歷:• 乃師實業技術總監• 恆逸資訊講師• 聯成電腦講師• 碩誠資訊研發部經理專長:• Unix / Linux 系統管理與開發• Java / Python / Perl / Objective-C /C / C++ / PHP / Ruby• Security / Agile / OOAD
    3. 3. • 全部都是 Java• 完全不遵守 JSR• 包含可能影響身 心健康的內容
    4. 4. 世間永遠不會改變的是...不斷改變的 需求
    5. 5. 給我寫⼀一隻 “指令模式”統計訂單金額的程式
    6. 6. public static void main(String[] args){ System.out.println(“開始統計”); List<Order> orders = // ...; double sum = 0.0; for (Order order : orders) { sum += order.getPrice(); } System.out.println( “總金額是: ” + sum );}
    7. 7. 給我寫⼀一隻 “指令模式” “視窗介面”統計訂單金額的程式
    8. 8. http://i.mtime.com/3176183/blog/5709802/
    9. 9. 內聚 & 耦合
    10. 10. ControllerView Model
    11. 11. PresentationBusiness Logic Data Access
    12. 12. 單⼀一責任原則
    13. 13. ConsoleUI Presentation OrderServiceBusiness LogicOrderRepository Data Access
    14. 14. 給我寫⼀一隻 “指令模式” “網頁介面”統計訂單金額的程式
    15. 15. ConsoleUI HtmlUI Presentation OrderServiceBusiness LogicOrderRepository Data Access
    16. 16. 針對介面而非實作
    17. 17. <<use>> Main Presentation UserInterface +showHint()+showPrice(price: double) <<class>> ConsoleUserInterface +showHint()+showPrice(price: double)
    18. 18. public interface UserInterface { void showHint(); void showPrice(double price); } public class ConsoleUI ConsoleUI implements UserInterface { public void showHint() { Presentation System.out.println(" "); } public void showPrice(double price) { Service System.out.println(" " + price); }Business Logic } public static void main(String[] args) { Repository UserInterface ui = ConsoleUI(); ui.showHint(); Data Access List<Order> orders = // double price = 0.0; ... for (Order order : orders) price += order.getPrice(); ui.showPrice(price); }
    19. 19. Data Access Layer??
    20. 20. 關聯式資料庫 NoSQL 其他•Native Driver •MongoDB •File•JDBC •CouchDB •Web Service•Hibernate •Redis •...•JPA •HBase•... •...
    21. 21. 資料儲存在 “檔案資料庫” “關聯式資料庫”“NoSQL 資料庫” ...
    22. 22. <<use>> Main Data Access OrderRepository +findAll(): List<Order> <<class>>OrderRepositoryJpaImpl +findAll(): List<Order>
    23. 23. public interface OrderRepository { List<Order> findAll(); } public class OrderRepositoryJpaImpl implements OrderRepository { ConsoleUI public List<Order> findAll() { Presentation // JPA … } } Service public static void main(String[] args) {Business Logic UserInterface ui = ConsoleUI(); ui.showHint(); OrderRepository repo = Repository new OrderRepositoryJpaImpl(); List<Order> orders = repo.findAll(); Data Access double price = 0.0; for (Order order : orders) price += order.getPrice(); ui.showPrice(price); }
    24. 24. Spring Data
    25. 25. Spring Data降低 DAL 耦合 •減少 DAL 開發 •簡化 DAL 實作 •支援 QueryDSL
    26. 26. IoC & AOP
    27. 27. // OrderRepositorypublic interface OrderRepository { List<Order> findAll();} 注入實作細節,// Main Class 定義於外部的設定中,降@AutowiredOrderRepository repo; 低元件間的耦合,進而提 高維護的彈性@AutowiredUserInterface ui;public static void main(String[] args) { ui.showHint(); List<Order> orders = repo.findAll(); double price = 0.0; for (Order order : orders) price += order.getPrice(); ui.showPrice(price);}
    28. 28. Base Commons關聯式資料庫 JDBC / JPABig Data Apache HadoopData-Grid GemFireHTTP RESTDocument MongoDBGraph Neo4jColumn Stores HBaseKey-Value Redis
    29. 29. Repository +save(entity: T): T +findOne(id: ID): T +findAll(): Iterator<T> +count(): Long +delete(entity: T) CrudRepository +exists(id: ID): boolean ... +findAll(sort: Sort): Iterator<T> +findAll(pageable: Pageable): Page<T>PagingAndSortingRepository
    30. 30. Example:Spring Data JPA
    31. 31. 減少 DAL 的開發
    32. 32. public class OrderRepositoryJpaImpl implements OrderRepository { List<Order> findAll() { EntityManager em = Query q = em.createQuery(“SELECT o FROM Order o”); List<Order> results = new ArrayList<Order>(); for(Order order : q.getResultList()) { results.add(order); } return results; }} No Spring-Datapublic interface OrderJpaRepository extends JpaRepository<Order, Long>, OrderRepository {} Yes, Spring-Data!!
    33. 33. 簡化 DAL 的實作
    34. 34. Method Name Keywords
    35. 35. KeywordfindByFFFKKK 欄位名稱
    36. 36. public interface OrderJpaRepository extends JpaRepository<Order, Long>, OrderRepository { List<Order> findByDate(Date date); List<Order> findByCustomer(Customer customer); List<Order> findByCustomerAndDate(Customer c, Date d); List<Order> findByCustomer_NameLike(String name); List<Order> findByDateBefore(Date theDate); List<Order> findByPriceLessThan(double price); // ...} Method Name Keywords
    37. 37. Logic KeywordAFTER After / IsAfterBEFORE Before / IsBeforeCONTAINING Containing / IsContaining / ContainsBETWEEN Between / IsBetweenENDING WITH EndingWith / IsEndingWith / EndsWithEXISTS ExistsFALSE False / IsFalseGREATER THAN GreaterThan / IsGreaterThanGREATER THAN GreaterThanEqual / IsGreaterThanEqualEQUALSIN In / IsInIS Is / EqualsIS NOT NULL NotNull / IsNotNull
    38. 38. Logic KeywordIS NULL Null / IsNullLESS THAN LessThan / IsLessThanLESS THAN EQUALS LessThanEqual / IsLessThanEqualLIKE Like / IsLikeNEAR Near / IsNearNOT Not / IsNotNOT LIKE NotLike / IsNotLikeREGEX Regex / MatchesRegex / MachesSTARTING WITH StartingWith / IsStartingWith / StartsWithTRUE True / IsTrueWITHIN Within / IsWithin
    39. 39. @Query Annotation
    40. 40. // Order@Entity@NamedQuery( name=”Order.findByProfitThan100”, query=”select o from Order o where price - cost > 100”)class Order { // ...}// OrderJpaRepositorypublic interface OrderJpaRepository extends JpaRepository<Order, Long>, OrderRepository { List<Order> findByProfitThan100(); @Query(“select o from Order o where price - cost > 0”) List<Order> findByPositiveProfit(); @Query(“select o from Order o where price > ?1”) List<Order> findByPriceGreaterThanArg(double price); // ....} @Query Annotation
    41. 41. Thank you.http://slidesha.re/Oa2LYT
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×