Spring Boot ã§ã¡ã¼ã«éä¿¡ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( ã¡ã¼ã«éä¿¡ç»é¢ã®ä½æï¼ )
æ¦è¦
Spring Boot ã§ã¡ã¼ã«éä¿¡ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( ã¡ã¼ã«éä¿¡ç»é¢ã®ä½æï¼ ) ã®ç¶ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ã¡ã¼ã«éä¿¡ç»é¢ã®ä½æ
- ä»å㯠Doma 2 ã使ç¨ããã¡ã¼ã«ä¿åå¦çãå®è£ ãã¾ãã
- Doma 2 ã¯ä½¿ãæ¹ã調æ»ããã®ã«è¦å´ãã¾ããããã³ã¼ãã¸ã§ãã¬ã¼ã¿ãããã®ã¨ãSQL ãã¡ã¤ã«ã使ããã®ãé åã§ããã
ã½ããã¦ã§ã¢ä¸è¦§
åèã«ãããµã¤ã
-
Welcome to Doma
http://doma.readthedocs.org/ja/stable/ -
Spring @Component vs @Configuration for basic configuration class
http://stackoverflow.com/questions/18976634/spring-component-vs-configuration-for-basic-configuration-class- Doma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã Spring ã® DI ã³ã³ããã«ã·ã³ã°ã«ãã³ã§çæã»ç®¡çãã¦ããããã¨æã£ãã®ã§ãããå®è£ æã« @Component 㨠@Configuration ã®ã©ã¡ãã®ã¢ããã¼ã·ã§ã³ãä»ä¸ããã®ãè¯ãã®ãè¿·ã£ãã®ã§ããã®èª¿æ»ãããæã«åç §ãã¾ããã
- ComponentScan ã®å¯¾è±¡ã«ãã¦ä»ã®ã¯ã©ã¹ã« @Autowired 㧠DI ãããã®ã¯ @Component ãã@Bean ãå®ç¾©ããã XMLãã¼ã¹ã®è¨å®ã¨åããã¨ãããå ´åã«ã¯ @Configuration ããã¨ããæãã§ããããï¼
- Doma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã¯ Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã« DI ãããã®ã§ã@Component ã¢ããã¼ã·ã§ã³ã使ç¨ãããã¨ã«ãã¾ãã
IntelliJ Doma support plugin ã® GitHub ãµã¤ã
https://github.com/siosio/DomaSupportIntelliJ IDEA Plugins - Doma Support -> 0.3
https://plugins.jetbrains.com/update/index?pr=idea&updateId=19563Welcome to Doma-Gen
http://doma-gen.readthedocs.org/ja/stable/@AnnotateWithãã¡ã¿ã¢ããã¼ã·ã§ã³ã¨ãã¦ä½¿ç¨å¯è½ã«
http://d.hatena.ne.jp/taedium/20100130/p1- @AnnotateWith ã¢ããã¼ã·ã§ã³ã®èª¬æãè¨è¼ããã¦ãã¾ãã
SpringBootã¨Domaãé£æºãã
http://shinsuke789.hatenablog.jp/entry/2014/07/31/123800- Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã Spring ã® DI ã³ã³ããã§ç®¡çãããããã®å®è£ æ¹æ³ãåèã«ãã¾ããã
SpringBoot+Doma2+Gradleã試ãã¦ã¿ãã
http://qiita.com/nyasba/items/1e22c2401f3849f9071d- dataSource ã Spring ã®ç®¡çãããã©ã³ã¶ã¯ã·ã§ã³ã«åå ãããæ¹æ³ãåèã«ãã¾ããã
æé
1.0.x-make-mailsendform-saveemail ãã©ã³ãã®ä½æ
- IntelliJ IDEA 㧠1.0.x-make-mailsendform-saveemail ãã©ã³ããä½æãã¾ãã
Doma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã®ä½æ
以ä¸ã®ããã¥ã¡ã³ããåèã«ãã¦ãDoma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¾ãã
è¨å®
http://doma.readthedocs.org/ja/stable/config/- Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã Spring ã® DI ã³ã³ããã«ã·ã³ã°ã«ãã³ã¨ãã¦çæã»ç®¡çãããããã« @Component ã¢ããã¼ã·ã§ã³ãä»å ãã¾ãã
- dataSource 㯠ApplicationConfig ã¯ã©ã¹ã«å®ç¾©ãã dataSource Bean ã @Autowired ã¢ããã¼ã·ã§ã³ã§ DI ãã¦ä½¿ç¨ãã¾ãã
- dialect 㯠dialect Bean ãçæãã Java Configuration ã¯ã©ã¹ãå®è£ ããdialect Bean ã @Autowired ã¢ããã¼ã·ã§ã³ã§ DI ãã¾ãã
src/main/resources ã®ä¸ã® application.properties ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/config ã®ä¸ã« DomaBeanConfig.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/config ã®ä¸ã« DomaConfig.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Community Edition ã« IntelliJ Doma support plugin ã¯ã¤ã³ã¹ãã¼ã«ã§ãã¾ããã§ãã
Doma ã®éçºã«ä¾¿å©ãª IntelliJ IDEA ç¨ã®ãã©ã°ã¤ã³ãããã¨æ¸ããã¦ããã®ã§ã¤ã³ã¹ãã¼ã«ãããã¨æãã¾ããããIntelliJ IDEA Plugins ã®ãã©ã°ã¤ã³ã®ãã¼ã¸ ãè¦ã¦ã¿ãã¨ãCommunity Edition ã¯åä½å¯¾è±¡å¤ã§ãããæ®å¿µã§ãããã
Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãSQL ãã¡ã¤ã«ã®ä½æ
Doma ã«ã¯ Doma-Gen ã¨ããã³ã¼ãã¸ã§ãã¬ã¼ã¿ãæä¾ããã¦ãã¾ãã®ã§ããããå©ç¨ã㦠Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãSQL ãã¡ã¤ã«ãèªåçæãã¾ãã
build.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsãã¢ã¤ã³ã³ãã¯ãªãã¯ãã¦ãå¤æ´ãã build.gradle ã®å 容ãåæ ãã¾ãã
Gradle projects View ãã以ä¸ã®å ´æã«ãã gen ã¿ã¹ã¯ãå®è¡ãã¾ãã
- ä¸çªæåã®å®è¡æã¯ã©ã¤ãã©ãªã®ãã¦ã³ãã¼ããè¡ããã¾ããä¸ã®ãã£ããã£ã¯ä½åº¦ãå®è¡ããå¾ã®ãã®ã§ãã©ã¤ãã©ãªã®ãã¦ã³ãã¼ãæã®æ§åã¯ãã£ããã£ããã®ãå¿ãã¦ãã¾ããã
gen ã¿ã¹ã¯ãæ£å¸¸ã«çµäºããã¨ã以ä¸ã®ç»åã® Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãSQL ãã¡ã¤ã«ãä½æããã¾ãã
Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ãã®å¾ä½æãã Service ã¯ã©ã¹ã« @Autowired ã¢ããã¼ã·ã§ã³ã§ DI ãããã®ã§ã@Component ã¢ããã¼ã·ã§ã³ãä»å ãã¦ããã¾ãã
src/main/java/ksbysample/webapp/email/dao ã®ä¸ã® EmailDao.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/dao ã®ä¸ã® EmailItemDao.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Doma-Gen ã§èªåçæããããã¡ã¤ã«ã¯ Git ã®ãã¼ã¸ã§ã³ç®¡ç対象ã¨ãã¦ç»é²ããã¦ããªãã®ã§ç»é²ãã¾ããProject View ã§ã«ã¼ããã£ã¬ã¯ããªãé¸æå¾ãã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãGitã-ãAddããé¸æãããã¡ã¤ã«ã追å ãã¾ãã
Service ã¯ã©ã¹ã®ä½æãController ã¯ã©ã¹ã®å¤æ´
src/main/java/ksbysample/webapp/email/web/mailsend ã®ä¸ã« MailsendService.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/web/mailsend ã®ä¸ã® MailsendController.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèª ( ãã®ï¼ )
åä½ç¢ºèªãã¾ããGradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åãããã¨ãã¾ããã
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailsendController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ksbysample.webapp.email.web.mailsend.MailsendService ksbysample.webapp.email.web.mailsend.MailsendController.mailsendService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailsendService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ksbysample.webapp.email.dao.EmailDao ksbysample.webapp.email.web.mailsend.MailsendService.emailDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ksbysample.webapp.email.dao.EmailDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
ã¨ããã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºåããã¦èµ·åãã¾ããã§ãããã©ãã EmailDao ã MailsendService ã¯ã©ã¹ã« DI ã§ãã¦ããªãããã§ããåå ã調æ»ãã¾ãã調æ»ããçµæã¯ä»¥ä¸ã®éãã§ããã
- Doma 2 ã§ã¯ build æã« apt ã«ãã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ããå®è£ ã¯ã©ã¹ ( ï½DaoImpl ã¯ã©ã¹ ) ãçæããã¾ããapt ã«ã¤ãã¦ã¯ãaptï¼ã¢ããï¼ã¨ã¯ä½ã§ããï¼ ã«èª¬æãããã¾ããã
- çæãããå®è£ ã¯ã©ã¹ãã©ãã«ããã確èªããã¨ãããbuild\classes\main\generated\ksbysample\webapp\email\dao ã®ä¸ã«ä½æããã¦ãã¾ããã
- Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã« @Component ã¢ããã¼ã·ã§ã³ãä»å ãã¦ãå®è£ ã¯ã©ã¹ã«ã¯ @Component ã¢ããã¼ã·ã§ã³ã¯ä»å ããã¦ãã¾ããã§ããã
- Doma 2 ã® User Documentation - è¨å® - ã¢ããã³ã¹ã ãè¦ãã¨ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã« @AnnotateWith ã¢ããã¼ã·ã§ã³ãä»å ããã°ãå®è£ ã¯ã©ã¹ã«ä»å ããã¢ããã¼ã·ã§ã³ãæå®ã§ããããã§ãã
- @AnnotateWith ã¢ããã¼ã·ã§ã³ã§ä»¥ä¸ï¼ç¹ãå®ç¾ã§ããã°ããããã§ãã
- å®è£ ã¯ã©ã¹ã« @Component ã¢ããã¼ã·ã§ã³ãä»å ãããããã«ããã
- å®è£ ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã« @Autowired ã¢ããã¼ã·ã§ã³ãä»å ãããããã«ãããã³ã³ã¹ãã©ã¯ã¿ã« @Autowired ãä»å ãããã¨ãæåã«ä½æãã Doma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ãã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã« DI ã§æ¸¡ãããããã«ãªãã
- @AnnotateWith ã¢ããã¼ã·ã§ã³ã«ããè¨è¿°ã¯é·ãã®ã§ãããå¥ã®ã¢ããã¼ã·ã§ã³ãå®ç¾©ãã¦ããã¦ãããã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ä»å ããã°çãè¨è¿°ã§ããããã§ãã
以ä¸ã®å®è£ ã«å¤æ´ãã¾ãã
- Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ä»å ããããã® @AnnotateWith ã¢ããã¼ã·ã§ã³ãè¨è¿°ããã¢ããã¼ã·ã§ã³ãä½æãã¾ãã
- Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ã¯ä½æããã¢ããã¼ã·ã§ã³ãä»å ãã¾ãã
src/main/java/ksbysample/webapp/email ã®ä¸ã« annotation.dao ããã±ã¼ã¸ãä½æãã¾ãã
src/main/java/ksbysample/webapp/email/annotation/dao ã®ä¸ã« ComponentAndAutowiredDomaConfig.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/dao ã®ä¸ã® EmailDao.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/dao ã®ä¸ã® EmailItemDao.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Ctrl+F9 ãæ¼ã㦠build ãã¾ããbuild\classes\main\generated\ksbysample\webapp\email\dao ã®ä¸ã® EmailDaoImpl.java ãè¦ã㨠ãªã³ã¯å ã®å 容 ã«ãªã£ã¦ãããã¯ã©ã¹ã« @Component ã¢ããã¼ã·ã§ã³ããã³ã³ã¹ãã©ã¯ã¿ã« @Autowired ã¢ããã¼ã·ã§ã³ãä»å ããã¦ãã¾ããã
å度 Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ããã¨ãããä»åº¦ã¯ Tomcat ãæ£å¸¸ã«èµ·åãã¾ãããåä½ç¢ºèªãç¶ãã¾ãã
åä½ç¢ºèª ( ãã®ï¼ )
ãã©ã¦ã¶ãèµ·åã http://localhost:8080/mailsend ã¸ã¢ã¯ã»ã¹ãã¾ãã以ä¸ã®ç»åã®å¤ãå ¥åå¾ããéä¿¡ããã¿ã³ãã¯ãªãã¯ãã¾ãã
ãéä¿¡ããã¿ã³ãã¯ãªãã¯ããã¨ä»¥ä¸ã®ç»åã®ã¨ã©ã¼ãçºçãã¾ãããåå ã調æ»ãã¾ãã
調æ»ããçµæã¯ä»¥ä¸ã®éãã§ããã
- log4jdbc ã§åºåããã SQL ãè¦ã¦ã¿ãã¨ã以ä¸ã®å¦çãå®è¡ããã¦ãã¾ããã
- Connection.setAutoCommit(false)
- Connection.prepareStatement(insert into email .....
- org.seasar.doma.jdbc.command.ModifyCommand.executeUpdate 㧠PreparedStatement.executeUpdate() ãå¼ã³åºã㦠insert æãå®è¡
- Connection.prepareStatement(select currval('email_email_id_seq'))
- org.seasar.doma.jdbc.id.AbstractIdGenerator.getGeneratedValue 㧠PreparedStatement.executeQuery() ãå¼ã³åºã㦠select currval('email_email_id_seq') ãå®è¡
-
org.postgresql.util.PSQLException: ERROR: æ¬ã»ãã·ã§ã³ã§ã·ã¼ã±ã³ã¹"email_email_id_seq"ã®currvalã¯ã¾ã å®ç¾©ããã¦ãã¾ãã
ã®ã¨ã©ã¼ãçºç
- Google ã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ãæ¤ç´¢ãã¦ã¿ãã¨ä»¥ä¸ã®è¨äºãããããã¾ãããå 容ãè¦ã¦ã¿ãã¨ãã©ãã SQL çºè¡ã¨åæã«ã³ãããããã¦ãã¾ã£ã¦ãããããããã§ãã
- DB ã® EMAIL ãã¼ãã«ãè¦ãã¨ãã¼ã¿ã insert ããã¦ãã¾ããã@Transactional ã¢ããã¼ã·ã§ã³ãä»å ãã¦ããã®ã«å¹ãã¦ãã¾ãããããã
â»å®éã«ã¯ä½åº¦ãå®è¡ãã¦ããã®ã§ãemail_id 㯠1 ã§ã¯ãªã 13 ã¾ã§é²ãã§ãã¾ãã
- ãã©ã³ã¶ã¯ã·ã§ã³ãå¹ãã¦ããªãã ãã®ããã«è¦ããã®ã§ SpringBoot+Doma2+Gradleã試ãã¦ã¿ãã ã®è¨äºãåèã«
DataSourceBuilder.create().build()
ânew TransactionAwareDataSourceProxy(DataSourceBuilder.create().build())
ã¨å¤æ´ãã¦ã¿ãã®ã§ãããTomcat ãåèµ·åããã¨java.sql.SQLException: The url cannot be null
ã¨ããã¨ã©ã¼ãã°ãåºåãã㦠Tomcat ãèµ·åãã¾ããã§ããã - 確ãã« dataSource Bean ã®ä¸ã§
DataSourceBuilder.create().build()
ã§çæãã dataSource ã org.apache.tomcat.jdbc.pool.DataSource ã§ãã£ã¹ãããå¾ãè¨å®ããã¦ããå¤ãSystem.out.println(dataSource.getUrl());
ã§åºåãã¦ã¿ãã®ã§ãããnull ã§ãããDataSourceBuilder.create().build()
ã§çæããæã« application.properties ã®è¨å®ãåæ ããã¦ããã¨æã£ã¦ãã¾ããããã©ããããã§ã¯ãªãããã§ããService ã¯ã©ã¹ã« dataSource ã @Autowired 㧠DI ãã¦System.out.println(dataSource.getUrl());
ã§åºåãã¦ã¿ãã¨ã»ããããã¦ããã®ã§ããã - dataSource ã¸ã® application.properties ã®è¨å®ã®åæ 㯠dataSource Bean ã« AOP ã§å¦çã追å ãã¦è¡ããã¦ãããããªæ°ãããã®ã§ãããå ·ä½çãªä»çµã¿ãåãããªãã§ãããããããã®ãã¡èª¿ã¹ã¦ã¿ã¾ãããã
- ã¾ã¨ããã¨ããããã以ä¸ã®ä»çµã¿ã«ãªã£ã¦ããã¨æããã¾ãã
- dataSource Bean çææç¹ã§ã¯ Spring 管çã®ãã©ã³ã¶ã¯ã·ã§ã³ã«åå ãã¦ãã¾ããããã£ã¦ãDoma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã§ dataSource ããã®ã¾ã¾ä½¿ç¨ãã¦ããã©ã³ã¶ã¯ã·ã§ã³ãæå¹ã«ãªãã¾ããã
- çæããã dataSource ã«è¨å®ãåæ ããã¦ããã°
new TransactionAwareDataSourceProxy(...)
ã¯ã¨ã©ã¼ã«ãªãã¾ããã - dataSource Bean ã®çææç¹ã§ã¯ application.properties ã®è¨å®ã¯åæ ããã¦ãã¾ãããããã以å¤ã®å ´æã§ä½¿ç¨ããå ´åã«ã¯è¨å®ãåæ ããã¦ãã¾ãã
- DomaConfig ã¯ã©ã¹ã®ãã£ã¼ã«ã dataSource ã«ã¯ã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ã§ DI ããããã«å¤æ´ããã»ãã¿ã¼å
ã§
new TransactionAwareDataSourceProxy(...)
ãå¼ã³åºã㦠Spring 管çã®ãã©ã³ã¶ã¯ã·ã§ã³ã«åå ãããããã«ãã¦ã¿ã¾ãã
- log4jdbc ã§åºåããã SQL ãè¦ã¦ã¿ãã¨ã以ä¸ã®å¦çãå®è¡ããã¦ãã¾ããã
src/main/java/ksbysample/webapp/email/config ã®ä¸ã® DomaConfig.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
確èªãã¾ããRun View 㧠Ctrl+F5 ãæ¼ã㦠Tomcat ãåèµ·åãã¾ãã
ãã©ã¦ã¶ã§ http://localhost:8080/mailsend ã¸ã¢ã¯ã»ã¹ãã¾ãã以ä¸ã®ç»åã®å¤ãå ¥åå¾ããéä¿¡ããã¿ã³ãã¯ãªãã¯ãã¾ãã
ä»åº¦ã¯ã¨ã©ã¼ã¯çºçãããã¡ã¼ã«ãéä¿¡ããã¾ããã
DB ã«ãä¿åããã¦ãã¾ããã
â»æ§å¥(sex)ã¨é ç®(type)ãä¿åããã¦ããªãã®ã¯ Form ã¯ã©ã¹ã¨ Entity ã¯ã©ã¹ã®åãéãããã§ããä»å㯠Doma 2 ãå©ç¨ã§ããããã«ããããã®èª¿æ»ã§æéãããããããã®ã§ã次åã«è§£æ±ºãã¾ããã¨ã©ã¼çºçæã«ãã¼ã«ããã¯ããããã確èªãã¾ããæåã« email, email_item ãã¼ãã«ãããã¼ã¿ãåé¤ãã¾ãã
Service ã¯ã©ã¹å 㧠RuntimeException ãçºçããããã«ãã¾ããsrc/main/java/ksbysample/webapp/email/web/mailsend ã®ä¸ã® MailsendService.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Ctrl+F9 ãæ¼ã㦠build ãã¾ãã
ãã©ã¦ã¶ã§ http://localhost:8080/mailsend ã¸ã¢ã¯ã»ã¹ãã¾ããå ç¨ã¨åããã¼ã¿ãå ¥åå¾ããéä¿¡ããã¿ã³ãã¯ãªãã¯ãã¾ãã
äºå®éãã¨ã©ã¼ã«ã¯ãªã£ãã®ã§ãããã¡ã¼ã«ãéä¿¡ããã¾ããã§ãããSpring Loaded ã«ãã Tomcat ãåèµ·åãããã¨ãªãåæ ãããã¨æã£ãã®ã§ãããã©ããåæ ããã¦ããªãããã§ãã
Run View 㧠Ctrl+F5 ãæ¼ã㦠Tomcat ãåèµ·åãã¾ãã
å度ãã©ã¦ã¶ã§ http://localhost:8080/mailsend ã¸ã¢ã¯ã»ã¹ãã¾ããå ç¨ã¨åããã¼ã¿ãå ¥åå¾ããéä¿¡ããã¿ã³ãã¯ãªãã¯ãã¾ãã
ä»åº¦ã¯äºå®éãã¨ã©ã¼ã«ãªããã¡ã¼ã«ã¯éä¿¡ããã¾ãããã¾ã DB ã確èªããã¨ãããemail, email_item ã©ã¡ãã®ãã¼ãã«ã«ããã¼ã¿ã¯ä¿åããã¦ãã¾ããã§ããã
ãã°ã«ã rollback ãåºåããã¦ãã¾ããã
src/main/java/ksbysample/webapp/email/web/mailsend ã®ä¸ã® MailsendService.java ã®å®è£ ãå ã«æ»ãã¾ãã
Run View 㧠Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
commitãPushãPull Requestããã¼ã¸
commit ãã¾ããcommit æã« Code Analysis ã®ãã¤ã¢ãã°ã表示ããã¾ãããUnused property ã® Warning ã ããªã®ã§ãCommitããã¿ã³ãã¯ãªãã¯ãã¾ãã
GitHub 㸠Pushã1.0.x-make-mailsendform-saveemail -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-make-mailsendform-saveemail ãã©ã³ããåé¤ãããã¾ãã
Spring Boot 㧠Doma 2 ã使ãããã®ã¡ã¢
- Doma 2 ã使ç¨ããããã®è¨å® ( build æã® SQL ãã¡ã¤ã«ã®åºåå ãã£ã¬ã¯ããªç ) ã build.gradle ã«è¨è¿°ãã¾ãã
- dataSource 㯠spring-boot-starter-data-jpa ã build.gradle ã«è¨è¿°ããå¾ãJava Configuration ã®ã¯ã©ã¹ã« dataSource Bean ãå®è£ ãããã®ã使ç¨ãã¾ãã
- Doma 2 ã® Config ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
ã¯ã©ã¹ãå®è£
ãã¾ããSpring ã® DI ã³ã³ããã«ã·ã³ã°ã«ãã³ã§çæã»ç®¡çãã¦ããããããã¯ã©ã¹ã«ã¯ @Component ã¢ããã¼ã·ã§ã³ãä»å ãã¾ããã¾ã dataSource 㯠dataSource Bean ã¨ãã¦å®è£
ãããã®ãã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ã§ DI ãã¾ããã¤ã³ã¸ã§ã¯ã·ã§ã³æã«ã¯
new TransactionAwareDataSourceProxy(...)
ãå¼ã³åºã㦠dataSource ã Spring 管çã®ãã©ã³ã¶ã¯ã·ã§ã³ã«åå ããã¾ãã - ã³ã¼ãã¸ã§ãã¬ã¼ã¿ Doma-Gen ãå©ç¨ã㦠Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãSQL ãã¡ã¤ã«ãçæãã¾ãã
- Doma-Gen ãå©ç¨ããããã®è¨å® ( ã¯ã©ã¹ã®çæå ã®ããã±ã¼ã¸ãDB ã®è¨å® ) ã build.gradle ã«è¨è¿°ãã¾ãã
- Doma 2 ã apt ã§èªåçæãã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯ã©ã¹ã Spring ã® DI ã³ã³ããã«çæã»ç®¡çãã¦ãããããã«ã@AnnotateWith ã¢ããã¼ã·ã§ã³ã§å®è£ ã¯ã©ã¹ã«ä»å ããã¢ããã¼ã·ã§ã³ãæå®ãã¾ããæåã« @AnnotateWith ã¢ããã¼ã·ã§ã³ãè¨è¿°ããã¢ããã¼ã·ã§ã³ãä½æãã¦ãããå Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ã¯ä½æããã¢ããã¼ã·ã§ã³ãã¯ã©ã¹ã«ä»å ãã¾ãã
- ãã¨ã¯ Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã Service ã¯ã©ã¹çã« @Autowired ã¢ããã¼ã·ã§ã³ã§ DI ãã¦ä½¿ç¨ãã¾ãã
- IntelliJ Doma support plugin 㯠IntelliJ IDEA ã® Community Edition ã§ã¯ä½¿ãã¾ããããã
ãã®ä»ã®ã¡ã¢æ¸ã
- @Value ã¢ããã¼ã·ã§ã³ãä»å ãããã£ã¼ã«ãã¯ãã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã§ã¯ä½¿ç¨ã§ãã¾ããã§ãããã³ã³ã¹ãã©ã¯ã¿ãå®è¡ããã¦ããæç¹ã§ã¢ã¯ã»ã¹ããã¨ã¾ã null ãªãã§ããã
次åã¯ããã
以ä¸ã®ç¹ã調æ»ã»ä¿®æ£ãã¾ãããã¹ããæ¸ãã®ã¯ãã®å¾ã®äºå®ã§ãã
- create table æã®ãã¼ãã«å/ã«ã©ã åã¯è±å¤§æåã§è¨è¿°ããã®ã§ãããDoma-Gen ã§çæããã Entity ã¯ã©ã¹ã®ä¸ã®ãã¼ãã«å/ã«ã©ã åããpgAdmin â ¢ ä¸ã«è¡¨ç¤ºããããã¼ãã«å/ã«ã©ã åã¯è±å°æåã§è¡¨ç¤ºããã¦ãããã¨ã«æ°ã¥ãã¾ãããPostgreSQL ã§ã®ãã¼ãã«å/ã«ã©ã åã®å½åã«ã¼ã«ã確èªãã¦ã¿ã¾ãã
- ç¾å¨ Service ã¯ã©ã¹ã®ã¡ã½ããã« @Transactional ã¢ããã¼ã·ã§ã³ãä»å ãã¦ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãã¦ãã¾ãããAOP ã§ãã©ã³ã¶ã¯ã·ã§ã³ãè¨å®ãããããã«ãã¾ãã
- dataSource 㨠transactionManager ãå©ç¨ãããã ããªãã° spring-boot-starter-data-jpa ã§ã¯ãªã spring-boot-starter-data-jdbc ã§ããããããªæ°ãããã®ã§è©¦ãã¦ã¿ã¾ãã
- æ§å¥(sex)ã¨é ç®(type)ãä¿åãããªãåé¡ã解æ¶ãã¾ãã
- EMAIL_ITEM ãã¼ãã«ã«ãã¼ã¿ãä¿åããå¦çã§æ¯åã¤ã³ã¹ã¿ã³ã¹ãçæããããã«å®è£ ãã¾ããããemailItemId ãã¯ãªã¢ããã°ããã ãã®ãããªæ°ãããã®ã§è©¦ãã¦ã¿ã¾ãã
ã½ã¼ã¹ã³ã¼ã
application.properties
hibernate.dialect = org.hibernate.dialect.PostgreSQL9Dialect doma.dialect = org.seasar.doma.jdbc.dialect.PostgresDialect spring.jpa.hibernate.ddl-auto = none spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.velocity.enabled = false spring.velocity.charset = UTF-8
doma.dialect
ã追å ãã¾ãã
DomaBeanConfig.java
package ksbysample.webapp.email.config; import org.seasar.doma.jdbc.dialect.Dialect; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class DomaBeanConfig { @Value("${doma.dialect}") private String domaDialect; @Bean public Dialect dialect() throws ClassNotFoundException, IllegalAccessException, InstantiationException { return (Dialect)Class.forName(domaDialect).newInstance(); } }
DomaConfig.java
â ãã®ï¼
package ksbysample.webapp.email.config; import org.seasar.doma.jdbc.Config; import org.seasar.doma.jdbc.dialect.Dialect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; @Component public class DomaConfig implements Config { @Autowired private DataSource dataSource; @Autowired private Dialect dialect; @Override public DataSource getDataSource() { return dataSource; } @Override public Dialect getDialect() { return dialect; } }
â ãã®ï¼
package ksbysample.webapp.email.config; import org.seasar.doma.jdbc.Config; import org.seasar.doma.jdbc.dialect.Dialect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import javax.sql.DataSource; @Component public class DomaConfig implements Config { private DataSource dataSource; @Autowired private Dialect dialect; @Autowired public void setDataSource(DataSource dataSource) { this.dataSource = new TransactionAwareDataSourceProxy(dataSource); } @Override public DataSource getDataSource() { return this.dataSource; } @Override public Dialect getDialect() { return this.dialect; } }
private DataSource dataSource;
ã«ä»å ãã¦ãã @Autowired ã¢ããã¼ã·ã§ã³ãåé¤ãã¾ãã- ã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ããããã« @Autowired ã¢ããã¼ã·ã§ã³ãä»å ãã setDataSource ã¡ã½ããã追å ãã¾ããã»ãã¿ã¼å
ã§ãã£ã¼ã«ã dataSource ã«ã»ããããéã«
new TransactionAwareDataSourceProxy(...)
ãå¼ã³åºã㦠dataSource ã Spring 管çã®ãã©ã³ã¶ã¯ã·ã§ã³ã«åå ããã¾ãã
build.gradle
buildscript { repositories { jcenter() // for org.springframework:springloaded maven { url "http://repo.spring.io/repo/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE") classpath("org.springframework:springloaded:1.2.3.RELEASE") } } apply plugin: 'java' apply plugin: 'spring-boot' apply plugin: 'idea' // for Doma 2 // Javaã¯ã©ã¹ã¨SQLãã¡ã¤ã«ã®åºåå ãã£ã¬ã¯ããªãåãã«ãã processResources.destinationDir = compileJava.destinationDir // ã³ã³ãã¤ã«ããåã«SQLãã¡ã¤ã«ãåºåå ãã£ã¬ã¯ããªã«ã³ãã¼ããããã«ä¾åé¢ä¿ãé転ãã compileJava.dependsOn processResources jar { baseName = 'ksbysample-webapp-email' version = '0.0.1-SNAPSHOT' } idea { module { inheritOutputDirs = false outputDir = file("$buildDir/classes/main/") } } configurations { domaGenRuntime } repositories { jcenter() // for org.seasar.doma:doma maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } } dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4-1201-jdbc41" // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ããããã® // Appendix E. Dependency versions ( http://docs.spring.io/spring-boot/docs/current/reference/html/appendix-dependency-versions.html ) åç § compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.springframework.boot:spring-boot-starter-velocity") compile("org.springframework.boot:spring-boot-starter-mail") compile("org.codehaus.janino:janino") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.yaml:snakeyaml") // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã® compile("${jdbcDriver}") compile("org.seasar.doma:doma:2.2.0") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.apache.commons:commons-lang3:3.4") compile("org.projectlombok:lombok:1.16.2") testCompile("org.dbunit:dbunit:2.5.0") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.2.0") domaGenRuntime("${jdbcDriver}") } bootRun { jvmArgs = ['-Dspring.profiles.active=develop'] } test { jvmArgs = ['-Dspring.profiles.active=unittest'] } // for Doma-Gen task gen << { def rootPackageName = 'ksbysample.webapp.email' ant.taskdef(resource: 'domagentask.properties', classpath: configurations.domaGenRuntime.asPath) ant.gen(url: 'jdbc:postgresql://localhost/ksbyemail', user: 'ksbyemail_user', password: 'xxxxxxxx') { entityConfig(packageName: "${rootPackageName}.entity", useListener: false) daoConfig(packageName: "${rootPackageName}.dao") sqlConfig() } }
configurations { ... }
ã追å ãã¾ãã- dependencies ã®ä»¥ä¸ã®ç¹ãå¤æ´ãã¾ãã
def jdbcDriver = "org.postgresql:postgresql:9.4-1201-jdbc41"
ã追å ãã¾ããcompile("org.postgresql:postgresql:9.4-1201-jdbc41")
âcompile("${jdbcDriver}")
ã¸å¤æ´ãã¾ããdomaGenRuntime("org.seasar.doma:doma-gen:2.2.0")
,domaGenRuntime("${jdbcDriver}")
ã追å ãã¾ãã
task gen << { ... }
ã追å ãã¾ãã
EmailDao.java
â ãã®ï¼
@Dao @Component public interface EmailDao {
- ã¯ã©ã¹ã«
@Component
ã¢ããã¼ã·ã§ã³ãä»å ãã¾ãã
â ãã®ï¼
@Dao @ComponentAndAutowiredDomaConfig public interface EmailDao {
@Component
â@ComponentAndAutowiredDomaConfig
ã«å¤æ´ãã¾ãã
EmailItemDao.java
â ãã®ï¼
@Dao @Component public interface EmailItemDao {
- ã¯ã©ã¹ã«
@Component
ã¢ããã¼ã·ã§ã³ãä»å ãã¾ãã
â ãã®ï¼
@Dao @ComponentAndAutowiredDomaConfig public interface EmailItemDao {
@Component
â@ComponentAndAutowiredDomaConfig
ã«å¤æ´ãã¾ãã
MailsendService.java
â ãã®ï¼
package ksbysample.webapp.email.web.mailsend; import ksbysample.webapp.email.dao.EmailDao; import ksbysample.webapp.email.dao.EmailItemDao; import ksbysample.webapp.email.entity.Email; import ksbysample.webapp.email.entity.EmailItem; import ksbysample.webapp.email.service.EmailService; import ksbysample.webapp.email.util.VelocityUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class MailsendService { @Autowired private EmailDao emailDao; @Autowired private EmailItemDao emailItemDao; @Autowired private VelocityUtils velocityUtils; @Autowired private EmailService emailService; @Transactional(rollbackFor = Exception.class) public void saveAndSendEmail(MailsendForm mailsendForm) { // å ¥åããããã¼ã¿ã EMAIL, EMAIL_ITEM ãã¼ãã«ã«ä¿åãã saveEmail(mailsendForm); // ã¡ã¼ã«ãéä¿¡ãã sendEmail(mailsendForm); } public void saveEmail(MailsendForm mailsendForm) { // EMAIL ãã¼ãã«ã«ä¿åãã Email email = new Email(); BeanUtils.copyProperties(mailsendForm, email); emailDao.insert(email); // EMAIL_ITEM ãã¼ãã«ã«ä¿åãã for (String item : mailsendForm.getItem()) { EmailItem emailItem = new EmailItem(); emailItem.setEmailId(email.getEmailId()); emailItem.setItem(Short.parseShort(item)); emailItemDao.insert(emailItem); } } public void sendEmail(MailsendForm mailsendForm) { SimpleMailMessage mailMessage = MAIL001MailBuilder.build() .setForm(mailsendForm) .setVelocityUtils(velocityUtils) .setTemplateLocation("mail/MAIL001/MAIL001-body.vm") .create(); emailService.sendSimpleMail(mailMessage); } }
â ãã®ï¼
@Transactional(rollbackFor = Exception.class) public void saveAndSendEmail(MailsendForm mailsendForm) { // å ¥åããããã¼ã¿ã EMAIL, EMAIL_ITEM ãã¼ãã«ã«ä¿åãã saveEmail(mailsendForm); // ã¡ã¼ã«ãéä¿¡ãã sendEmail(mailsendForm); throw new RuntimeException("ã¡ã¼ã«ã¯éä¿¡ããããDBã«ã¯ä¿åãããªãã¯ã"); }
- saveAndSendEmail ã¡ã½ããã®æå¾ã« RuntimeException ã throw ãã¾ãã
MailsendController.java
package ksbysample.webapp.email.web.mailsend; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/mailsend") public class MailsendController { @Autowired private MailsendService mailsendService; @Autowired private MailsendFormValidator mailsendFormValidator; @InitBinder public void initBinder(WebDataBinder binder) { binder.addValidators(mailsendFormValidator); } @RequestMapping public String index(MailsendForm mailsendForm , Model model) { return "mailsend/mailsend"; } @RequestMapping("/send") public String send(@Validated MailsendForm mailsendForm , BindingResult bindingResult , Model model) { if (bindingResult.hasErrors()) { return "mailsend/mailsend"; } // å ¥åããããã¼ã¿ãDBã«ä¿åããå¾ãã¡ã¼ã«ãéä¿¡ãã mailsendService.saveAndSendEmail(mailsendForm); return "redirect:/mailsend"; } }
- @Autowired ã¢ããã¼ã·ã§ã³ãä»å ã㦠DI ãã¦ãã以ä¸ã®ãã£ã¼ã«ã㯠MailsendService ã¯ã©ã¹ã¸ç§»åãã¾ãã
private VelocityUtils velocityUtils;
private EmailService emailService;
- send ã¡ã½ããå
ã«è¨è¿°ãã¦ããã¡ã¼ã«éä¿¡å¦çã MailsendService ã¯ã©ã¹ã¸ç§»åããsend ã¡ã½ããããã¯
mailsendService.saveAndSendEmail(mailsendForm);
ãå¼ã³åºãããã«å¤æ´ãã¾ãã
ComponentAndAutowiredDomaConfig.java
package ksbysample.webapp.email.annotation.dao; import org.seasar.doma.AnnotateWith; import org.seasar.doma.Annotation; import org.seasar.doma.AnnotationTarget; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @AnnotateWith(annotations = { @Annotation(target = AnnotationTarget.CLASS, type = Component.class), @Annotation(target = AnnotationTarget.CONSTRUCTOR, type = Autowired.class) }) public @interface ComponentAndAutowiredDomaConfig { }
EmailDaoImpl.java
package ksbysample.webapp.email.dao; /** */ @org.springframework.stereotype.Component() @javax.annotation.Generated(value = { "Doma", "2.2.0" }, date = "2015-05-05T04:01:34.940+0900") public class EmailDaoImpl extends org.seasar.doma.internal.jdbc.dao.AbstractDao implements ksbysample.webapp.email.dao.EmailDao { static { org.seasar.doma.internal.Artifact.validateVersion("2.2.0"); } private static final java.lang.reflect.Method __method0 = org.seasar.doma.internal.jdbc.dao.AbstractDao.getDeclaredMethod(ksbysample.webapp.email.dao.EmailDao.class, "selectById", java.lang.Long.class); private static final java.lang.reflect.Method __method1 = org.seasar.doma.internal.jdbc.dao.AbstractDao.getDeclaredMethod(ksbysample.webapp.email.dao.EmailDao.class, "insert", ksbysample.webapp.email.entity.Email.class); private static final java.lang.reflect.Method __method2 = org.seasar.doma.internal.jdbc.dao.AbstractDao.getDeclaredMethod(ksbysample.webapp.email.dao.EmailDao.class, "update", ksbysample.webapp.email.entity.Email.class); private static final java.lang.reflect.Method __method3 = org.seasar.doma.internal.jdbc.dao.AbstractDao.getDeclaredMethod(ksbysample.webapp.email.dao.EmailDao.class, "delete", ksbysample.webapp.email.entity.Email.class); /** * @param config the config */ @org.springframework.beans.factory.annotation.Autowired() public EmailDaoImpl(org.seasar.doma.jdbc.Config config) { super(config); }
- ã¯ã©ã¹ã«
@org.springframework.stereotype.Component()
ããã³ã³ã¹ãã©ã¯ã¿ã«@org.springframework.beans.factory.annotation.Autowired()
ãä»å ããã¦ãããã¨ã確èªã§ãã¾ãã
å±¥æ´
2015/05/05
åççºè¡ã