Skip to content

Commit 557e55f

Browse files
Merge pull request #1 from empenguin1186/feature/refactor
リファクタリング
2 parents 8950732 + 41a9b63 commit 557e55f

File tree

5 files changed

+39
-48
lines changed

5 files changed

+39
-48
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies {
1818
implementation 'org.springframework.boot:spring-boot-starter-web'
1919
runtimeOnly 'org.postgresql:postgresql'
2020
testImplementation 'org.springframework.boot:spring-boot-starter-test'
21-
compileOnly 'org.projectlombok:lombok'
21+
implementation 'org.projectlombok:lombok'
2222
annotationProcessor 'org.projectlombok:lombok'
2323
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.8.RELEASE'
2424
// https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2

src/main/java/jp/co/penguin/emperor/databaseaccess/config/PoolingDatabaseConfig.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@Configuration
1919
@ComponentScan("jp.co.penguin.emperor.databaseaccess")
2020
@PropertySource("classpath:application.yml")
21-
@MapperScan("jp.co.penguin.emperor.databaseaccess.mapper")
21+
@MapperScan("jp.co.penguin.emperor.databaseaccess.mapper") // 使用するMapperインターフェースのパッケージを指定
2222
@EnableTransactionManagement
2323
public class PoolingDatabaseConfig {
2424

@@ -31,8 +31,19 @@ public class PoolingDatabaseConfig {
3131
@Value("${cp.minIdle}") int minIdle;
3232
@Value("${cp.maxWaitMillis}") long maxWaitMillis;
3333

34-
35-
@Bean(destroyMethod = "close")
34+
/**
35+
* データソース設定
36+
* @param driverName アプリケーションで使用するRDBのドライバークラス
37+
* @param url 接続先のデータベースのurl
38+
* @param username データベースで使用するユーザ名
39+
* @param password データベースで使用するパスワード
40+
* @param maxTotal データベース接続しているコネクションの最大値
41+
* @param maxIdle コネクションプールに待機しているコネクションの最大値
42+
* @param minIdle コネクションプールに待機しているコネクションの最小値
43+
* @param maxWaitMillis コネクションが使用され返却されるまでの時間の最大値
44+
* @return 上記設定でカスタマイズされたデータソース
45+
*/
46+
@Bean(destroyMethod = "close") // アプリケーションが終了するタイミングでデータソースが解放されるように設定
3647
public DataSource dataSource(
3748
@Value("${database.driver-class-name}") String driverName,
3849
@Value("${database.url}") String url,
@@ -54,6 +65,10 @@ public DataSource dataSource(
5465
return dataSource;
5566
}
5667

68+
/**
69+
* TransactionManagerのBean定義
70+
* @return TransactionManager
71+
*/
5772
@Bean
5873
public PlatformTransactionManager transactionManager() {
5974
return new DataSourceTransactionManager(dataSource(
@@ -68,6 +83,10 @@ public PlatformTransactionManager transactionManager() {
6883
));
6984
}
7085

86+
/**
87+
* SqlSessionFactoryBeanのBean定義
88+
* @return アプリケーション起動時にSqlSessionFactoryを生成するSqlSessionFactoryBean
89+
*/
7190
@Bean
7291
public SqlSessionFactoryBean sqlSessionFactory() {
7392
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
@@ -82,6 +101,7 @@ public SqlSessionFactoryBean sqlSessionFactory() {
82101
maxWaitMillis
83102
)
84103
);
104+
// MyBatis設定ファイルを指定
85105
sessionFactoryBean.setConfigLocation(
86106
new ClassPathResource("/mybatis-config.xml")
87107
);

src/main/java/jp/co/penguin/emperor/databaseaccess/controller/MainController.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,40 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.web.bind.annotation.*;
77

8+
import java.util.List;
9+
810
@RestController
911
@RequestMapping(path="/")
1012
public class MainController {
1113

1214
@Autowired
13-
ArticleMapper mapper;
15+
private ArticleMapper artlcleMapper;
1416

1517
@RequestMapping(path="articles", method = RequestMethod.GET)
16-
public String findAll(@RequestParam(required = false) String keyword) {
18+
public List<Article> findAll(@RequestParam(required = false) String keyword) {
1719
if (keyword == null) {
18-
return mapper.findAll().toString();
20+
return artlcleMapper.findAll();
1921
}
20-
return mapper.findByKeyWord(keyword).toString();
22+
return artlcleMapper.findByKeyWord(keyword);
2123
}
2224

2325
@RequestMapping(path="articles/{id}", method=RequestMethod.GET)
24-
public String findOne(@PathVariable int id) {
25-
return mapper.findOne(id).toString();
26+
public Article findOne(@PathVariable int id) {
27+
return artlcleMapper.findOne(id);
2628
}
2729

2830
@RequestMapping(path="articles/create", method=RequestMethod.POST)
29-
public String insert(@RequestBody Article article) {
30-
return String.valueOf(mapper.insert(article));
31+
public boolean insert(@RequestBody Article article) {
32+
return artlcleMapper.insert(article);
3133
}
3234

3335
@RequestMapping(path="articles/{id}/delete", method=RequestMethod.POST)
34-
public String delete(@PathVariable int id) {
35-
return String.valueOf(mapper.delete(id));
36+
public boolean delete(@PathVariable int id) {
37+
return artlcleMapper.delete(id);
3638
}
3739

38-
@RequestMapping(path="articles/{id}/update", method=RequestMethod.POST)
39-
public String update(@PathVariable int id, @RequestBody Article article) {
40-
return String.valueOf(mapper.update(id, article.getContent(), article.getAuthor()));
40+
@RequestMapping(path="articles/{id}/update", method=RequestMethod.PUT)
41+
public boolean update(@PathVariable int id, @RequestBody Article article) {
42+
return artlcleMapper.update(id, article.getContent(), article.getAuthor());
4143
}
4244
}

src/main/java/jp/co/penguin/emperor/databaseaccess/entity/Article.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,6 @@
44

55
@Data
66
public class Article {
7-
87
private String content;
9-
108
private String author;
11-
12-
public Article() {}
13-
14-
public Article(String content, String author) {
15-
this.content = content;
16-
this.author = author;
17-
}
18-
19-
public void setContent(String content) {
20-
this.content = content;
21-
}
22-
23-
public void setAuthor(String author) {
24-
this.author = author;
25-
}
26-
27-
public String getContent() {
28-
return content;
29-
}
30-
31-
public String getAuthor() {
32-
return author;
33-
}
349
}

src/main/resources/application.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
spring:
2-
datasource:
3-
driver-class-name: org.postgresql.Driver
4-
url: jdbc:postgresql://localhost:5432/dev
5-
username: dev
6-
password: secret
71
#logging:
82
# level:
93
# root: DEBUG

0 commit comments

Comments
 (0)