29
Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved. 1 Seasar Seasar Conference Conference 2007 Spring 2007 Spring Seasar Seasar Conference Conference 2007 2007 Spring Spring DB DB Flute Flute の紹介 の紹介 2007.5.27 株式会社ビルドシステム 久保 雅彦

DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.1

SeasarSeasar ConferenceConference2007 Spring2007 Spring

SeasarSeasar ConferenceConference2007 2007 SpringSpring

DBDBFluteFluteの紹介の紹介

2007.5.27株式会社ビルドシステム

久保 雅彦

Page 2: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

2SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

AAgendagenda

• Goal• Introduction• DBFlute Style• ConditionBean• Sql2Entity• History• Summary/Q&A

Page 3: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

3SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

GoalGoal

DBFluteって何?

を知っていただくこと

Page 4: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

4SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

AgendaAgenda

• Goal• Introduction• DBFlute Style• ConditionBean• Sql2Entity• History• Summary/Q&A

Page 5: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

5SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Introduction : What is Introduction : What is DBFluteDBFlute??

DBFluteは、S2Daoの利用を

サポートするToolです。

※Apache TorqueのGenerator機能を

S2Daoに適応させたものです。

Page 6: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

6SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Introduction : Function of Introduction : Function of DBFluteDBFlute

• S2Daoで利用するDao/EntityをDatabaseのSchema情報を利用して自動生成します。

• S2Daoの“DTOによるSQL自動生成”の機能を拡張したDTOを自動生成します。

• DatabaseのSchema情報を表示する'SchemaHTML'を自動生成します。

• S2Daoの“外だしSQL”(SQLファイル)から対応するEntityを自動生成します。

Page 7: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

7SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

IntroductionIntroduction : : DBFluteDBFluteののPolicy Policy

【O/R-MapperとしてPolicy】DBFluteは、SQL文を自動生成する機能を有しますが、SQL文を隠蔽するつもりはありません。

【実装Policy 】[簡単なSQL+複雑だけれども定型的なSQL]

ConditionBeanでTypeSafe実装

[複雑なSQL]

Sql2Entityの支援と共に“外だしSQL”で実装

Page 8: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

8SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

• Goal• Introduction• DBFlute Style• ConditionBean• Sql2Entity• History• Summary/Q&A

Page 9: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

9SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

DBFluteDBFlute StyleStyle

• 1. DBのSchemaとTableを作成する。

• 2. DBFluteの環境を準備する。

• Project固有の設定を行うなど

• 3. DBFluteでJdbc-Taskを実行し、DB情報を取得する。

• 4. DBFluteでGenerate-Taskを実行し、 DB情報から「Dao/Entity/dicon/DBFlute独自Class」を自動生成する。

• 5. 自動生成されたClassを利用してApplicationを実装する。

【DB変更発生】

A. DBのSchemaとTableを変更する。

B. 「3」/「4」を実行する。

C. Compile-Errorになった部分を修正する。

DB変更に強い!

Page 10: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

10SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

AgendaAgenda

• Goal• Introduction• DBFlute Style• ConditionBean• Sql2Entity• History• Summary/Q&A

Page 11: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

11SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : What is : What is ConditionBeanConditionBean??

What is ConditionBean?

S2Daoの「DTO引数によるSQL自動生成機能」を拡張し、SQL自動生成の幅を広げたもの。

[Dto]public class EmployeeSearchCondition {

public static final String dname_COLUMN = "dname_0";private String job; private String dname;...

}[Dao]List getEmployeesBySearchCondition(EmployeeSearchCondition dto);

Page 12: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

12SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : What has been enhanced to it? : What has been enhanced to it?

What has been enhanced to it?

1. Where句に指定する条件で{=}以外の演算子もサポート

2. OrderBy句に指定するColumn & {Asc or Desc}の指定をサポート

3. Select句に含める結合先Tableの指定をサポート

4. Where句に指定する条件で無限階層のForeignTableのColumnの指定をサポート

5. fetchFirst(), fetchScope(), fetchPage()によるLimitSearch/PagingSearchをサポート

6. lockForUpdate()による更新Lockのサポート

7. …

Page 13: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

13SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : Sample DB : Sample DB 「「LDBLDB」」

Sample DB 「LDB」

Page 14: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

14SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : Query: Query

ex) BOOKに対してBOOK_NAMEの‘S2Dao’前方一致で、かつ、GENRE_CODEがnullでないものを検索

