SpringBootã¨Domaã®é£æºãGitHubã«ãããµã³ãã«ãåèã«ä½ã£ã¦ã¿ã¾ããã
ãã«ãã¯Gradleã使ã£ã¦ãã¾ãã
ãã£ã¬ã¯ããªæ§æ
SpringBootæ¨æºã®æ§æã使ç¨ãã¾ãã
ä½æã»ç·¨éãããã¡ã¤ã«
build.gradle
DBã©ã¤ãã©ãªã®ä¾åé¢ä¿ã追å ãã¾ãã
JDBCãã©ã¤ãã¼ã¯ãã¼ã«ã«ã¾ãã¯Mavenãªãã¸ããªããåå¾ãããã«ãå¾ã®jarãã¡ã¤ã«ã«å«ããããã«ãã¾ãã
dependencies { // SpringJDBC compile("org.springframework.boot:spring-boot-starter-jdbc") // Doma1.x(Java1.7ã¾ã§) compile("org.seasar.doma:doma:1.+") // ãã¼ã«ã«ã®OracleDriver compile files("C:/app/lib/jdbc/ojdbc7.jar") }
application.properties
SpringBootã®ã¢ããªã±ã¼ã·ã§ã³è¨å®ãã¡ã¤ã«ã«ãã¼ã¿ãã¼ã¹ã®è¨å®æ å ±ãè¨è¿°ãã¾ãã
# Oracleã®å ´å spring.datasource.url=jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:sid spring.datasource.username=username spring.datasource.password=password spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
ConfigAutowireable.java
Daoã¯ã©ã¹ãSpringã®DIã³ã³ããã§ç®¡çããããã®ã¢ããã¼ã·ã§ã³ãä½æãã¾ãã
package demo; 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 ConfigAutowireable { }
DataSourceConfiguration.java
Springã¨Domaãé£æºããããã®ãã¼ã¿ãã¼ã¹è¨å®ã¯ã©ã¹ãä½æãã¾ãã
package demo; import javax.annotation.Resource; import javax.sql.DataSource; import org.seasar.doma.jdbc.Config; import org.seasar.doma.jdbc.DomaAbstractConfig; import org.seasar.doma.jdbc.NoCacheSqlFileRepository; import org.seasar.doma.jdbc.SqlFileRepository; import org.seasar.doma.jdbc.dialect.Dialect; import org.seasar.doma.jdbc.dialect.OracleDialect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement public class DataSourceConfiguration { @Resource private Environment env; @Bean public DataSource dataSource() { // application.propertiesã«ããDBè¨å®ãã¼ããå¤ãåå¾ãè¨å®ãã¾ãã DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("spring.datasource.driverClassName")); dataSource.setUrl(env.getRequiredProperty("spring.datasource.url")); dataSource.setUsername(env.getRequiredProperty("spring.datasource.username")); dataSource.setPassword(env.getRequiredProperty("spring.datasource.password")); // TransactionAwareDataSourceProxyã§ã©ããã³ã°ããªãã¨Domaå´ã§ã³ãã¯ã·ã§ã³ããããããªã return new TransactionAwareDataSourceProxy(dataSource); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } @Bean public Dialect dialect() { // DBã®æ¹è¨ã¯ããã§è¨å®ãã¾ãã return new OracleDialect(); } @Bean public SqlFileRepository sqlFileRepository() { return new NoCacheSqlFileRepository(); } @Bean public Config config() { // Domaã®è¨å®ãè¡ãã¾ããããã©ã«ãã¯DomaAbstractConfigã使ç¨ãã¾ãã return new DomaAbstractConfig() { @Override public Dialect getDialect() { return dialect(); } @Override public DataSource getDataSource() { return dataSource(); } @Override public SqlFileRepository getSqlFileRepository() { return sqlFileRepository(); } }; } }
# 2015/01/23追è¨
SpringBoot1.2以éã ã¨ããããã£ãã¡ã¤ã«ããã®å¤åå¾ã«Springã§ä½¿ç¨ããã¦ãããDataSourcePropertiesãã使ç¨ã§ãã¾ãã
@Autowired private DataSourceProperties properties; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(properties.getDriverClassName()); dataSource.setUrl(properties.getUrl()); dataSource.setUsername(properties.getUsername()); dataSource.setPassword(properties.getPassword()); return new TransactionAwareDataSourceProxy(dataSource); }
TestDao.java
å ã»ã©ä½æããConfigAutowireableã¢ããã¼ã·ã§ã³ãDaoã«è¨å®ãã¾ãã
package demo.dao; import org.seasar.doma.Dao; import demo.ConfigAutowireable; @Dao @ConfigAutowireable public interface TestDao { }
ãã©ã³ã¶ã¯ã·ã§ã³ã®è¨å®
ãã©ã³ã¶ã¯ã·ã§ã³ãæå¹ã«ããã«ã¯ããã©ã³ã¶ã¯ã·ã§ã³ã使ç¨ãããã¯ã©ã¹ã¾ãã¯ã¡ã½ããã«@Transactional
ãå®ç¾©ãã¾ãã
ã¯ã©ã¹ã§å®ç¾©ããã¨ã¯ã©ã¹å ã®å ¨ã¡ã½ããã対象ãã¡ã½ããã«å®ç¾©ããã¨ãã®ã¡ã½ããã®ã¿å¯¾è±¡ã¨ãªãã¾ãã
@Transactional @Controller public class IndexController { @Transactional @RequestMapping("/") public String index() { return "index.html"; } }