[PF/PlayAsset] WebFlux R2dbc MySQL SELECT 구현 및 샘플코드

    728x90
    반응형

    2023.03.06

    개발환경구성

    - FE: React-Native

    - BE: WebFlux

    - DB: MySQL, Redis

    - IDE: Android Studio, SpringBoot(2.7.3)

    - JDK: 11

    - SERVER: AWS

     

     

    Entity

    package com.assetinfo.playasset.onl.user.entity;
    
    import java.sql.Date;
    import org.springframework.format.annotation.DateTimeFormat;
    import lombok.Data;
    
    @Data
    public class UserEntity {
        private String userId;
        private String userPassword;
        private String userName;
        private String userCp;
        private String userAuth;
        private String regId;
    
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date regDtm;
        private String udtId;
    
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date udtDtm;
    }

    1) SpringMVC의 Model과 유사한 구조

    2) SELECT를 제외한 나머지 DML(INSERT, UPDATE, DELETE)는 @Table 어노테이션이 필요

     

     

    Handler 구현 및 샘플코드

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Component;
    import org.springframework.web.reactive.function.BodyInserters;
    import org.springframework.web.reactive.function.client.WebClient;
    import org.springframework.web.reactive.function.server.ServerRequest;
    import org.springframework.web.reactive.function.server.ServerResponse;
    
    import com.assetinfo.playasset.onl.user.entity.UserEntity;
    import com.assetinfo.playasset.onl.user.svc.UserSvc;
    
    import lombok.NonNull;
    import reactor.core.publisher.Mono;
    
    /**
     * @Desc
     * Router에 정의된 URL과 매핑된 메소드명을 Handler 메소드로 작성
     */
    @Component
    public class Handler {
    
        private final UserSvc userSvc;
    
        @Autowired
        public Handler(@NonNull UserSvc userSvc) {
            this.userSvc = userSvc;
        }
    
        /**
         * @Desc    단건조회
         * @param   request
         * @return  
         */
        public Mono<ServerResponse> findOne(ServerRequest request) {
            return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
                    .body(BodyInserters.fromProducer(userSvc.findOne(), UserEntity.class));
        }

    1) SpringMVC의 Controller와 유사한 구조

    2) Handler 메소드 구현 방법 참고(WebFlux Functional Endpoints (Router-Handler) 구성 및 샘플코드)

     

     

    Service 구현 및 샘플코드

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.assetinfo.playasset.onl.user.dao.UserDao;
    import com.assetinfo.playasset.onl.user.entity.UserEntity;
    
    import reactor.core.publisher.Mono;
    
    @Service
    public class UserSvc {
        
        @Autowired
        private UserDao userDao;
    
        public Mono<UserEntity> findOne() {
            return userDao.findOne();
        }
    }

    1) SpringMVC의 Service와 유사한 구조

     

     

    Dao 구현 및 샘플코드(조건절 없음)

    import org.springframework.data.r2dbc.repository.Query;
    import org.springframework.data.repository.reactive.ReactiveCrudRepository;
    import org.springframework.stereotype.Repository;
    
    import com.assetinfo.playasset.onl.user.entity.UserEntity;
    
    import reactor.core.publisher.Mono;
    
    @Repository
    public interface UserDao extends ReactiveCrudRepository<UserEntity, String> {
    
        @Query("SELECT * FROM TM_USER")
        Mono<UserEntity> findOne();
    }

    1) SpringMVC의 @Repository(또는 @Mapper)와 MyBatis를 합친 구조

    2) ReactiveCrudRepository<객체, Key>를 상속 (객체와 Key는 개발자가 직접 작성해야하는 부분)

    3) @Query 어노테이션에 실행할  쿼리를 작성

    4) 리턴타입은 반드시 Mono 또는 Flux로 작성

     

     

    Dao 구현 및 샘플코드(조건절 있음)

    import org.springframework.data.r2dbc.repository.Query;
    import org.springframework.data.repository.reactive.ReactiveCrudRepository;
    import org.springframework.stereotype.Repository;
    
    import com.assetinfo.playasset.onl.user.entity.UserEntity;
    
    import reactor.core.publisher.Mono;
    
    @Repository
    public interface UserDao extends ReactiveCrudRepository<UserEntity, String> {
    
        @Query("SELECT * FROM TM_USER WHERE USER_NAME LIKE CONCAT('%', :userName, '%')")
        Mono<UserEntity> findOne(String userName);
    }

    1) 조건절이 있는 쿼리를 작성

    2) MyBatis의 #{PreparedStatement 파라미터} 형태와 같이 ':'를 붙여 사용한다 (:userName)

    728x90
    반응형

    댓글