/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// select ...// from BOOK book// where book.BOOK_NAME like 'S2Dao%'// and book.GENRE_CODE is not null// final LdBookCB cb = new LdBookCB();cb.query().setBookName_PrefixSearch("S2Dao");cb.query().setGenreCode_IsNotNull();// ☆Pointfinal java.util.List<LdBook> ls = bhv.selectList(cb);- - - - - - - - -/

Where句に指定する条件で{=}以外の演算子もサポート

Page 15: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

15SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : Order By: Order By

ex) BOOKに対してAUTHOR_IDの降順かつBOOK_IDの昇順で全件検索

/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// select ...// from BOOK book// order by book.AUTHOR_ID desc, book.BOOK_ID asc// final LdBookCB cb = new LdBookCB();cb.query().addOrderBy_AuthorId_Desc()

.addOrderBy_BookId_Asc();final java.util.List<LdBook> ls = bhv.selectList(cb);- - - - - - - - -/

OrderBy句に指定するColumn&{Asc or Desc}の指定をサポート

Page 16: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

16SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : Select Tables: Select Tables

ex) BOOKに対してAUTHORを結合してSelect句に含める検索/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// select book.BOOK_ID, book...// , author.AUTHOR_ID as authorId_0, author...// from BOOK book// left outer join AUTHOR author// on book.AUTHOR_ID = author.AUTHOR_ID//final LdBookCB cb = new LdBookCB();cb.setupSelect_Author();final java.util.List<LdBook> ls = bhv.selectList(cb);- - - - - - - - -/

Select句に含める結合先Tableの指定をサポート

Page 17: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

17SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : Query : Query ForeignTableForeignTable

ex) BOOKに対してForeignTableのAUTHORを結合してAUTHOR_AGEで30歳以上を検索。(AUTHOR_AGE降順)

/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// select ...// from BOOK book// left outer join AUTHOR author// on book.AUTHOR_ID = author.AUTHOR_ID// where author.AUTHOR_AGE >= 30// order by author.AUTHOR_AGE desc//final LdBookCB cb = new LdBookCB();cb.query().queryAuthor().setAuthorAge_GreaterEqual(30);cb.query().queryAuthor().addOrderBy_AuthorAge_Desc();final java.util.List<LdBook> ls = bhv.selectList(cb);- - - - - - - - -/

Where句に指定する条件で無限階層のForeignTableのColumnの指定をサポート

Page 18: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

18SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : LimitSearch/PagingSearch: LimitSearch/PagingSearch--11

ex) BOOKに対して登録日時の降順で、最初の50件のみ検索

/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -final LdBookCB cb = new LdBookCB();cb.query().addOrderBy_RTime_Desc();cb.fetchFirst(50);final java.util.List<LdBook> ls = bhv.selectList(cb);- - - - - - - - -/

fetchFirst(), fetchScope(), fetchPage()によるLimitSearch/PagingSearchをサポート

Page 19: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

19SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : LimitSearch/PagingSearch: LimitSearch/PagingSearch--22

ex) BOOKに対して登録日時の降順で、81件目から100件目のみ

検索

/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -final LdBookCB cb = new LdBookCB();cb.query().addOrderBy_RTime_Desc();// 80件飛ばして20件分を取得(81件目から100件目)cb.fetchScope(80, 20);final java.util.List<LdBook> ls = bhv.selectList(cb);- - - - - - - - -/

fetchFirst(), fetchScope(), fetchPage()によるLimitSearch/PagingSearchをサポート

Page 20: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

20SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : LimitSearch/PagingSearch: LimitSearch/PagingSearch--33

ex) BOOKに対して登録日時の降順で、PageSize20件でPaging検索/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -final LdBookCB cb = new LdBookCB();cb.query().addOrderBy_RTime_Desc();cb.fetchFirst(20);cb.fetchPage(1);// ☆Point - 1-20final java.util.List<LdBook> lsFirstPage = bhv.selectList(cb);

cb.fetchPage(2);// ☆Point - 21-40final java.util.List<LdBook> lsSecondPage = bhv.selectList(cb);

cb.fetchPage(3);// ☆Point - 41-60final java.util.List<LdBook> lsThirdPage = bhv.selectList(cb);- - - - - - - - -/

fetchFirst(), fetchScope(), fetchPage()によるLimitSearch/PagingSearchをサポート

Page 21: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

21SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

ConditionBeanConditionBean : LimitSearch/PagingSearch: LimitSearch/PagingSearch--44

• MySQL → limit/offset構文利用• Postgre → limit/offset構文利用• Firebird → first構文を利用• Oracle → rownum利用• DB2 → fetch first x rows only構文利用

