groovyãé«éèµ·åããgyã³ãã³ã
...ãä½ã£ãã ä½ã£ãã¨ãã£ã¦ãæ¢åã®groovyã³ãã³ããæ¹é ããã ããªãã ãã©ã
èæ¯
groovyãé«éã«èµ·åããã«ã¯groovyservã¨ãããã®ããããããã¯ååèµ·åã¯é ãããã®å¾ã¯groovyã®èµ·åãçéã«ãªãã¨ãããã®ã ã
ã§ãã使ãåæãä»ã²ã¨ã¤ã ã£ãã ä¾ãã°groovyservã使ã£ã¦ãã¹ã¯ãªããã§æ¸ãããµã¼ããèµ·åãCtrl-cã§åæ¢ããã¨ããã¼ãã解æ¾ãããªãã£ããããããã ã
ã¡ãªã¿ã«ãååèµ·åã§ãµã¼ããèµ·åã次åããã¯ãã®ãµã¼ãã§å®è¡ãããã¨ã§ã³ãã³ãã®èµ·åãé«éåããæ¹æ³ã¯NailGunãGradleã§ããã£ã¦ãããçµæ§ã¡ã¸ã£ã¼ãªããã ã
ã ãããã®æ¹æ³ä»¥å¤ã«é«éåã®æ¹æ³ããªãã®ã ããããã£ã¦ãã¨ã§èª¿ã¹ãããã£ããä¸è¨ãªã³ã¯ã«ããã¨JVMã®è¨å®ã§Clojureãé«éã«èµ·åã§ãããããããªãgroovyã ã£ã¦é«éã«èµ·åã§ããã¯ãã ã
http://tnoda-clojure.tumblr.com/post/51495039433/jvm-clojure-30
ã¨ãããã¨ã§ãé«éåãªãã·ã§ã³ãè¨å®ãã¦JVMãèµ·åããããã«groovyã³ãã³ããæ¹é ããã
追å ããjavaèµ·åãªãã·ã§ã³
- -client
- -Xverify:none
- -XX:+TieredCompilation
- -XX:TieredStopAtLevel=1
ã¤ã³ã¹ãã¼ã«æé
- ãã®gistã®ã·ã§ã«ã¹ã¯ãªããããã¹ã®éã£ãã¨ããã«ç½®ãã
- ã·ã§ã«ã¹ã¯ãªããã®GROOVY_VERSIONã«ãã¤ã³ã¹ãã¼ã«ããã¦ããgroovyã®ãã¼ã¸ã§ã³ãæå®ããã
â»ã·ã§ã«ã¹ã¯ãªãããªã®ã§windowsã ã¨åããªãï¼cygwinãªãåããï¼ã
æ©è½å¶é
å°ãã§ãé«éåããããã«èµ·åæã«~/.groovy/setupãã¡ã¤ã«ã¯èªã¿è¾¼ã¾ãªãããã«ããããªã®ã§ãgrapeã®ãããã·è¨å®ã¨ããsetupãã¡ã¤ã«ã«æ¸ãã¦ããå ´åã¯é常ã®groovyã³ãã³ãã使ããããªãã
ãã®ä»
- ã³ãã³ãåã®ç±æ¥ã¯ããããã
- dripã使ãã°JVMã®èµ·åãé«éåã§ããããããgyã®javaã³ãã³ããdripã«å¤æ´ãããã¨ã§ããã«é«éåã§ãããããããªãã
- æ¬å½ãªãã¤ã³ã¹ãã¼ã«ããã¦ããgroovyã®æ å ±ããgyã³ãã³ããèªåã§ä½æãã¹ãã ã¨æããä¸ã ãã¼ã¸ã§ã³æå®ããªãã¦ãããã
ãããã·ãµã¼ãçµç±ã§Groovyã®Grapeã使ã
ä¼ç¤¾ã§ã¯ãããã·çµç±ãããªãã¨ã¤ã³ã¿ã¼ãããã«ç¹ããªãã ãªã®ã§ãGrapeã使ãã¨ãããããã·ã®è¨å®ãå¿ è¦ã ã
Grapeã§ãããã·ã使ãå ´åã¯2éãããã å ¬å¼ã«ããã¨groovyã³ãã³ãã使ãã¨ãã«ãªãã·ã§ã³ãä»ããããç°å¢å¤æ°JAVA_OPTSãè¨å®ãããã ã
groovy -Dhttp.proxyHost=yourproxy -Dhttp.proxyPort=8080 yourscript.groovy
JAVA_OPTS = -Dhttp.proxyHost=yourproxy -Dhttp.proxyPort=8080
ã§ããã®JAVA_OPTSã¯ã©ãã«æ¸ããããããã ããããgroovyã«ã ããã®ç°å¢å¤æ°ãé©ç¨ããããã ãã©ã
ãããªå ´å㯠~/.groovy/startup ãã¡ã¤ã«ã«æ¸ãã groovyã³ãã³ãå®è¡æã«ãã®ã¹ã¯ãªãããèªåçã«å¼ã³åºãããã«ãªã£ã¦ããã ãªã®ã§ä¸ã®è¨è¿°ãstartupãã¡ã¤ã«ã«æ¸ãã°ããã
JAVA_OPTS="-Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=user =Dhttp.proxyPassword=password"
ã¡ãªã¿ã«ãwindowsã®cygwinç°å¢ã§groovyãå®è¡ããéãstartupãã¡ã¤ã«å ã§æ¹è¡ããã«ã¯æ³¨æãå¿ è¦ã æ¹è¡ã³ã¼ããÂ¥rÂ¥nã«ãªã£ã¦ããã¨ãÂ¥rã®ããã§æåãæ¶ããã ãã®ããã§ãã¡ãããããã£ãã
ã³ã¡ã³ãã¯æ¸ããªãã¦ãããJavadocãæ¸ãã¦ãã
ä¼ç¤¾ã®ã¨ããããã¸ã§ã¯ãã§ã³ã¼ãã£ã³ã°ã®å¯èªæ§ã«é¢ããåå¼·ä¼ãå®æ½ãã¦ããããã
ãã®ããã¸ã§ã¯ãã¯ãªãªã¼ã¹ã¯çµãã£ã¦ä¿å®ã«ç§»è¡ãã¦ãã ãã©ä»æ´ããåå¼·ãã¦ãã®ã£ã¦æããå端ãªããæ°äººåããï¼
ãã®ããã¸ã§ã¯ãã¯ãã¡ããçä¸ãã¾ãã£ã¦ããä»ãçä¸ãã¦ãã¨æãã
ãã¦ããã®åå¼·ä¼ã®è³æãè¦ãæ©ä¼ããã£ã¦ã ãããã¯ããããå½ããåã®ãã¨ãæ¸ãã¦ããã¡ã½ããã®è¡æ°ã¨ãããããã®ã
ã§ãè³æã®ä¸ã«ã³ã¡ã³ããæ¸ãã£ã¦ããé
ç®ããã£ã¦Javadocãæ¸ãã£ã¦è¨ãé
ç®ããªãã£ãã®ã«é©ããé ããªãã£ãã
ã³ã¡ã³ããªããããJavadocã®æ¹ã大äºãªãã ã£ã¦ãã¨ãããã£ã¦ãªãã¿ããã
ã¨ãããããã®ããã¸ã§ã¯ãã§ã¯Javadocãæ¸ããªã主義ã ã£ãããããå
¨ãæå³ãããããªããæãããããã
ã¨ãåç½®ããé·ããªã£ã¦ãã¾ã£ããã ã³ã¡ã³ããæ¸ãããJavadocï¼privateã¡ã½ãããå«ãï¼ãæ¸ãæ¹ããããã ã£ã¦ãã¨ããã£ã¨ã¾ã¨ãã¦ã¿ããã¨æãã
Javadocã®æ¹ãã³ã¡ã³ããããä½ãæ¸ãã¹ãããã¯ã£ãããã¦ãã
Javadocã®æ¹ããã©ã¼ãããã決ã¾ã£ã¦ããããã¡ã½ããå ¨ä½ã«ã¤ãã¦ã®ã³ã¡ã³ããæ¸ããããªãã®ãªã®ã§ã æå³ã®ãªãæ å ±ãæ¸ããã¨ãå°ãªãã
æå³ã®ãªãæ å ±ã£ã¦ä½ãï¼ä¾ãã°ããããã®ã
i++; // å¤ãï¼ã ãå¢å ããã
ããããã®ãæ¸ãã¦ãã¾ãã®ã¯ã³ã¼ãä¸ã¤ä¸ã¤ã«å¯¾ãã¦ã³ã¡ã³ããå¿ è¦ã ã¨æã£ã¦ããããã ãã¨ãã³ã¡ã³ãã¯éè¦ãªãã¨ã ãæ¸ãã£ã¦è¨ãããã£ã¦ãä½ãéè¦ãªã®ãã¯çµé¨ç©ã¾ãªãã¨ããããªãã¨æãã
Javadocã®æ¹ãã³ã¡ã³ããã¾ã¨ã¾ã£ã¦ããã®ã§ã³ã¼ããèªã¿ããã
ã³ã¼ãã®éä¸ã«ã³ã¡ã³ããæ£å¨ãã¦ããã®ãããè¦ãããã
ãããããèªã¿ã¥ãããç¹ã«ã³ã¡ã³ããã³ã¼ãã®æ¨ªã«æ¸ãã¦ããªããã®ã¯ã
ä¾ãã°ããããã®ã
// ãªãã¸ã§ã¯ãã®æ°ã ãã«ã¼ããã for (int i = 0; i < objs.length; i++) { // ãªãã¸ã§ã¯ããæå¹ã§ããå ´å if (objs[i].isValid()) { // å®è¡ç¨ã®ã¡ã½ãããå¼ã¶ objs[i].exec(); } }
Javadocã ã¨å¥éããã¥ã¡ã³ããçæã§ãã
ã½ã¼ã¹ã³ã¼ãããä»æ§æ¸ãä½æã§ããã£ã¦è¨ãã®ãJavadocã®é
åã ããã
privateã¡ã½ããã§ãã£ã¦ãå¥éããã¥ã¡ã³ãã§ããã®ã¯ãããã¨ã ã
ä»æ§æ¸ã¨ã½ã¼ã¹ã³ã¼ããå¥ã
ã«ç®¡çãããã人ã£ã¦ãããã ãããï¼
管çã大å¤ã ããã§ããã ãã½ã¼ã¹ã³ã¼ãä¸ã«ä»æ§æ¸ãæ¸ãããã«ãã¹ãã ã¨æãã
ãã¡ããåºæ¬è¨è¨ä»æ§ã¨ãä¸æµã®ãã®ãªãå¥ææ¸ã§ç®¡çãã¦ããããã©ã
æè¿èªã¿è¿ãã¦ãã人æã®ç¥è©±ã«ãä»æ§æ¸ã¯å¥ç®¡çããã¨å¤§å¤ã ããã½ã¼ã¹ã³ã¼ãã«æ¸ãã¿ãããªãã¨ãæ¸ãã¦ãã£ãã
Javadocãæ¸ããã¨ãæèããæ¹ãè¯ãã³ã¼ããæ¸ãã
ã³ã¡ã³ãã¯å¤ããã¦ãå°ãªããã¦ããããªãã ã³ã¡ã³ããå¤ã人ã®ç¹å¾´ã¯ã³ã¼ããã¾ã¨ã¾ã£ã¦ãªãã¦ã³ã¼ãããããã«ãããããããã³ã¡ã³ãã§è£ãã¨ãããã¨ããã¦ãã ã¾ããã³ã¡ã³ãã大éã«æ¸ã人ã¯ã¡ã½ãããé·ãæ¸ãå¾åã«ããã¨æãã
ä¾ãã°ããããã®ã
public void doSomething() { // xxxãªãã¸ã§ã¯ãããç¹°ãè¿ããã¼ã¿ãåå¾ãã // åå¾ããã®ã¯ããããã®çç±ããããã ... ... ... // oooãªãã¸ã§ã¯ãã«ãã¼ã¿ãè¨å®ãã // è¨å®ããã®ã¯ããããã®çç±ããããã ... ... ... // fooã¨barãå®è¡ãã ... ... ... }
æ®éã¯åã³ã¡ã³ãã«æ¸ãã¦ããå¦çãprivateã¡ã½ããã«åãåããã¹ããªãã ãã ã³ã¡ã³ããæ¸ãã¦ããããã¨ãããã¨ã§privateã¡ã½ããã«åãåãããã¨ã横çãã¦ãã ããããå ´åã¯privateã¡ã½ããã«åãåãã¦ãã¡ãã¨Javadocãæ¸ããªãã¨ãããªãã
ã¾ã¨ã
publicã¡ã½ããã¯åºæ¬çã«ã¯Javadocãå¿ é ï¼ãã¡ããä¾å¤ããããä¾ãã°getter/setterã¨ãï¼ã§privateã¡ã½ããã¯ä»»æã ã§ããã³ã¡ã³ããæ¸ãããããªãprivateã¡ã½ããã§ãJavadocãæ¸ããæ¹ãã¡ãªããã¯å¤§ããã ã ããããªãã ãJavadocãæ¸ããã¨ãããã¨ãæèããæ¹ãããã ãã¡ããã³ã¡ã³ããæ¸ããã»ããããå ´åããããã ããåªå 度çã«ã¯ä¸ã®ãããªé ããªã
Java8ã®ã©ã ãå¼ããããããã解説
ã§ãããã©ããããããããã£ã¦ã¿ãã
ãã£ããã¯ä¼ç¤¾ã®äººãJava8ã®ã©ã ãå¼ã¯é£ããã¨è¨ã£ã¦ããããã
確ãã«é¢æ°åè¨èªããããªãJavaããå¦ã¶ã®ã¯é£ããããããªãããªã
ãªãé£ããæããã®ãã¨ããã¨ãJavaã¯ãªãã¸ã§ã¯ãæåè¨èªãåæã«è¨è¨ãã¦ããã®ã«é¢æ°åè¨èªã®æ¦å¿µãç¡çç¢çããè¾¼ãã ããã Javaã¯è¯ããæªããäºææ§ã大äºã«ãããããæ¢åã®æ§æãå´©ããé¢æ°åãå©ç¨ã§ããããã«ããã¨ä½¿ãåæãæªããªãã å ãããªãã¸ã§ã¯ãæåã¨é¢æ°åã®ä¸¡æ¹ã®æ¦å¿µããã¼ã¹ã«è¨è¨ãã¦ããããã£ã¨ãããããããªã£ã¦ãã¨æããã©ã
ãã¦ãæ¬é¡ã
Javaã®ã©ã ãå¼ã説æããã«ããã£ã¦ãGroovyãæ¯è¼ã«ãã¦èª¬æãããã¨æãã
ãªãGroovyãï¼Javaã«è¿ãããã©ã ãå¼ã®æ¦å¿µãããããããããã
Groovyã ã¨ã©ã ãå¼ã¯ããæ¸ããã
// å®ç¾© Closure increment = { x -> x + 1 } // å¼ã³åºã increment(1)
Javaã ã¨ããã ã
// å®ç¾© Function<Integer, Integer> increment = (x) -> { return x + 1; }; // å¼ã³åºã increment.apply(1);
ã¾ããã»ã¨ãã©åãã ã
ãã ãGroovyã¨éã£ã¦Javaã«ã¯applyã¨ãããã£ã¦ããªããããããã£ã¦æãããããªãã ãããã
Groovyã§ã¯Closureåãã©ã ãå¼ãæ§æãããããã¦Closureã¯ãããã¯{}ã®ãã¨ã ã
ãã®ãããã¯ã£ã¦ã©ããã§è¦ããã¨ããªãã ããããããåæååã ï¼...ãããªãã¦ã¡ã½ããã ï¼
ã¤ã¾ããã¡ã½ããã®ä¸èº«ãClosureã£ã¦ãã¨ã ãã¡ã½ããã®ä¸èº«ããªãã¸ã§ã¯ãã¨ãã¦ä½¿ããããããé¢æ°åè¨èªãªãã ã
ãªã®ã§Closureã¯ã¡ã½ããã¨åãããã«å¼æ°ãåãåãããã³ã¼ãã§è¨ãxãããã ã
ã¾ããClosureãå¼ã³åºãå ´åãã¡ã½ããã¨ä½ãå¤ãããªããå¼æ°ã«å¤ãå
¥ãã¦å¼ã³åºãã ãã ã
ä¸æ¹Javaã¯Closureåãããªãã¦Functionåã§ãã¸ã§ããªã¯ã¹ã使ã£ã¦ããããã
Groovyã¯åçåä»ããªã®ã§å¼æ°ãæ»ãå¤ã®åã¯æ¸ããªãã¦è¯ãã£ãã
ã§ããJavaã¯éçåä»ããªã®ã§å¼æ°ãæ»ãå¤ã®åã¯æ¸ããªãã¨ãããªãã
Groovyã®ã©ã ãå¼ã®æ§æã¯ãããã¯{}ã¨å¼æ°ã®ããã®ç¢å°->ã ãã§è¯ãã£ãããJavaã¯ããå°ãè¤éã ã
å¼æ°ã«æ¬å¼§()ãå¿
è¦ãªã®ã¨ãããã¯{}ã®ä½ç½®ãéããã¾ãããã ããªãã
ãããJavaã®ã©ã ãå¼ã®åºæ¬å½¢ã§ãå¼æ°ãä¸åã ãã ã£ããæ¬å¼§()ãçç¥ã§ããããæãä¸æã ãã ã£ãããããã¯{}ã¨returnãçç¥ã§ããããããGroovyã¯ã¡ã½ããã§ãClosureã§ãreturnæãçç¥ã§ãããã ãã©ãã
ããã¦ãè¬ã®applyã ãããã¯ä¸ä½ãªããªã®ãï¼
ãããJavaããªãã¸ã§ã¯ãæåããé¢æ°åã®ãã©ãã¤ã ãå¯è½ã«ããéã®ã²ãã¿ã ã¨ä¿ºã¯æãã
Javaã«ã¯é¢æ°ã表ç¾ãããªãã¸ã§ã¯ããªãã¦ãã®ã¯ãªãããã åã«é¢æ°åã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ãããã®ãããã ãã ã
ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ã©ã ãå¼ã表ç¾ãããã¨ãã¦ãã ãã ã
ã¤ã¾ããä¸ã®Javaã®ã³ã¼ãã¯ãããããã¨ã ã
Function<Integer, Integer> increment = new Function<Integer, Integer>() { @Override public Integer apply(Integer x) { return x + 1; } };
ããã¯ããããç¡åã¯ã©ã¹ã ãã¤ã¾ããJavaã®ã©ã ãå¼ã®æ£ä½ã¯ãªãã¦ãã¨ã¯ãªãããã ã®ç¡åã¯ã©ã¹ã ã£ãã¨ãããã¨ã ã
ãããããå°ãç°¡æ½ã«æ¸ããææ³ã§è¡¨ç¾ããã®ãã©ã ãå¼ã£ã¦ãããã®ãªãã ã
ã ããapplyã£ã¦ããã®ã¯ã©ã ãå¼ã§æé»ã§ä½ã£ãç¡åã¯ã©ã¹ã®applyã¡ã½ãããå¼ãã§ããã ããªãã ã
ãããã£ããã¨ãããJavaã®ã©ã ãå¼ã¯Groovyã¨ã¯éã£ã¦ä»ã®Javaã®ä»æ§ã«ç¡çç¢çå½ã¦ã¯ãããã®ã¨ããæããããªãã§ããªãã
Javaã®ãããããã¯ããå°ãç¶ããããã¯ã©ã ãå¼ã表ç¾ããåã«ããã
ã¤ã¾ããä¸ã§è¨ãFunctionåã ãããã¯ä¸ã¤ã®å¼æ°ã¨ä¸ã¤ã®æ»ãå¤ã®é¢æ°ã表ãåã§ããã
ããããä¸ã¤ã®å¼æ°ã¯åããã©æ»ãå¤ã¯å¿
è¦ãªãå ´åã¯ã©ãããããããã ãããã
ãã¡ããFunctionåã§è¡¨ç¾ã§ããªãããªããè¦ã¯æ»ãå¤ãVoidåã«ããã°ãããã ããã
ã§ãJavaã«ã¯ããå°ç¨ã®åããããã ãããããããããã¨ã«ã
ããã¯Consumeråã ã
ããããæ»ãå¤ã ãåãåãããå ´åã¯ï¼Supplieråã ãå¼æ°ã2ã¤åãåãããå ´åã¯ï¼BiFunctionåã ã
...ã¨ããããã«åããã£ã±ããããä¸æ¹Groovyã§ã¯Closureåã ãã§è¡¨ç¾ã§ããã
以ä¸ã®ããã«ãGroovyã¨æ¯è¼ãããã¨ã§ã©ã ãå¼ã®çæ³(Groovy)ã¨ç¾å®(Java)ãè¦ãã¦Javaã®ã©ã ãå¼ã®ç解ãæ·±ã¾ã£ãããªï¼ã¨æããJavaãæ¬å½ã¯Groovyã¿ãããªã©ã ãå¼ãæ¸ããããã«ãããã£ããã ã¨æããã ãã©ããããããäºææ§ã®åé¡ãããããªå½¢ã«ãªã£ã¦ãã¾ã£ããã ã¨æãã
ãªããGroovyãæã¡ä¸ãã¦ããããªæç« ã«ãªã£ã¦ãã¾ã£ãã
å¥ã«C#ã¨æ¯è¼ãã¦ããããã ãã©ãC#ã¯ä¿ºã¯ããããã
Lispã ã¨èª¬æãè¤éã«ãªãããªãã
ã¤ã³ããã¯ã¹è¨è¨ã®åºæ¬æ¹é
ã¤ã³ããã¯ã¹ã®æé©ãªå®ç¾©ã®ä»æ¹ãããããã
ä¼ç¤¾ã§ã¯ãè¦ãæãæ¤ç´¢ã§ä½¿ãã«ã©ã ã¯ä½ã§ãããã§ãã¤ã³ããã¯ã¹ãä½æãã¦ããããã ã ãããªæãã§éé²ã«ã¤ã³ããã¯ã¹ãä½æããã¢ã³ããã¿ã¼ã³ãSQLã¢ã³ããã¿ã¼ã³ã§ã¯ãã¤ã³ããã¯ã¹ã·ã§ããã¬ã³ãã¨å½åãã¦ããããããã¼ãã³ã°ã»ã³ã¹ã ã¨æãã
ãã®ãã¿ã¼ã³ã®å ´åã®è§£æ±ºçã¯EXPLAINæãªã©ã使ã£ã¦ãã¡ãã¨ã¤ã³ããã¯ã¹ãè©ä¾¡ããã£ã¦ãã¨ã ã ã¾ãããããããªãã ãã©ãããã©ãããããªããåºæºããªãã®ãã¨ãããã¨ã§ãèãã¦ã¿ãï¼ã¤ã¼ã調ã¹ãï¼ã
ã¤ã³ããã¯ã¹ãä½æããæ¹ããããã®
- æ¤ç´¢ã§ããå©ç¨ãããã«ã©ã ãNOT NULLã§ãããLIKEã使ã£ã¦å¾æ¹ä¸è´ãé¨åä¸è´ã§æ¤ç´¢ãã¦ããªããã®ï¼åæ¹ä¸è´ã¯åé¡ãªãï¼ããã¨å¦å®ã¨ãORã使ã£ã¦ããªããã®
- ORDER BYã§ããå©ç¨ãããã®ï¼B-Treeã¤ã³ããã¯ã¹ã®ããã«æ¨æ§é ã ã¨ã½ã¼ãæ¸ã¿ãªã®ã§å¹çãã使ããï¼
- JOINã§ä½¿ãã«ã©ã
ã¤ã³ããã¯ã¹ãä½æãã¦ã¯ãããªããã®
- 主ãã¼ï¼ã»ã¨ãã©ã®DBã§èªåçã«ä½æãããããï¼
- å¤é¨ãã¼ï¼ã»ã¨ãã©ã®DBã§èªåçã«ä½æãããï¼ï¼
- Enumã®ã«ã©ã ï¼å¤ã®åå¸ãå°ããããã¤ã³ããã¯ã¹ã®æ©æµãå¾ãããªãã«ãããããã容éãé£ãããï¼
æ£ç´Enumã«é¢ãã¦ã¯èªä¿¡ããªããããã¶ãä½æããªãæ¹ãå¹ççãªããããªãããªã
MyBatisã§Enumã使ã
MyBatisã§Enumã使ãã«ã¯ã©ãããã°ããã®ãã
ãã¡ããããã¥ã¢ã«ã«æ¸ãã¦ããããã¾ã¨ããã¨ããã ã
- resultMapã§Enumã使ãã¨ãã¯typeHandlerãæ¸ã
- parameterã§Enumã使ãã¨ãã¯mybatis-config.xmlã«handlerãå®ç¾©ãã
å ·ä½ä¾ãããã¥ã¢ã«ããå¼ç¨ããã¨
<resultMap type="org.apache.ibatis.submitted.rounding.User" id="usermap2"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="funkyNumber" property="funkyNumber"/> <result column="roundingMode" property="roundingMode" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/> </resultMap> <!-- mybatis-config.xml --> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/> </typeHandlers>
ãã®æ¹æ³ãresultMapã¯ãããã©parameterã使ãã¨ãã«ããããmybatis-config.xmlã«handlerãå®ç¾©ããã£ã¦ããã®ã¯ããã©ããããªããã俺ã¯ããã©ãããã
ãªã®ã§ãmybatis-config.xmlã«handlerãå®ç¾©ããªãã¦æ¸ãæ¹æ³ããªããæ¢ããã çµæããã£ãã«ã¯ãã£ããæ£æ»æ³ãããªãã£ã½ãã®ã§æ³¨æãå¿ è¦ã
è¦ã¯typeHandlerã®ä»£ããã«ordinalã使ãã£ã¦ãã¨ã ã
ã¤ã¾ããããã
#{roundingMode} <!-- mybatis-config.xml --> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/> </typeHandlers>
ãããã
#{roundingMode.ordinal}
ã¡ãªã¿ã«ordinal()ã ã¨ãã¾ããããªãã ã©ãããã#{}ã®ä¸ã§ã¯Reflectionã§ãã£ã¼ã«ããåç §ãããã¨ãã§ãããããã
ä¸ã®ä¾ã§ã¯EnumOrdinalTypeHandlerã®å ´åã ããEnumTypeHandlerã使ãããå ´åã¯ordinalãæ¸ããªãã¦ãããåãEnumã ã¨ããã©ã«ãã§EnumTypeHandlerã使ãããã
ãã¨ãparameterã1ã¤ã ãã§ãããEnumã®å ´åã¯@Paramã¤ããæ¹ããããã¤ããªãã¨ãã¾ãåããªãã£ãã
JPAã¨N+1åé¡
JPAã¨ã¯
JPAï¼Java Persistence APIï¼ã¨ã¯JavaEEã®ããã«å®ç¾©ãããæ°¸ç¶åï¼persistenceï¼ã«é¢ããAPIä»æ§ã§ããJPAã¯APIä»æ§ãªã®ã§JPAåä½ã§ã¯åãã¾ãããJPAãå®è£ ããHibernateãEclipseLinkãªã©ã®O/R Mapperãå¿ è¦ã«ãªãã¾ãã
N+1åé¡ã¨ã¯
N+1åé¡ã¨ã¯O/R Mapperãå©ç¨ããéã«SQLæãæå³ãã大éã«çºè¡ããã¦ãã¾ãåé¡ã§ãããã®åé¡ã¯O/R Mapperã«ããSQLæã®èªåçæã«èµ·å ãã¾ããObjectå´ã®é¢é£ãå«ãã¦ãã¼ã¿ãåå¾ããå ´åã«ãO/R Mapperã¯ä¸è¨ã®ããã«N+1åSELECTæãçºè¡ãã¦ãã¾ãã¾ãã
- TableããNåã®Recordãåå¾ããããã«SELECTæã1åçºè¡
- Nåã®Recordãé¢é£ãããã¼ã¿ãåå¾ããããã«SELECTæã1åãã¤ãè¨Nåçºè¡
N+1åé¡ã®è§£æ±ºç
N+1åé¡ã解決ããã«ã¯ãã¼ãã«ãJOINãã¦ãã¼ã¿ãåå¾ããå¿ è¦ãããã¾ããå ·ä½çãªè§£æ±ºçã¯å¤§ããåãã¦2ã¤ã§ãã
- JPQLã§JOIN FETCHã使ç¨ãã
- @Fetchã¢ããã¼ã·ã§ã³ã使ç¨ãã
ãã ããå¾è ã®æ¹ã¯JPAã®ä»æ§ã«ã¯ãªããå®è£ ã§ããHibernateã«ä¾åãã¾ããEclipseLinkã®å ´åã¯@JoinFetchã使ç¨ãã¾ãã
注æï¼ä¸è¨ã§èª¬æãã解決çã¯JPA + Hibernateç°å¢ã®å ´åã®ãã®ã§ããEclipseLinkã使ç¨ããå ´åã§ãåæ§ã®æ¹æ³ã§è§£æ±ºã§ããã¨ã¯éãã¾ããã
1. JPQLã§JOIN FETCH ã使ç¨ãã
JPAã§ã¯ä¸è¨ã®4ã¤ã®æ¹æ³ã§DBã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã
- Entityã®ã¡ã½ãã(persist(), find(), remove(), merge())
- JPQLï¼JPAç¨ã®SQLãSQLæã®æ¹è¨ãå¸åããããã®ãã®ï¼
- Criteriaï¼Javaã®åãå©ç¨ãã¦ã¯ã¨ãªãä½æããããã®ãã®ï¼
- Native SQL
ãã®ä¸ã®JPQLã使ã£ã解決çã§ããJPQLã§ã§ãããã¨ã¯Criteiaã§ãã§ãã¾ãããCriteriaã§ã®è§£æ±ºæ¹æ³ã¯ããã§ã¯è§¦ãã¾ããã
åæ¹åé¢é£ã®å ´å
N+1åé¡ã¯é¢é£ãããããã«çºçãã¾ããããã§è¨ãé¢é£ã¯Objectå´ã®é¢é£ã§ãã å ·ä½çã«ã¯ä¸è¨ã®ãããªã³ã¼ãã§ãã
public class Parent { private List<Child> children; ... } public class Child { ... }
ä¸è¨ã§ã¯Parentã ããChildãåç §ãã¦ããããåæ¹åé¢é£ã¨å¼ã³ã¾ããããã«Parentã¯ChildãListåã§è¤æ°æã£ã¦ããããã1対å¤ï¼One to Manyï¼ã®é¢é£ã¨å¼ã³ã¾ãã
åæ¹åé¢é£ã®å ´åã«N+1åé¡ãçºçããã®ã¯ãParentã¨é¢é£ãã¦ããchildrenãO/R Mapperãåå¾ããã¨ãã§ãããããã£ã¦ãLazy Fetchã®å ´åã¯Parentãªãã¸ã§ã¯ããåå¾ããã ãã§ã¯SELECTæã¯çºè¡ããã¾ãããParentãªãã¸ã§ã¯ãããchildrenãåå¾ãããã¨ããå ´åã«åãã¦Nåã®SELECTæãçºè¡ããã¾ãã
JPQLã§ä¸è¨ã®ããã«JOIN FETCH
ã使ããã¨ã§N+1åé¡ã解決ã§ãã¾ãã
SELECT p FROM Parent p JOIN FETCH p.children
ãã®ã¨ãçºè¡ãããSELECTæã¯1åã§ããJOIN FETCH
ã¯INNER JOINã®SELECTæã¨ãã¦çºè¡ããã¾ããOUTER JOINã使ç¨ãããå ´åã¯ä¸è¨ã®ããã«ãªãã¾ãã
SELECT p FROM Parent p LEFT OUTER JOIN FETCH p.children
åæ¹åé¢é£ã®å ´å
åæ¹åé¢é£ã¨ã¯2ã¤ã®ãªãã¸ã§ã¯ãããäºãã«åç
§ãã¦ããç¶æ
ã®ãã¨ã§ãã
å
·ä½çã«ã¯ä¸è¨ã®ãããªã³ã¼ãã§ãã
public class Parent { private List<Child> children; // One to Many ... } public class Child { private Parent parent; // Many to One ... }
ä¸è¨ã¯1対å¤ã®é¢é£ã§ãããChildããè¦ãå ´åã¯å¤å¯¾1ï¼Many to Oneï¼ã®é¢é£ã¨å¼ã³ã¾ãã
åæ¹åé¢é£ã®å ´åã§ã¯Parentãåå¾ããå ´åã®N+1åé¡ã ããèããã°è¯ãã£ãã®ã§ãããåæ¹åé¢é£ã®å ´åã¯Childãåå¾ããå ´åã®N+1åé¡ãèããªããã°ããã¾ãããChildã¯Parentãä¿æãã¦ããããã®Parentã¯Childãè¤æ°ä¿æãã¦ãã¾ãããããã£ã¦ãChildãæã¤Parentã®é¢é£ãJOIN FETCH
ã§åå¾ãããã¨ã§Childã®N+1åé¡ã解決ã§ãã¾ãã
SELECT c FROM Child c JOIN FETCH c.parent p JOIN FETCH p.children
2. @Fetchã¢ããã¼ã·ã§ã³ã使ç¨ãã
@Fetchã¢ããã¼ã·ã§ã³ã¯ä¸è¨ã®ããã«ä½¿ç¨ãã¾ãã
@Entity public class Parent { @OneToMany(mappedBy="parent") @Fetch(FetchMode.SUBSELECT) private List<Child> children; ... }
@Fetchã«ã¯è¤æ°ã®FetchModeãããã¾ããFechModeã®ç¨®é¡ã«ãã£ã¦SELECTæã®çºè¡æ°ãç°ãªãã¾ãã
FetchMode | SELECTæã®çºè¡æ° |
---|---|
SELECT | N+1 |
JOIN | 1 |
SUBSELECT | 2 |
FechModeã®è©³ç´°ã¯ä¸è¨ãªã³ã¯ãåç
§ãã¦ãã ããã
http://www.solidsyntax.be/lessons-learned/hibernate-fetchmode-explained-example/
ãã ããFetchMode.JOINã¯JPQLã使ç¨ããå ´åã¯æ£ããåä½ãã¾ãããCriteriaã使ç¨ããå ´åã¯ä¸è¨ã®ããã«1åã ãSELECTæãçºè¡ãã¾ãããJPQLã®å ´åã¯N+1åãSELECTæãçºè¡ãã¦ãã¾ãã¾ããããã¯Hibernateã®æ¢ç¥ã®ãã°ã®ããã§ãã
http://stackoverflow.com/questions/18891789/fetchmode-join-makes-no-difference-for-manytomany-relations-in-spring-jpa-reposi
@Fetchã使ç¨ããã®ã§ããã°FetchModeã¯SUBSELECTãé¸æããæ¹ãè¯ãã¨æãã¾ãã
ãã ããSUBSELECTã«ãåé¡ã¯ããã¾ãããã®åé¡ã¯SUBSELECTãåæ¹åé¢é£ã§ä½¿ç¨ããå ´åã«çºçãã¾ããMany to Oneã®ãªãã¸ã§ã¯ãï¼ä¸è¨ä¾ã§ã¯Childï¼ãåå¾ãããã¨ããå ´åãSUBSELECTã使ç¨ããã¨N+M+1åã®SELECTæãçºè¡ããã¦ãã¾ãã¾ããMã¯One to Manyï¼ä¸è¨ä¾ã§ã¯Parentï¼ã®ã¬ã³ã¼ãæ°ã§ãã
ãã®ç¾è±¡ã¯ãã®ã³ã¼ãã使ç¨ãã¦ç¢ºèªãã¾ãããããããããããããäºãã«é¢é£ãåå¾ãããã¨ãã¦ããã®ãåå ã ã¨æãã¾ããã詳ããã¯ãããã¾ããã
ã¾ã¨ã
N+1åé¡ã解決ãããå ´åã¯JOIN FETCH
ã使ç¨ãã¹ãã§ãã@Fetchã¢ããã¼ã·ã§ã³ã¯JPAã§ã¯å®ç¾©ããã¦ããããHibernateã®ãã°ãããã®ã§ä½¿ç¨ããªãæ¹ãç¡é£ã§ãã
ã¾ããå¯è½ã§ããã°åæ¹åé¢é£ã®ãªãã¯ã©ã¹è¨è¨ãããæ¹ãè¯ãã¨æãã¾ããåæ¹åé¢é£ã¯åæ¹åé¢é£ã«æ¯ã¹ã¦N+1åé¡ãçºçããããããããé²ãããã«èæ ®ãã¹ãç®æãå¢ããããã³ã¼ãã£ã³ã°ãé£ãããªãã¾ãã