ãååã®ãScalaã®Webã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ãPlay Frameworkãå ¥éï½ï¼1ï¼ç°å¢æ§ç¯ã¨ã¹ã¿ã¼ãã¢ããã¢ããªã®ä½æãã§ã¯Play2ã®ç°å¢æ§ç¯æ¹æ³ã¨ã¹ã¿ã¼ãã¢ããã¢ããªã«ã¤ãã¦ç´¹ä»ãã¾ããããã®ç°å¢ãå ã«ãã¼ã¿ãã¼ã¹ï¼ä»¥ä¸ãDBï¼ã«CRUDæä½ãè¡ãç°¡åãªWebã¢ããªã±ã¼ã·ã§ã³ãä½ãæ¹æ³ãã2åã«ããã£ã¦ã¹ãããã»ãã¤ã»ã¹ãããã§ç´¹ä»ãã¦ããã¾ããä½æããã¢ããªã±ã¼ã·ã§ã³ã¯ãã¤ãã³ãã®æ å ±ããç»é²ã»æ¤ç´¢ã»æ´æ°ã»åé¤ãããã¤ãã³ã管çã¢ããªãã§ãã
ãDBã¯ããã«ä½¿ããH2Databaseï¼ä»¥ä¸ãH2ï¼ãå©ç¨ããDBã¸ã®ã¢ã¯ã»ã¹ã¯Slickãå©ç¨ãããã¨ã«ãã¾ããäºåæºåã¨ãã¦ã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ã¢ããã¨DBã¹ãã¼ãã®çæãH2ã¨Slickã使ãããã®è¨å®ãè¡ã£ãå¾ãä»åã¯ãCï¼ç»é²æ©è½ããä½æãã次åã«ãRï¼åç §æ©è½ãããUï¼æ´æ°æ©è½ãããDï¼åé¤æ©è½ããé ã«ä½æãã¦ããã¾ãã
éçºåã®æºå
ï¼1ï¼ã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ã¢ãã
ãå·çæç¹ã§ã¯Play2ã®ææ°ãã¼ã¸ã§ã³ã¯ã2.3.0ããªã®ã§ãååæ§ç¯ããç°å¢ããã¼ã¸ã§ã³ã2.3.0ãã§åæ§ç¯ãã¦ãããã¨ã«ãã¾ããä»åå©ç¨ããã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ã¯æ¬¡ã®ã¨ããã§ãã
ã©ã¤ãã©ãª | ãã¼ã¸ã§ã³ |
---|---|
Scala | 2.11.1 |
Play2 | 2.3.0 |
Slick | 2.1.0-M2 |
Bootstrap | 3.1.1 |
ãPlay2ã¯ã2.3.0ãããç°å¢æ§ç¯æ¹æ³ãå°ãå¤ããã¾ããååè¨äºã§ç´¹ä»ããããã«ã以åã®ãã¼ã¸ã§ã³ã§ã¯ãplay newããªã©ã®Playã³ãã³ãã§ã¢ããªã±ã¼ã·ã§ã³ã®ç°å¢ãæ§ç¯ãã¾ããããæ¬ãã¼ã¸ã§ã³ããPlay2ã¯ãTypesafe Activatorãã¨ã»ããã§æä¾ãããããã«ãªãActivatorã³ãã³ããå©ç¨ãããã¨ã«ãªãã¾ããåºæ¬çãªæµãã¯åããªã®ã§ç°¡åã«ç´¹ä»ãã¾ãã
ããã¦ã³ãã¼ããµã¤ãããZipãã¡ã¤ã«ãtypesafe-activator-1.2.2-minimal.zipãããã¦ã³ãã¼ãããä»»æã®ãã£ã¬ã¯ããªã«å±éãã¾ããçè ã¯ãC:\pleiades\activator\activator-1.2.2ãã¨ãã¾ãããActivatorã³ãã³ããå©ç¨ã§ããããã«å±éãããã£ã¬ã¯ããªãç°å¢å¤æ°ã®ãPathãã«è¿½å ãã¦ããã¾ãã
ãã¢ããªã±ã¼ã·ã§ã³ã®ä½æã¯ãactivator new <ã¢ããªã±ã¼ã·ã§ã³å> <ãã³ãã¬ã¼ãå>ãã§è¡ãã¾ããTypesafe Activatorã«ã¯100種é¡ä»¥ä¸ã®ãã³ãã¬ã¼ããæä¾ããã¦ãããä½æããã¢ããªã±ã¼ã·ã§ã³ã«ãã£ã¦æãé©ãããã³ãã¬ã¼ããé¸æãããã¨ãã§ãã¾ããæä¾ããã¦ãããã³ãã¬ã¼ãã¯ãTYPESAFE ACTIVATOR/TEMPLATESãã§åç §ã§ããã³ã³ããªãã¥ã¼ããããã¨ãå¯è½ã§ããã¾ãããactivator newãã³ãã³ãã§å¯¾è©±åã§ã¢ããªã±ã¼ã·ã§ã³åãªã©ãæå®ãããã¨ãããactivator uiãã³ãã³ãã使ç¨ãã¦Webãã©ã¦ã¶ãããã³ãã¬ã¼ãã®é¸æãªã©ãè¡ãã¢ããªã±ã¼ã·ã§ã³ãä½æãããã¨ãã§ãã¾ãã
ãä»åã¯ãScalaã§Play2ãç¨ããã·ã³ãã«ãªã¢ããªã±ã¼ã·ã§ã³ãæ§ç¯ããããããplay-scalaãã¨ãããã³ãã¬ã¼ãã使ç¨ãããactivator new techApp play-scalaããå®è¡ãã¾ãããããã·ç°å¢ä¸ã§å©ç¨ããå ´åã¯ãã¦ã¼ã¶ãã£ã¬ã¯ããªã®ã.activatorããã£ã¬ã¯ããªé ä¸ã«ã次ã®ãããªãactivatorconfig.txtããä½æãã¦ããã¾ãããã
activatorconfig.txtï¼C:\Users\<ã¦ã¼ã¶å>\.activatorï¼
-Dhttp.proxyHost=<ãããã·ãã¹ãå> -Dhttp.proxyPort=<ãããã·ãã¼ãçªå·> -Dhttp.nonProxyHosts="<ãããã·é¤å¤ãã¹ãå>"
ãEclipseããã¸ã§ã¯ãã¸ã®å¤æã¯ãactivator eclipseããã¢ããªã±ã¼ã·ã§ã³ã®å®è¡ã¯ãactivator runããããã¸ã§ã¯ããã£ã¬ã¯ããªã§å®è¡ãã¾ãããactivatorããå®è¡ããã¨Activatorã®ã³ã³ã½ã¼ã«ç»é¢ãèµ·åããããã«ãhelpãã³ãã³ããå®è¡ãããã¨ã§ã³ã³ã½ã¼ã«ä¸ã§å®è¡å¯è½ãªã³ãã³ãã確èªãããã¨ãã§ãã¾ãã
ãPlay2ã«ä¼´ãScalaã®ãã¼ã¸ã§ã³ãã2.11.1ãã«ãã¼ã¸ã§ã³ã¢ãããã¾ãããååè¨äºã§ã¯Eclipseã«ã2.10ãåãã®ãã©ã°ã¤ã³ãScalaIDE for Eclipseããã¤ã³ã¹ãã¼ã«ãã¾ããã®ã§ã以ä¸ã®ãã©ã°ã¤ã³ãµã¤ãã使ç¨ããã2.11ãçã«ã¢ãããã¼ããã¦ããã¾ãããã
ï¼2ï¼Slickã©ã¤ãã©ãªã®è¨å®
ãPlay2ã®ãã«ãã¯ãsbtãã§è¡ããã¾ãã®ã§ãã©ã¤ãã©ãªã使ç¨ããã«ã¯sbtã®ãã«ãã¹ã¯ãªããã«ä¾åé¢ä¿ãå®ç¾©ããå¿ è¦ãããã¾ããSlickãå©ç¨ãããããããã¸ã§ã¯ãç´ä¸ã«ãããbuild.sbtããç·¨éããä¾åé¢ä¿ãå®ç¾©ãã¾ããDBã¢ã¯ã»ã¹ã©ã¤ãã©ãªã¨ãã¦ãããã©ã«ãã§ã¯ãAnormããå®ç¾©ããã¦ãã¾ãã®ã§Slickï¼"com.typesafe.slick" %% "slick" % "<ãã¼ã¸ã§ã³>"ï¼ã«å¤æ´ãã¦ããã¾ãããã
build.sbt
name := """techApp""" version := "1.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayScala) scalaVersion := "2.11.1" libraryDependencies ++= Seq( jdbc, "com.typesafe.slick" %% "slick" % "2.1.0-M2", cache, ws )
ãå·çæç¹ã®Slickã®ææ°ãã¼ã¸ã§ã³ã§ããã2.0.2ãã¯Scalaã®ã2.11ãã«å¯¾å¿ãã¦ããã¾ããã®ã§ãLatest Milestoneã®ã2.1.0-M2ãã使ç¨ãã¾ãããlibraryDependenciesãã«Slickã¸ã®ä¾åè¨å®ãè¡ãã¾ãããä¾åè¨å®ã¯ãã°ã«ã¼ãåã%ãã¢ã¼ãã£ãã¡ã¯ãåã%ããªãã¸ã§ã³ãã®å½¢å¼ã§è¨å®ãã¾ãããã°ã«ã¼ãåã%%ãã¢ã¼ãã£ãã¡ã¯ãåã%ããªãã¸ã§ã³ãã®ããã«ãã°ã«ã¼ãåãã®å¾ãã«ã%ãã2ã¤éããã¨ã¢ã¼ãã£ãã¡ã¯ãåã«Scalaã®ãã¼ã¸ã§ã³ãèªåçã«è£å®ããã¾ããä»å使ç¨ãã¦ããScalaã®ãã¼ã¸ã§ã³ã¯2.11.1ãªã®ã§ããã¦ã³ãã¼ãããSlickã®ã¢ã¼ãã£ãã¡ã¯ãåã¯ãslick_2.11ãã§ããsbtã¸ã®ã©ã¤ãã©ãªã®è¨å®ãå®äºå¾ããactivatorãã³ãã³ãã§ã³ã³ã½ã¼ã«ãèµ·åãããeclipse source-with=trueããå®è¡ããEclipseã®ã¯ã©ã¹ãã¹ãã¡ã¤ã«ã.classpathããåçæãã¾ãã
ï¼3ï¼Eclipseãã©ã°ã¤ã³ã®ã¤ã³ã¹ãã¼ã«
ãä»åå©ç¨ããH2ã®èµ·åã»åæ¢ãè¡ãããã«Eclipseã®ãã©ã°ã¤ã³ã§ãããDBLauncherããã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãï¼ç´æ¥ã³ãã³ãããèµ·åããå ´åããç°ãªããã¼ã«ã§è¡ãå ´åã¯ãæ¬ãã©ã°ã¤ã³ã®ã¤ã³ã¹ãã¼ã«ã¯å¿ è¦ã¯ããã¾ããï¼ãããã«ãã->ãã½ããã¦ã§ã¢ã®ã¤ã³ã¹ãã¼ã«ããé¸æããã¤ã³ã¹ãã¼ã«ãã¦ã£ã³ãã¦ã表示ãã¾ããä½æ¥å¯¾è±¡ã«ã http://eclipse.seasar.org/updates/3.2/ããå ¥åãã表示ãããä¸è¦§ãããDBLauncherããé¸æãã¾ããã次ã¸ããæ¼ä¸ããæµãã«æ²¿ã£ã¦ã¤ã³ã¹ãã¼ã«ä½æ¥ãå®äºããã¦ãã ããã
ã次ã«EclipseããH2ãå©ç¨ããããã®è¨å®ãè¡ãã¾ããH2ã®DBãã¡ã¤ã«ãé åããä»»æã®ãã£ã¬ã¯ããªãä½æãã¾ããçè ã¯ããã¸ã§ã¯ãç´ä¸ï¼techAppé ä¸ï¼ã«ãdataãã¨ãããã£ã¬ã¯ããªãä½æãã¾ãããç¶ãã¦ãããã¸ã§ã¯ãã®ããããã£ï¼ããã¸ã§ã¯ããå³ã¯ãªãã¯->ãããããã£ããé¸æï¼ããDBLauncherã®è¨å®ããã¾ãããH2ãã¼ã¿ãã¼ã¹ã使ç¨ãããã§ãã¯ãããã¼ã¹ã»ãã£ã¬ã¯ããªã«å ã»ã©ä½æãããã¼ã¿ãã¡ã¤ã«é åç¨ãã£ã¬ã¯ããªï¼/techApp/dataï¼ãæå®ãã¦ãã ããã
ãã¤ã³ã¹ãã¼ã«ãå®äºãããããã©ã°ã¤ã³ãå©ç¨ã§ãããã¨ã確èªãã¦ããã¾ããããããã±ã¼ã¸ã»ã¨ã¯ã¹ããã¼ã©ã¼ã§ããã¸ã§ã¯ããå³ã¯ãªãã¯ããã¨ãH2ãã¨ããã¡ãã¥ã¼ã追å ããã¦ãã¾ãã®ã§ããH2ãµã¼ãã®èµ·åããé¸æããã¨H2ãèµ·åããã¾ãï¼ãH2ãµã¼ãã®åæ¢ãã§åæ¢ãããã¨ãã§ãã¾ãï¼ã
ï¼4ï¼DBã¹ãã¼ãã®ä½æ
ããã¤ãã³ã管çã¢ããªãã§ä½¿ç¨ããDBã¹ãã¼ããä½æãã¾ãããã¤ãã³ã管çã¢ããªãã§å¿ è¦ãªãã¼ãã«ã¯ãEVENTããã¼ãã«ã®ã¿ã§ããããã±ã¼ã¸ã»ã¨ã¯ã¹ããã¼ã©ã¼ã§ããã¸ã§ã¯ããå³ã¯ãªãã¯ããH2ã->ããã¼ã¿ãã¼ã¹ã»ããã¼ã¸ã£ã¼ã®è¡¨ç¤ºãã®é ã«é¸æããã¨ãWebãã©ã¦ã¶ä¸ã§H2ã«å¯¾ãã¦SQLãå®è¡ãããã¨ãã§ãã¾ãã以ä¸ã®ã¹ã¯ãªãããå®è¡ããTECHAPPãã¹ãã¼ãã¨ãEVENTããã¼ãã«ãä½æãã¾ãããã
DDLã¹ã¯ãªãã
CREATE SCHEMA TECHAPP; CREATE TABLE TECHAPP.EVENT ( ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, EVENT_ID VARCHAR(100) NOT NULL, EVENT_NM VARCHAR(100) NOT NULL );
ï¼5ï¼DBã¸ã®æ¥ç¶è¨å®
ãDBã¸ã®æ¥ç¶è¨å®ã¯ãapplication.confãã«è¡ãã¾ãããDatabase configurationãæ¬ã«DBã¸ã®è¨å®ãã³ã¡ã³ãã¢ã¦ãããã¦ãã¾ãã®ã§ãæå¹ã«ã以ä¸ã®ããã«è¨å®ãã¦ãã ããã
application.confï¼/confï¼
# Database configuration # ~~~~~ # You can declare as many datasources as you want. # By convention, the default datasource is named `default` # db.default.driver=org.h2.Driver db.default.url="jdbc:h2:tcp://localhost:9092/demo;SCHEMA=TECHAPP" db.default.user=sa db.default.password=""
ãå ã»ã©ä½æããH2ã®ãTECHAPPãã¹ãã¼ãã¸æ¥ç¶ããããã«è¨å®ãã¾ããJDBCãã©ã¤ãã¼ã«ãorg.h2.DriverããDBæ¥ç¶URLã«ã¯ãjdbc:h2:tcp://localhost:9092/demo;SCHEMA=TECHAPPããæå®ããã¦ã¼ã¶ã¯ãsaãããã¹ã¯ã¼ãã¯ããªããã¨ãã¾ãã