é£è¼è¨äºãSpringBoot ã¢ããªéçºããèªã¿é²ããã¨ããµã³ãã«ã® Webã¢ããªãå¶ä½ãããã¨ãã§ãã¾ãã
ä»åã¯ããµã³ãã«ã¢ããªã®ããã¸ã§ã¯ãã¨SQLã使ãã¦ããã¾ãã
é£è¼è¨äº
SpringBoot ã¢ããªéçºã®é£è¼è¨äºã¯ä»¥ä¸ã®éãã§ãã
- æ¦è¦ã»ä½¿ç¨ãããã¯ã
- ããã¸ã§ã¯ã使ã»SQL使ï¼ä»åã®è¨äºï¼
- ã¢ãã«ã»ãªãã¸ããªã®ä½æ
- ã³ã³ããã¼ã©ã¼ã»ã¡ã¤ã³ã®ä½æ
- HTMLã»JavaScriptã®ä½æ
- CSSã»ç»åã®ä½æ
- ãã¹ãã»åä½ç¢ºèª
ç®æ¬¡
- ãã£ã¬ã¯ããªæ§æ
- ãã«ããã¡ã¤ã«ã®ä½æ
- è¨å®ãã¡ã¤ã«ã®ä½æ
- SQLãã¡ã¤ã«ã®ä½æ
1. ãã£ã¬ã¯ããªæ§æ
äºåã«ä»¥ä¸ã®ãã£ã¬ã¯ããªæ§æã使ãã¦ããã¾ãã
spring-boot-tweet
ââsrc
ââmain
â ââjava
â â ââsample
â â ââcontroller
â â ââmodel
â â âârepository
â ââresources
â ââdb
â â ââmigration
â ââstatic
â ââcss
â ââimg
â ââjs
ââtest
ââjava
ââit
ââsample
ââcontroller
ããã¸ã§ã¯ãã®ã«ã¼ããã£ã¬ã¯ããªã¯ãspring-boot-tweetãã§ããmainã«ã¯ãããã¯ãã³ã¼ããtestã«ã¯ãã¹ãã³ã¼ããæ ¼ç´ããã¾ãã
2. ãã«ããã¡ã¤ã«ã®ä½æ
ã«ã¼ããã£ã¬ã¯ããªã®ä¸ã«ã以ä¸ã®ãã«ããã¡ã¤ã«ã使ãã¾ãã
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>sample</groupId>
<artifactId>spring-boot-tweet</artifactId>
<version>1.0.0</version>
<description>Spring Boot Sample Web App.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>7.2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. è¨å®ãã¡ã¤ã«ã®ä½æ
Spring Boot ã®è¨å®ãã¡ã¤ã«ã使ãã¾ãã
src/main/resources/application.properties
spring.application.name=tweet spring.datasource.url=jdbc:postgresql://localhost:5432/tweet spring.datasource.username=user spring.datasource.password=pass spring.jackson.date-format=yyyy/MM/dd HH:mm:ss spring.jackson.time-zone=Asia/Tokyo
PostgreSQL ã® tweet ã¹ãã¼ãï¼ãã¼ã¿ãã¼ã¹ï¼ã«æ¥ç¶ããããã«ãã¦ãã¾ãã
4. SQLãã¡ã¤ã«ã®ä½æ
Flywayãå®è¡ããSQLãã¡ã¤ã«ã使ãã¾ãã
src/main/resources/db/migration/V1__Create.sql
CREATE TABLE tweet ( id bigserial PRIMARY KEY, txt varchar(100) NOT NULL, version bigint NOT NULL, update_time timestamp NOT NULL DEFAULT current_timestamp, create_time timestamp NOT NULL DEFAULT current_timestamp );
SQLãå®è¡ãããã¨ãã¤ã¶ãããæ ¼ç´ãããã¼ãã«ã使ããã¾ããã¢ããªã®èµ·åæã«ãDBãã¤ã°ã¬ã¼ã·ã§ã³ã¨ãã¦1åã ãå®è¡ããã¾ãã
ãã¡ã¤ã«åã¯ãFlywayã®å½åè¦ç´ã«å¾ã£ã¦ãã¾ãã
GitHubãªãã¸ããª
ã¢ããªã®ã³ã¼ãã¯ã以ä¸ã®ãªãã¸ããªã«ãããã¾ãã