[컴] spring R2DBC에서 mariadb 연결

springboot r2dbc 연결 / db 연결 / database 연결 / 디비연결 / helloworld / springboot helloworld / spring hello world / spring helloworld

spring R2DBC에서 mariadb 연결

절차

  1. Spring Initializr에서 dependency 넣고 project 생성
    • Spring Data R2DBC SQL
    • MariaDB JDBC and R2DBC driver.
    • Spring Reactive Web Web
    • Lombok
  2. unzip
  3. /src/main/resources/application.properties 설정
    • spring.r2dbc.url
    • spring.r2dbc.username
    • spring.r2dbc.password
  4. gradlew
  5. JAVA_HOME 변수 확인
  6. gradlew bootRun

Spring Initializr

Spring Initializr로 가서 springboot project를 만들자

db configuration

  1. /src/main/resources/application.properties 에 설정

    spring.r2dbc.url=r2dbc:mariadb://127.0.0.1:3306/auth_server
    spring.r2dbc.username=myuser
    spring.r2dbc.password=mypass
  2. java file 설정

    아래는 ref. 5에서 가져온 code이다.

    @Configuration
    public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
    
        @Override
        @Bean
        public ConnectionFactory connectionFactory() {
    
            return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
                    .host("localhost")
                    .port(5432)
                    .username("username")
                    .password("password")
                    .database("mydb")
                    .build());
        }
    
    }

실행 결과

오전 10:46:25: Executing 'bootRun'...

Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:63799', transport: 'socket'
Gradle Daemon started in 890 ms
> Task :compileJava UP-TO-DATE
> Task :processResources
> Task :classes
Disconnected from the target VM, address: '127.0.0.1:63799', transport: 'socket'
> Task :bootRunMainClassName
Connected to the target VM, address: 'localhost:63866', transport: 'socket'

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.7.14)

2023-08-23 10:46:31.110  INFO 28476 --- [           main] com.mycom.test1.Test1Application         : Starting Test1Application using Java 17.0.6 on DESKTOP-V8P2I3H with PID 28476 (D:\a\prog\test02-from-springinitializer\test1\build\classes\java\main started by namh in D:\a\prog\test02-from-springinitializer\test1)
2023-08-23 10:46:31.112  INFO 28476 --- [           main] com.mycom.test1.Test1Application         : No active profile set, falling back to 1 default profile: "default"
2023-08-23 10:46:31.367  INFO 28476 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2023-08-23 10:46:31.372  INFO 28476 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 2 ms. Found 0 R2DBC repository interfaces.
2023-08-23 10:46:32.138  INFO 28476 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2023-08-23 10:46:32.143  INFO 28476 --- [           main] com.mycom.test1.Test1Application         : Started Test1Application in 1.247 seconds (JVM running for 1.461)

gradlew bootRun 시점에 VM Option 사용방법

gradle.build 에 다음 내용을 추가하면 gradlew bootRun 을 실행할 때 parameter 로 넘겨진 값들이 적용된다.

bootRun {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}

Reference

  1. Unblock Your Applications with R2DBC, Spring Data and MariaDB | MariaDB
  2. Native R2DBC Code Example — MariaDB Documentation
  3. Getting Started | Accessing data with R2DBC
  4. Spring Boot R2DBC + MySQL example - BezKoder
  5. java - How to set up database connection in r2dbc? - Stack Overflow
  6. 쿠...sal: [컴] Spring Boot 간략 정리

댓글 없음:

댓글 쓰기