반응형

Spring Boot 초기 데이터 생성 - CommandLineRunner 활용 (1)

sql문을 활용하지 않고, JPA Repository를 활용하여 json 파일을 읽어서 초기 데이터 생성

 

  • 개발환경
  • 초기 데이터 생성 과정
  • 초기 데이터 생성 테스트용 기본 파일 생성 (pom.xml, TestInitializerApplication.java, application.properties)
  • Spring Boot 실행 설정
  • Spring Boot 실행 여부 확인

 

<< 개발환경  >>

Java OpenJDK 1.8.0_292
Spring Spring-Boot 2.4.5
IDE IntelliJ

 

<< 초기 데이터 생성 >>

프로그램 실행 --> json파일 읽기 --> entity/model List --> h2/mysql DB에 데이터 적재

  • Sample: Long, String, Integer, boolean
  • DB: h2, mysql
  • Data Type: json

 

<< 프로젝트 디렉토리 구조 >>

 

<< 기본 파일 설정: pom.xml; 실행파일(TestInitializerApplication.java); 설정파일(application.properties) >>

 

[ pom.xml ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?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 http://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>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.home.test</groupId>
    <artifactId>test-initializer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <description>Spring Boot project for data initialization test</description>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!-- <scope>provided</scope> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

[ TestInitializerApplication.java ] 실행

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.home.test;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class TestInitializerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(TestInitializerApplication.class, args);
    }
}
 

 

[ application.properties ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
###### Server Properties  ###########################################
#server.port=9091
server.port=8081
 
 
###### Database Properties  ###########################################
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
 
spring.datasource.platform=h2
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:h2:file:./target/h2db/db/test-initializer;DB_CLOSE_DELAY=-1
spring.datasource.username=test
spring.datasource.password=
 

 

 

<< Spring Boot 실행 설정  >>

Run > Edit Configurations... > Add New Configuration: Maven > Run/Debug Configurations > Parameters > Command line: spring-boot:run > Apply > OK

 

Run > Edit Configurations...

Add New Configuration: Maven

Run/Debug Configurations > Parameters > Command line: spring-boot:run > Apply

 

 

<< Spring-Boot 실행 여부 확인 >>

maven clean > install > spring-boot:run

 

maven clean > install 

spring-boot:run

 

반응형