Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok'
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.8.RELEASE'
// https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Configuration
@ComponentScan("jp.co.penguin.emperor.databaseaccess")
@PropertySource("classpath:application.yml")
@MapperScan("jp.co.penguin.emperor.databaseaccess.mapper")
@MapperScan("jp.co.penguin.emperor.databaseaccess.mapper") // 使用するMapperインターフェースのパッケージを指定
@EnableTransactionManagement
public class PoolingDatabaseConfig {

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


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

/**
* TransactionManagerのBean定義
* @return TransactionManager
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource(
Expand All @@ -68,6 +83,10 @@ public PlatformTransactionManager transactionManager() {
));
}

/**
* SqlSessionFactoryBeanのBean定義
* @return アプリケーション起動時にSqlSessionFactoryを生成するSqlSessionFactoryBean
*/
@Bean
public SqlSessionFactoryBean sqlSessionFactory() {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
Expand All @@ -82,6 +101,7 @@ public SqlSessionFactoryBean sqlSessionFactory() {
maxWaitMillis
)
);
// MyBatis設定ファイルを指定
sessionFactoryBean.setConfigLocation(
new ClassPathResource("/mybatis-config.xml")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,40 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path="/")
public class MainController {

@Autowired
ArticleMapper mapper;
private ArticleMapper artlcleMapper;

@RequestMapping(path="articles", method = RequestMethod.GET)
public String findAll(@RequestParam(required = false) String keyword) {
public List<Article> findAll(@RequestParam(required = false) String keyword) {
if (keyword == null) {
return mapper.findAll().toString();
return artlcleMapper.findAll();
}
return mapper.findByKeyWord(keyword).toString();
return artlcleMapper.findByKeyWord(keyword);
}

@RequestMapping(path="articles/{id}", method=RequestMethod.GET)
public String findOne(@PathVariable int id) {
return mapper.findOne(id).toString();
public Article findOne(@PathVariable int id) {
return artlcleMapper.findOne(id);
}

@RequestMapping(path="articles/create", method=RequestMethod.POST)
public String insert(@RequestBody Article article) {
return String.valueOf(mapper.insert(article));
public boolean insert(@RequestBody Article article) {
return artlcleMapper.insert(article);
}

@RequestMapping(path="articles/{id}/delete", method=RequestMethod.POST)
public String delete(@PathVariable int id) {
return String.valueOf(mapper.delete(id));
public boolean delete(@PathVariable int id) {
return artlcleMapper.delete(id);
}

@RequestMapping(path="articles/{id}/update", method=RequestMethod.POST)
public String update(@PathVariable int id, @RequestBody Article article) {
return String.valueOf(mapper.update(id, article.getContent(), article.getAuthor()));
@RequestMapping(path="articles/{id}/update", method=RequestMethod.PUT)
public boolean update(@PathVariable int id, @RequestBody Article article) {
return artlcleMapper.update(id, article.getContent(), article.getAuthor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,6 @@

@Data
public class Article {

private String content;

private String author;

public Article() {}

public Article(String content, String author) {
this.content = content;
this.author = author;
}

public void setContent(String content) {
this.content = content;
}

public void setAuthor(String author) {
this.author = author;
}

public String getContent() {
return content;
}

public String getAuthor() {
return author;
}
}
6 changes: 0 additions & 6 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/dev
username: dev
password: secret
#logging:
# level:
# root: DEBUG
Expand Down