[Spring Boot2.4.2] postgresのCRUDの前準備

Spring Bootのアーキテクチャ

CRUDの処理は、Mapper(Repository)クラスで行なっている。
Spring Initializrで雛形をgenerateする。
DependenciesにValidation, Spring Web, MyBatis, PostgreSQL Driveを追加する。
mybatisとは?
-> カスタムSQL、ストアドプロシージャ、高度なマッピング処理に対応

src/main/resources/application.yml

# Web
server:
  port: 8081
  servlet:
    context-path: /api

postgres/initdb/01_DDL_CREATE_TABLE.sql

CREATE TABLE customer (
	id VARCHAR(10) PRIMARY KEY,
	username VARCHAR(50) NOT NULL,
	email VARCHAR(50) NOT NULL,
	phone_number VARCHAR(11) NOT NULL,
	post_code VARCHAR(7) NOT NULL
);

postgres/initdb/02_DML_INSERT_INIT_DATA.sql

INSERT INTO customer VALUES ('001', 'user001', 'test.user.001@example.com', '12345678901', '1234567');
INSERT INTO customer VALUES ('002', 'user002', 'test.user.002@example.com', '23456789012', '2345671');
INSERT INTO customer VALUES ('003', 'user003', 'test.user.003@example.com', '34567890123', '3456712');
INSERT INTO customer VALUES ('004', 'user004', 'test.user.004@example.com', '45678901234', '4567123');
INSERT INTO customer VALUES ('005', 'user005', 'test.user.005@example.com', '56789012345', '5671234');
INSERT INTO customer VALUES ('006', 'user006', 'test.user.006@example.com', '67890123456', '6712345');
INSERT INTO customer VALUES ('007', 'user007', 'test.user.007@example.com', '78901234567', '7123456');
INSERT INTO customer VALUES ('008', 'user008', 'test.user.008@example.com', '89012345678', '1234567');
INSERT INTO customer VALUES ('009', 'user009', 'test.user.009@example.com', '90123456789', '2345671');
INSERT INTO customer VALUES ('010', 'user010', 'test.user.010@example.com', '01234567890', '3456712');

test=> select * from customer;
id | username | email | phone_number | post_code
—–+———-+—————————+————–+———–
001 | user001 | test.user.001@example.com | 12345678901 | 1234567
002 | user002 | test.user.002@example.com | 23456789012 | 2345671
003 | user003 | test.user.003@example.com | 34567890123 | 3456712
004 | user004 | test.user.004@example.com | 45678901234 | 4567123
005 | user005 | test.user.005@example.com | 56789012345 | 5671234
006 | user006 | test.user.006@example.com | 67890123456 | 6712345
007 | user007 | test.user.007@example.com | 78901234567 | 7123456
008 | user008 | test.user.008@example.com | 89012345678 | 1234567
009 | user009 | test.user.009@example.com | 90123456789 | 2345671
010 | user010 | test.user.010@example.com | 01234567890 | 3456712

application.yml
-> camel caseとsnake caseの命名の差分をMyBatis側で吸収し、適切にテーブル・カラム名とクラス・フィールド名をマッピングする。

# Datasource
spring:
  datasource:
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/test
    username: root
    password:
    
# MyBatis
mybatis:
  configuration:
    map-underscore-to-camel-case: true

com.example.demo.controllerにCustomerController.javaを作る
CustomerController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
	
	@GetMapping("/hello")
	public String hello() {
		return "Hello World.";
	}
}


ここまでは基礎

build.gradleに依存性を追加

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
	runtimeOnly 'org.postgresql:postgresql'
	testImplementation 'org.dbunit:dbunit:2.5.3'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}