반응형

[ Spring Boot 실습 #7 ] H2 Database 설정 - h2 console 접근

 

• 개발환경

MacOS M1 - macOS Monterey 12.0.1

 

JDK 1.8

Spring Boot 2.6.1

Spring Data JPA (spring-boot-starter-data-jpa-2.6.1 : spring-data-jpa-2.6.0)

h2database (h2-1.4.200)

IntelliJ IDEA 2021.2 (Community Edition)

 

 

[ 구조 ]

 

 

[ 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
<?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>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.home.test.h2console.starter</groupId>
    <artifactId>h2-console-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>h2-console-starter</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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>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>
 

 

 

[ application.properties ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
###### Server Properties  ###########################################
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:./h2-console-starter/target/h2db/db/h2console;DB_CLOSE_DELAY=-1
spring.datasource.username=tester
spring.datasource.password=
 
###### JPA Hibernate Properties  ###########################################
spring.jpa.hibernate.ddl-auto=update
 

 

 

[ H2ConsoleStarterApplication.java ]

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

 

 

[ Post.java ]

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package io.home.test.h2console.starter.app.post.domain;
 
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
 
import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
 
@Entity
@Table(name = "tb_post")
public class Post implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    @Id
    @GeneratedValue
    @Column(name = "id", updatable = true, nullable = false)
    private long id;
 
    @Column(name = "record_state", columnDefinition = "int default 0")
    private RecordState recordState = RecordState.ACTIVE;
 
    @CreatedBy
    @Column(name = "created_by"length = 100, updatable = false)
    private String createdBy;
 
    @CreatedDate
    @Column(name = "created_date", updatable = false)
    private ZonedDateTime createdDate = ZonedDateTime.now();
 
    @LastModifiedBy
    @Column(name = "last_modified_by"length = 100)
    private String lastModifiedBy;
 
    @LastModifiedDate
    @Column(name = "last_modified_date")
    private ZonedDateTime lastModifiedDate = ZonedDateTime.now();
 
    @Column(name = "title"length = 255)
    private String title;
 
    @Lob
    @Column(name = "content")
    private String content;
 
    private long viewCount = 0;
 
    // getter, setter
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public RecordState getRecordState() {
        return recordState;
    }
    public void setRecordState(RecordState recordState) {
        this.recordState = recordState;
    }
    public String getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    public ZonedDateTime getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(ZonedDateTime createdDate) {
        this.createdDate = createdDate;
    }
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }
    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }
    public ZonedDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }
    public void setLastModifiedDate(ZonedDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
    public void increaseViewCount() {
        viewCount++;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public long getViewCount() {
        return viewCount;
    }
    public void setViewCount(long viewCount) {
        this.viewCount = viewCount;
    }
    //
}
 

 

 

[ RecordState.java ]

1
2
3
4
5
6
package io.home.test.h2console.starter.app.post.domain;
 
public enum RecordState {
    ACTIVE, UPDATING, DELETED;
}
 

 

 

[ 실행 ]

 

 

[ h2 - console 접근]

localhost:8081/h2-console

 

 

[ h2 - console 접속 ]

 

반응형