2024. 6. 17. 21:26ㆍSpring Boot
스프링 부트는 기본적으로 blocking이다
non-blocking을 위해 나온 reactive 프레임워크가 webflux이다
다른 url에 요청을 보낼때는 기존의 RestTemplate 대신 webClient가 쓰이고
DB에 요청을 보낼 때 기존의 JDBC대신 R2DBC가 쓰인다
비교적 새로 나왔기 때문에 JDBC 보다 지원하는 DB가 적다
https://docs.spring.io/spring-data/relational/reference/r2dbc/getting-started.html
Getting Started :: Spring Data Relational
Spring Data R2DBC uses a Dialect to encapsulate behavior that is specific to a database or its driver. Spring Data R2DBC reacts to database specifics by inspecting the ConnectionFactory and selects the appropriate database dialect accordingly. If you use a
docs.spring.io
위 문서에 따르면
H2
MariaDB
Microsoft SQL Server
MySQL
Postgres
Oracle을 지원한다
이번에는 ORACLE을 사용해 구현해보겠다
build.gradle
우선 webflux, r2dbc 그리고 oracle의 필요한 라이브러리를 implement한다
application.properties
그 후 DB주소와 계정 명 password등을 입력해준다
URL은 r2dbc:oracle://localhost:1521/xe와 같이 구성 된다.
연결은 이제 Spring Boot가 알아서 Bean을 생성해줄테니
그후 JDBC처럼 Repository를 설정해준다
package com.example.demo.WebFlux.r2dbc;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface oracle_repository extends R2dbcRepository<oracle_test, Integer> {
}
사용할 테이틀과 매칭할 클래스를 만들어주고
package com.example.demo.WebFlux.r2dbc;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Table("TEST")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class oracle_test {
@Column("DATA") private String data;
@Column("TITLE") private String title;
@Column("ID") @Id private int id;
}
JDBC를 이용할 때처럼 컨트롤러를 만들어주면 된다.
단 return값들을 Flux나 Mono로 바꾸어주어야 한다.
package com.example.demo.WebFlux.r2dbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RequestMapping("/oracle")
@Controller
public class oracle_cointroller {
@Autowired
oracle_repository repo;
@GetMapping("")
public Flux<oracle_test> getAllData() {
return repo.findAll();
}
@GetMapping("/{param}")
public Mono<oracle_test> getData(@RequestParam int param) {
return repo.findById(param);
}
@PostMapping("")
public String insertData(@ModelAttribute oracle_test test) {
repo.save(test);
return "home";
}
@DeleteMapping("/{param}")
public String deleteData(@RequestParam int param) {
repo.deleteById(param);
return "home";
}
@PutMapping("/{param}")
public String updateData(@RequestParam int param, @ModelAttribute oracle_test test) {
test.setId(param);
repo.save(test);
return "home";
}
}
'Spring Boot' 카테고리의 다른 글
| Spring AOP (0) | 2024.07.10 |
|---|---|
| 여러 데이터 베이스 사용 시 Transaction (0) | 2024.07.10 |
| Spring WebFlux (0) | 2024.03.18 |
| Spring WebClient (0) | 2024.03.10 |
| Asynchronized (0) | 2024.03.02 |