본문 바로가기

토이프로젝트

프로젝트 생성

우선 전체를 설계하고 시작하지 않고 대략적인 아웃라인을 통해 기본적인 기능을 구현하고 이후 상세화를 통해 develop을 하기로 한다.

 

gradle기반의 멀티모듈을 통해 이후 필요한 공통 모듈등을 도출할 계획이고, 우선 아래와 같이 3개의 서비스를 구성하여 기본적인 상품조회, 주문, 주문내역 조회 등을 구현한다.

 

toy-project-1

├ order-svc

│    com.rara.toy1

     ├ order

       OrderSvcApplication.java       

    build.gradle

├ product-svc

    com.rara.toy1

     ├ product

       ProductSvcApplication.java       

    build.gradle

├ user-svc

    com.rara.toy1

      ├ user

       UserSvcApplication.java       

   build.gradle

├ build.gradle

└ settings.gradle

 

그룹 프로젝트의 build.gradle 설정은 다음과 같다.

buildscript {
    ext {
        springBootVersion = '2.4.4'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE"
    }
}

allprojects {
    group = 'com.rara.toy1'
    version = '0.0.1-SNAPSHOT'
}


subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    sourceCompatibility = '1.8'

    repositories {
        mavenCentral()
    }

    dependencies {
    	compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }	
}

springboot 버전은 최신 버전을 사용한다.

spring-web 대신에 spring-webflux를 사용해볼 계획이지만 R2DBC 등을 추가 고려해야하기 때문에 이는 다른 branch에서 추가 검증을 진행 한 후 이후 적용하기로 한다. (webclient는 사용할 예정)

 

확인해본 결과 spring-web와 spring-webflux는 동시 적용이 가능하고, 동시 적용했을 경우 tomcat 기반의 spring mvc가 적용된다. (webclient 사용 가능)

rara-project.tistory.com/8

 

Spring Web MVC와 Spring WebFlux 같이 사용하기

Spring Boot는 웹 어플리케이션 개발을 위해 embedded Tomcat, Jetty, Undertow, or Netty를 지원하며 이를 이용하여 빠르게 관련 모듈을 구성하기 위해 spring-boot-starter-web와 spring-boot-starter-webflux를..

rara-project.tistory.com

때문에 우선 spring-data-jpa를 사용하고 각 서비스는 서비스에 필요한 DB Driver를 사용하도록 한다.

 

각 서비스의 주요한 Entity객체를 다음과 같이 생성한다.

User Service

@Entity
@Table(name = "TB_USER")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String emailId;

    private String userNm;

    private String password;
    
    private BigDecimal credit;
    
    @ElementCollection
    @CollectionTable(name = "TB_USER_ROLES", joinColumns = @JoinColumn(name = "USER_ID"))
    private List<String> roles = new ArrayList<>();

    @Builder
    private User(Long id, String emailId, String userNm, String password, BigDecimal credit, List<String> roles) {
		this.id = id;
		this.emailId = emailId;
		this.userNm = userNm;
		this.password = password;
		this.credit = credit;
		this.roles = roles;
	}
    
}

해당 계정으로 로그인이 가능한 entity를 설계하였고, credit이란 칼럼을 통해 추후 주문건에 대한 credit 차감을 진행할 예정이다.

Order Service

@Entity
@Table(name = "TB_ORDER")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Order extends BaseEntity {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY) // 데이터 베이스에 위임
	private Long id;
	
	private LocalDateTime orderDate;
	
	private Long customerId;
	
	private BigDecimal totalPrice;
	
	private String orderStatusCode;

	@Builder
	private Order(Long id, LocalDateTime orderDate, Long customerId, BigDecimal totalPrice,
			String orderStatusCode) {
		this.id = id;
		this.orderDate = orderDate;
		this.customerId = customerId;
		this.totalPrice = totalPrice;
		this.orderStatusCode = orderStatusCode;
	}
	
}

@Entity
@Table(name = "TB_ORDER_DETAIL")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class OrderDetail extends BaseEntity {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY) // 데이터 베이스에 위임
	private Long id;
	
	@ManyToOne
	@JoinColumn(name="order_id")
	private Order order;
	
	private Long productId;
	
	private int orderProductQuantity;
	
	private BigDecimal orderProductPrice;

	@Builder
	public OrderDetail(Long id, Order order, Long productId, int orderProductQuantity,
			BigDecimal orderProductPrice) {
		this.id = id;
		this.order = order;
		this.productId = productId;
		this.orderProductQuantity = orderProductQuantity;
		this.orderProductPrice = orderProductPrice;
	}
	
}

주문과 주문 상세를 두고 하나의 주문에 여러 Item(Product)를 담아 처리할 수 있도록 설계했다.

Product Service

@Entity
@Table(name = "TB_PRODUCT")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Product extends BaseEntity {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY) // 데이터 베이스에 위임
	private Long id;
	
	private String prodTypeCode;
	
	private String prodName;
	
	private BigDecimal prodPrice;
	
	private int prodStock;
	
	@Builder
	private Product(Long id, String prodTypeCode, String prodName, BigDecimal prodPrice, int prodStock) {
		this.id = id;
		this.prodTypeCode = prodTypeCode;
		this.prodName = prodName;
		this.prodPrice = prodPrice;
		this.prodStock = prodStock;
	}
	
}

상품명과 가격, 재고를 관리하는 객체이다.

 

모든 테이블 공통으로 Setter와 생성자를 막아 불변(immutable) 객체로 만들어 사용한다.

(필요한 setter는 변경하려는 기능을 설명하는 method 형태로 구현하여 사용할 예정)

 

BaseEntity는 기본적인 생성시간, 수정시간등을 관리한다.

 

전체 코드는 아래 Git에서 확인 가능하다.

 

github.com/raon-project/toy-project-1

 

raon-project/toy-project-1

Contribute to raon-project/toy-project-1 development by creating an account on GitHub.

github.com

 

'토이프로젝트' 카테고리의 다른 글

주문하기 (1)  (0) 2021.04.17
토이프로젝트 시작  (0) 2021.04.08