티스토리 뷰

0. 정보통신

Mybatis 로 Spring-Boot 와 H2DB 를 연동하기

패스트코드블로그 2020. 5. 24. 21:21

메이븐 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
<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
        </dependency>
        <dependency>
           <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <scope>annotationProcessor</scope>
        </dependency>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web-services</artifactId>
          <scope>compile</scope>
        </dependency>
       <!--  https://mvnrepository.com/artifact/com.h2database/h2 -->
       <dependency>
            <groupId>com.h2database</groupId>
           <artifactId>h2</artifactId>
            <version>1.4.200</version><!--$NO-MVN-MAN-VER$-->
           <scope>test</scope>
       </dependency>
       <!--  https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core -->
       <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.29</version><!--$NO-MVN-MAN-VER$-->
       </dependency>
       <!--  https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
       <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
           <version>2.2.1</version>
       </dependency>
        <dependency>
           <groupId>org.mariadb.jdbc</groupId>
           <artifactId>mariadb-java-client</artifactId>
        </dependency>
    </dependencies>
cs

 

 

 

DatabaseConfig.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.bitcamp.web;
import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
 
/**
* DatabaseConfig
*/
@Configuration
public class DatabaseConfig {
 
 
    @Bean
    public ServletRegistrationBean h2servletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
        registration.addUrlMappings("/console/*");
        return registration;
    }
}
 
cs

 

application.properties

 

1
2
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
cs

 

application.yml

 

1
2
3
4
5
6
7
8
spring:
    datasource:
        url: jdbc:h2:mem:testdb;
        driverClassName: org.h2.Driver
        username: sa
        password:
    jpa:
        database-platform: org.hibernate.dialect.H2Dialect
cs

 

data.sql

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  customer_id VARCHAR(15UNIQUE NOT NULL,
  customer_name VARCHAR(15NOT NULL,
  pwd VARCHAR(50NOT NULL,
  ssn VARCHAR(50UNIQUE NOT NULL,
  phone VARCHAR(50NOT NULL,
  city VARCHAR(110NOT NULL,
  addr VARCHAR(110NOT NULL,
  postalcode VARCHAR(50NOT NULL,
  photo VARCHAR(50DEFAULT 'defaut_photo'
)DEFAULT CHARSET=utf8;
commit;
cs

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함