– ※OffsetはResultSetにてすっ飛ばし

• SQLServer → TOP構文利用– ※OffsetはResultSetにてすっ飛ばし

• Sybase → ※TOPを利用しようと思ったら Sybase-12.x ではサポートされていない!? (15.xかららしい)

fetchFirst(), fetchScope(), fetchPage()によるLimitSearch/PagingSearchをサポート

Page 22: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

22SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

AgendaAgenda

• Goal• Introduction• DBFlute Style• ConditionBean• Sql2Entity• History• Q&A

Page 23: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

23SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Sql2Entity : What is Sql2Entity?Sql2Entity : What is Sql2Entity?

What is Sql2Entity?

S2Daoの“外だしSQL”(SQLファイル)から、それに対応するEntityを自動生成

* * * * * * * * * * * * * * * * * * * * * * * * このような開発が可能になります。

1. {.sql}を作る。

2. {.sql}から対応するEntityを自動生成する。

3. DaoにMethodを定義して呼び出す。* * * * * * * * * * * * * * * * * * * * * * * *

Page 24: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

24SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Sql2Entity : Making SQLSql2Entity : Making SQL

exdao-Packageに{.sql}を作成。

ex) LdBookDao_selectBookCollectionStatistic.sql/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ---#BookCollectionStatistic#select book.BOOK_ID

, book.BOOK_NAME, (select count(*) from COLLECTION

where BOOK_ID = book.BOOK_ID) as COLLECTION_COUNTfrom BOOK book

/*BEGIN*/where/*IF bookName != null*/book.BOOK_NAME like /*bookName*/'S2Dao' || '%'/*END*/

/*END*/order by COLLECTION_COUNT desc;- - - - - - - - - -/

※自動生成したいEntityのClass名を「--#EntityName#」という形式でSQLに記述。

1. {.sql}を作る。

Page 25: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

25SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Sql2Entity : Making SQLSql2Entity : Making SQL

sql2entityを実行。

{DBFlute内部処理}

1. ‘exdao’以下(ExtendedDao)のDirectoryに存在する‘.sql’ファイルを読み込む。

2. ‘.sql’の中に‘--#entityName#’と記載されているものを対象にSQLを実行する。

3. 結果のResultSetのMetaDataから、ColumnNameとColumnTypeを取得する。

4. CustomizeEntityを自動生成する。

※必ず2WAY-SQLにしておくこと

2. {.sql}から対応するEntityを自動生成する。

Page 26: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

26SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Sql2Entity : Making SQLSql2Entity : Making SQL

手動で exdaoにMethodを定義

public java.util.List<LdBookCollectionStatistic>

selectBookCollectionStatistic(String bookName);

3. DaoにMethodを定義して呼び出す。

Page 27: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

27SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

• Goal• Introduction• DBFlute Style• ConditionBean• Sql2Entity• History• Q&A

Page 28: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

28SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

HiHistorystory• 2005年1-4月頃:作者がApache Torqueを利用したProjectを経験。

» 同時にSeasar/S2Daoの存在も知る。» Torqueの便利さを感じつつ不足部分も痛感する。» S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

• 2005年5-7月頃:Torqueを基にS2DaoのDaoやEntityを生成するS2DaoGenを実装。» SeasarProjectへの参加まで、株式会社アークシステム様のADEサーバ上のCVSにて管理。

(本当にありがとうございました)

• 2005年12月 :S2DaoGenを改良し、現在のConditionBeanの構造を確立。» それまでは、S2Daoの“DTOによるSQL自動生成”の延長でしかなかったが、その方法では

Performanceに問題があることが判明した。新しい構造により、それらは解消された。

• 2006年6-7月頃:C#版へ移植し、作者自らProjectにて利用する。» このとき現場で必要な機能の多くを実装。» 同時に別ProjectにてJava版を利用してもらい、多くの要望とバグを修正。

• 2006年10月 :DBFlute誕生。SeasarProjectのSandBoxとしてOpenSourceとなる。

• 2007年~ :実績と実力を付けてSandBox卒業を目指す。» DBFlute用EclipsePluginの開発

Page 29: DBFluteの紹介...» Torqueの便利さを感じつつ不足部分も痛感する。 »S2DaoのSQLコメント機能に感動し、Torqueの不足を埋められると感じる。

29SeasarSeasar ConferenceConference

2007 Spring2007 Spring Copyright© 2004-2007 The Seasar Foundation and the others. All rights reserved.

Q&A