본문 바로가기

노력/Spring Boot

Spring Boot와 Spring의 디렉터리 구조 차이점 그리고 Entity

Spring
Spring boot

제가 완성해 본 프로젝트들은 모두 Spring으로 개발했었습니다. Spring은 각 기능마다 VO, DAO, Proc, Controller 그리고 각각을 인터페이스로 만들어서 제작했었습니다. Spring Boot를 사용하려고 여러 예제들을 확인해보니 사람들마다 사용하는 구조가 크게 두 부류로 나뉘는 것 같았습니다.

 

주노의 IT 지식창고, Spring Boot 디렉터리 구조

 

자바가이드넷, 기존에 사용하던 기능별 디렉터리 구조

대부분의 Spring Boot 프로젝트가 위의 두 부류로 나눌 수 있었습니다. 그리고 기존에 사용하던 파일명과는 다르게 분류돼 있는 것을 확인할 수 있습니다. 위에서 언급했던 대로 기존에는 VO, DAO, Proc, Controller를 사용했다면, 이제는 VO를 붙이지 않은 Model 그리고 Repository, Service, Controller로 나뉩니다.

 

현재 프로젝트 디렉터리 구조

디렉터리 구조는 사용해보지 않은 새로운 구조로 만들기로 했습니다. 

 

package com.tistory.jimin.twitter.model;



import java.sql.Date;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;



@Entity

public class User {

	

	@Id

	@GeneratedValue

	private int id;

	

	@Column(length=50, nullable=false)

	private String email;

	

	@Column(length=45, nullable=false)

	private String password;

	

	@Column(length=45, nullable=false)

	private String nickname;





	private char grade;

	private int score;

	private Date joinDate;

	

	

	public User(String email, String password, char grade, int score) {

		super();

		this.email = email;

		this.password = password;

		this.grade = grade;

		this.score = score;

	}

	

	

	public int getId() {

		return id;

	}

	public void setId(int id) {

		this.id = id;

	}

	public String getEmail() {

		return email;

	}

	public void setEmail(String email) {

		this.email = email;

	}

	public String getPassword() {

		return password;

	}

	public void setPassword(String password) {

		this.password = password;

	}

	public char getGrade() {

		return grade;

	}

	public void setGrade(char grade) {

		this.grade = grade;

	}

	public int getScore() {

		return score;

	}

	public void setScore(int score) {

		this.score = score;

	}

	public Date getJoinDate() {

		return joinDate;

	}

	public void setJoinDate(Date joinDate) {

		this.joinDate = joinDate;

	}

}

미리 생각해 둔 DB 구조에 따라서 만든 User 모델입니다. 각 JPA를 사용할 때는 Entity 클래스가 곧 DB 구조가 된다고 생각하면 된다고 하니 Entity가 곧 평소에 생각해 뒀던 모델과 같다고 할 수 있습니다. 엔티티는 적어도 @Entity와 @Id 어노테이션을 기본으로 가져야합니다. @Entity 어노테이션이 달린 클래스는 DB와 1:1로 매핑됩니다. @Id 어노테이션은 primary key를 나타내며 @GeneratedValue는 생성 규칙을 만들어 줄 수 있습니다.