프로젝트 생성

 

start.spring.io/

 

 

스프링 부트 기반으로 프로젝트를 만들어주는 사이트이다.!!!

 

Maven Project , Gradle Project

 

필요한 라이브러리를 가져다 쓰고 빌드까지 관리해주는 툴이다.

 

과거에는 Maven을 많이 사용했지만,

 

요즘에는 Gradle를 많이쓴다!!

 

 

Spring Boot version 선택

 

SNAPSHOT = 만들고 있는 버전

 

 

Dependencies

사용할 라이브러리 선택!!

 

 

GENERATE를 눌러 설치!!

 

압축을 풀고

 

Eclipse 나 intellij에서 프로젝트 오픈!

JSP의 경우 HTML 태그와 같이 사용되어 전체적인 코드의 가독성이 떨어진다.

이러한 단점을 보완하고저 만들어진 태그가 JSTL이다.

JSTL의 경우 우리가 사용하는 Tomcat 컨테이너에 포함되어 있지 않으므로 , 별도의 설치가 필요하다.

 

 

의존성 설정

mvnrepository.com/artifact/javax.servlet/jstl

 

Maven Repository: javax.servlet » jstl

Professional Java Data: RDBMS, JDBC, SQLJ, OODBMS, JNDI, LDAP, Servlets, JSP, WAP, XML, EJBs, CMP2.0, JDO, Transactions, Performance, Scalability, Object and Data Modeling (2001)by Carl Calvert Bettis, Michael Bogovich, Sean Rhody, Mark Wilcox, Kelly Lin P

mvnrepository.com

 

 

<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

 

설치

jakarta.apache.org/

 

The Jakarta Site - The Apache Jakarta™ Project -- Java Related Products

Founded in 1999, the Jakarta Project housed a diverse set of popular open source Java solutions. In 2005, as a part of creating a flatter Apache Software Foundation, Jakarta subprojects began to become full top-level Apache projects. This process has conti

jakarta.apache.org

 

Taglibs 클릭

 

다운로드

 

JSTL 라이브러리

lib URI Prefix ex
Core http://java.sun.com/jsp/jstl/core c <c:tag />
XML Processing http://java.sun.com/jsp/jstl/xml x <x:tag />
I18N formatting http://java.sun.com/jsp/jstl/fmt fmt <fmt: tag/>
SQL http://java.sun.com/jsp/jstl/sql sql <sql :tag />
Functions http://java.sun.com/jsp/jstl/functions fn fn.function()

 

 

Core 라이브러리

Core 라이브러리는 기본적인 라이브러리로 출력, 제어문, 반복문 같은 기능이 포함되어 있다.

 

 

사용

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

 

 

출력 태그 : <c:out>

<c:out value="출력값" default="기본값" escapeXml ="true or false">

 

변수 설정 태그 <c:set>

<c:set var="변수명" value="설정값" target="객체" property="값" scope="범위">

 

변수를 제거하는 태그 <c:remove>

<c:remove var="변수명" scope="범위">

 

예외 처리 태그 <c:catch>

<c:catch var="변수명">

 

제어문(if) 태그 <c:if>

<c:if test="조건" var="조건 처리 변수명" scope="범위">

 

ex)detail.jsp

<c:if test="${board.user.id == principal.user.id}">
  <a href="/board/${board.id}/updateform" class="btn btn-primary">수정</a>
  <button id="btn-delete" class="btn btn-danger">삭제</button>
  </c:if>

 

제어문(swich) 태그 <c:choose>

 

<c:choose>
<c:when test="조건">처리 내용 </c:when>
<c:otherwise> 처리 내용 </c:otherwise>
</c:choose>

 

반복문 (for) 태그 <c:forEach>

<c:forEach items="객체명" begin="시작 인덱스" end="끝 인덱스" step="증감식"
var="변수명" varStatus="상태변수">

 

ex) detail.jsp

	<ul id="reply--box" class="list-group">
 		<c:forEach var="reply" items="${board.reply }">
 		<li id="reply--1" class="list-group-item d-flex justify-content-between">
             <div>${reply.content}</div>
             <div class="d-flex">
              <div class ="font-italic">작성자 : ${reply.user.username} &nbsp: </div>
              <button class="badge">삭제</button>
             </div>
 		</li>
 		</c:forEach>

 

페이지 이동 태그  <c:redirect>

<c:redirect url="url">

 

파라미터 전달 태그 <c:param>

 

<c:param name="파라미터명" value="값" >

justify-self 가 가로 정렬 이었다면

 

align-self 는 세로 정렬이다.

 

<style>
.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
  }
  .item1{background:LightSkyBlue;}
  .item2{background:LightSalmon;}
  .item3 {background: PaleTurquoise;}
  .item4{background:LightPink;}
  .item5{background:PaleGreen;}

  
</style>

<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>


align-self

3번 상자에 align-self 속성을 사용해보자

 

셀 내부의 세로 정렬

  • start :  셀 내부의 위쪽에 정렬
  • center : 셀 내부의 중심에 정렬
  • end : 셀 내부의 밑에 정렬
.item3 {
    background: PaleTurquoise;
    align-self : start;
  }


.item3 {
    background: PaleTurquoise;
    align-self : center;
  }


.item3 {
    background: PaleTurquoise;
    align-self : end;
  }


 

 

align-items

 

모든 항목에 대해 그리드 세로 정렬 

 

<style>
.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
  }
  .item1{background:LightSkyBlue;}
  .item2{background:LightSalmon;}
  .item3 {background: PaleTurquoise;}
  .item4{background:LightPink;}
  .item5{background:PaleGreen;}

  
</style>

<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>


 

 align-items : start;

 

.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
    
     align-items : start;
  }

위쪽으로 정렬

 


 

align-items : center;

.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
    
     align-items : center;
  }

세로 가운데 정렬!!! ( 텍스트 세로 가운데 정렬을 하기 위해 vertical-align이나 baseline , line-height 속성을 공부하여 

적용 했는데 다 실패했었다)

 

이제 그리드 align-self , align-items를 사용하면 세로 가운데 정렬은 문제없다!!!

 

 


align-items : end;
  

아래쪽으로 정렬

.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
    
     align-items : end;
  }

 


실제로 화면 UI를 구현 하던 중

카드 폼 안에있는 글자를 세로 가운데 정렬하려고 많은 시행 착오를 겪었다.

const CardInfo = styled.div`
    
    display: grid;
    margin-left: 10px;
`;

div 태그 안에 있어서 vertical-align은 적용 되지 않았다.

 

vertical-align같은 경우 inline 이나 inline-block 요소에만 적용 되었다.

 

 

이때 align-items 속성을 사용하여

 

const CardInfo = styled.div`
    
    display: grid;
    align-items: center;
    margin-left: 10px;
`;

세로 가운데 정렬을 할 수 있었다.!!!

 

 

만약

 

align-items: end를 사용하면?

 

 

const CardInfo = styled.div`
    
    display: grid;
    align-items: end;
    margin-left: 10px;
`;

텍스트가 밑에 있는 것을 볼 수 있다.

 

 

 

'CSS' 카테고리의 다른 글

[bootstrap] button 속성  (0) 2021.01.10
[CSS] display: flex (justify-content)  (0) 2021.01.01
[CSS] display: grid - justify-self, justify-items  (0) 2020.12.29
HTML, CSS 색 코드  (0) 2020.12.21
display : inline , block, inline-block  (0) 2020.12.21
<style>
.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
  }
  .item1{background: LightSkyBlue;}
  .item2{background: LightSalmon; }
  .item3{background:PaleTurquoise;}
  .item4{background:LightPink;}
  .item5{background:PaleGreen;}
</style>

<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>

justify-self

css  grid-layout 셀 내부의 위치를 가로로 정렬 

기본 값 :stretch ( 셀의 전체 너비를 채움)

 

 

상자 2에 justify-self의 속성을 사용해보자 

 

 

  •  start :  내용을  셀 왼쪽에 정렬
  • center : 내용을 셀 중앙에 정렬 
  • end : 내용을 셀 오른쪽에 정렬
.item2 {
    background: LightSalmon;
    justify-self : start
   
  }

 

 

.item2 {
    background: LightSalmon;
    justify-self : center
   
  }

.item2 {
    background: LightSalmon;
    justify-self : end
   
  }

'

 

 

justify-items

 

모든 항목에 대해 적용 하고 싶을 때

<style>
.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
  }
  .item1{background: LightSkyBlue;}
  .item2{background: LightSalmon; }
  .item3{background:PaleTurquoise;}
  .item4{background:LightPink;}
  .item5{background:PaleGreen;}
</style>

<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>

5개의 상자를 감싸고 있는 컨테이너 클레스에 justify-items 속성 사용

 

justify-items : center 

.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
   
    justify-items: center;
  
  }


justify-items : start

.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
   
    justify-items: start;
  
  }


 

justify-items : end

.container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
   
    justify-items: end;
  
  }

'CSS' 카테고리의 다른 글

[CSS] display: flex (justify-content)  (0) 2021.01.01
[CSS ] 세로 가운데정렬 : align-self , align-items  (0) 2020.12.29
HTML, CSS 색 코드  (0) 2020.12.21
display : inline , block, inline-block  (0) 2020.12.21
CSS - background 속성  (0) 2020.12.21

의존성 설정

	<!-- 시큐리티 태그 라이브러리 -->
		
		<dependency>
		  <groupId>org.springframework.security</groupId>
		  <artifactId>spring-security-taglibs</artifactId>
		</dependency>
        
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

 

시큐리티 taglib 선언

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

 

security taglib

www.baeldung.com/spring-security-taglibs

 

Introduction to Spring Security Taglibs | Baeldung

Learn how to use Spring Security's tag library in your JSPs for finer-grained control of your views.

www.baeldung.com

 

 

시큐리티를 설정한 뒤 서버를 실행하면

이런 화면이 생성 된다.

 

어떤 url을 지정해도 스프링 시큐리티 라이브러리가 

서버를 가로채서

/login 으로 이동시킨다.

 

 

이때 아이디는 user를 입력하고

 

비밀번호는 

cosole에 있는 것을 입력한다.

 

 

이때 위에서 선언한 taglib와 함께

<sec:authorize access="isAuthenticated()">
	
	<script>
		alert("시큐리티 로그인");
	</script>
	<sec:authentication property="principal" var="principal" />
	
	
</sec:authorize>

속성을 지정한다.

 

isAuthenticated() = 시큐리티 로그인이 되었을때

 

principal = 시큐리티가 설정된 사용자에 대한  접근 권한

 

 

 

문서!

 

docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

 

15. Expression-Based Access Control

There are some built-in expressions which are specific to method security, which we have already seen in use above. The filterTarget and returnValue values are simple enough, but the use of the hasPermission() expression warrants a closer look. The Permiss

docs.spring.io

 

 

 

세션과 같은 역할을 하는 principal 사용

 

<c:choose>
		<c:when test="${empty principal}">
			<ul class="navbar-nav">
				<li class="nav-item">
               <a class="nav-link" href="/loginform">로그인</a></li>
				<li class="nav-item">
                <a class="nav-link"	href="/joinform">회원가입</a></li>
					</ul>
		</c:when>
		<c:otherwise>

			<ul class="navbar-nav">
				<li class="nav-item">
                <a class="nav-link" href="/board/form">글쓰기</a></li>
				<li class="nav-item">
                <a class="nav-link"	href="/user/form">회원정보</a></li>
				<li class="nav-item">
                <a class="nav-link"	href="/logout">로그아웃</a></li>
			</ul>
		</c:otherwise>

</c:choose>

 

 

시큐리티의 디폴트 url은  로그인 : /login , 로그아웃 : /logout

 

 

로그아웃:

 

기본 시큐리티 로그인 , 로그아웃에

사용자가 직접 만든 로그인, 로그아웃 form으로 커스터마이징 하여 사용하면 된다.

 

 

시큐리티를 사용하기 전

세션을 이용한  전통적인 로그인 방식

 

 

 

loginform.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ include file="../layout/header.jsp"%>
<form >
  <div class="form-group">
    <label for="username">아이디:</label>
    <input type="text" class="form-control" placeholder="아이디를 입력하세요" id="username">
  </div>
  
  <div class="form-group">
    <label for="pwd">비밀번호:</label>
    <input type="password" class="form-control" placeholder="비밀번호를 입력하세요" id="password">
  </div>
  <div class="form-group form-check">
    <label class="form-check-label">
      <input class="form-check-input" type="checkbox"> Remember me
    </label>
  </div>
</form>
  <button id="btn-login" class="btn btn-primary">로그인</button>

<script src="/js/user.js"></script>
<%@ include file="../layout/footer.jsp" %>

<script scr="/js/user.js"></script>

 

->

user.js

let index={
	init:function(){
		$("#btn-save").on("click",()=>{ //화살표 함수를 쓰는이유 this를 바인딩 하기 위해서
			this.save();
		});
		$("#btn-login").on("click",()=>{ //화살표 함수를 쓰는이유 this를 바인딩 하기 위해서
			this.login();
		});
	}
	,
	save:function(){
		//alert('user의 save함수 호출 됨')
		
		let data ={
			username: $("#username").val(),
			password: $("#password").val(),
			email: $("#email").val
		};
		
		//console.log(data);
		// ajax 호출시 default가 비동기 호출
		// ajax 통신을 이용해서 3개의 데이터를 json으로 변경하여 요청
		$.ajax({
			//회원가입 수행 요청
			type:"POST",
			url:"/api/user",
			data: JSON.stringify(data), // http body 데이터
			contentType:"application/json; charset=utf-8", //body 데이터가 어떤 타입인지(MIME)
			dataType: "json" // 요청을 서버로해서 응답이 왔을 때 기본적으로 모든것이 문자열
		}).done(function(resp){
			alert("회원가입이 완료되었습니다.");
			console.log(resp);
			location.href="/"
			
		}).fail(function(error){
			alert(JSON.stringify(error));
			
		}); //ajax 통신을 이용해서 3개의 데이터를 json으로
		//변경하여 insert 요청!!
	},
	login:function(){
		//alert('user의 save함수 호출 됨')
		
		let data ={
			username: $("#username").val(),
			password: $("#password").val(),
			
		};
		
		//console.log(data);
		// ajax 호출시 default가 비동기 호출
		// ajax 통신을 이용해서 3개의 데이터를 json으로 변경하여 요청
		$.ajax({
			//회원가입 수행 요청
			type:"POST",
			url:"/api/user/login",
			data: JSON.stringify(data), // http body 데이터
			contentType:"application/json; charset=utf-8", //body 데이터가 어떤 타입인지(MIME)
			dataType: "json" // 요청을 서버로해서 응답이 왔을 때 기본적으로 모든것이 문자열
		}).done(function(resp){
			alert("로그인이 완료되었습니다.");
			console.log(resp);
			location.href="/"
			
		}).fail(function(error){
			alert(JSON.stringify(error));
			
		}); //ajax 통신을 이용해서 3개의 데이터를 json으로
		//변경하여 insert 요청!!
	}
}

index.init();

ajax 통신을 이용!

url = /api/user/login

 

->

 

UserApiController.java

@Autowired
	private UserService userService;
	
	@Autowired
	private HttpSession session;

@PostMapping("/api/user/login")
	public ResponseDto<Integer> login(@RequestBody User user ){
		System.out.println("login 호출 됨 ");
		User principal = userService.로그인(user);
		//principal (접근주체)
		if(principal != null) {
			session.setAttribute("principal", principal); //세션 만듬
			
		}
		
		return new ResponseDto<Integer>(HttpStatus.OK.value(),1);
	}

UserService의 로그인 메서드 수행한 로직이

null 이 아니면

세션을 만들어

로그인 로직을 수행!!

 

 

UserService.java

@Autowired
	private UserRepository userRepository;
    
@Transactional(readOnly =true) 
	public User 로그인(User user) {
		
		return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
		
	}

 

설정한 세션 값을 이용해

 

header.jsp

 

<div class="collapse navbar-collapse" id="collapsibleNavbar">
			<c:choose>
				<c:when test="${empty sessionScope.principal}">
					<ul class="navbar-nav">
						<li class="nav-item"><a class="nav-link"
							href="/loginform">로그인</a></li>
						<li class="nav-item"><a class="nav-link"
							href="/joinform">회원가입</a></li>
					</ul>
				</c:when>
				<c:otherwise>

					<ul class="navbar-nav">
						<li class="nav-item"><a class="nav-link"
							href="/board/form">글쓰기</a></li>
						<li class="nav-item"><a class="nav-link"
							href="/user/form">회원정보</a></li>
							<li class="nav-item"><a class="nav-link"
							href="/logout">로그아웃</a></li>
					</ul>
				</c:otherwise>

			</c:choose>
		</div>

화면 구성

 

www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

 

JSP - Standard Tag Library (JSTL) Tutorial - Tutorialspoint

JSP - Standard Tag Library (JSTL) Tutorial In this chapter, we will understand the different tags in JSP. The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates the core functionality common to many JSP appli

www.tutorialspoint.com

 

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

jsp 파일에 붙여넣기

 

 

 

의존성 설정

<!-- JSTL -->
<dependency>
 	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
</dependency>

 

 


※  그 전에 JSP 파일을 스프링 부트에서 사용하려면

 

<!-- JSP 템플릿 엔진 -->
		 	
		 <dependency>
		  <groupId>org.apache.tomcat.embed</groupId>
		  <artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

JPA란?

Java Persistence API 이다.

 

Persistence(영속성)

 

영속성(persistence)은 데이터를 생성한 프로그램의 실행이 종료되더라도 사라지지 않는 데이터의 특성을 의미한다. 영속성은 파일 시스템, 관계형 테이터베이스 혹은 객체 데이터베이스 등을 활용하여 구현한다. 영속성을 갖지 않는 데이터는 단지 메모리에서만 존재하기 때문에 프로그램을 종료하면 모두 잃어버리게 된다. 결국 영속성은 특정 데이터 구조를 이전 상태로 복원할 수 있게 해주어 프로그램의 종료와 재개를 자유롭게 해준다.

출처: https://sugerent.tistory.com/587 [MISTERY]

 


 

의존성 설정

 <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

 

 

어노테이션

 

@Entity

DB에 테이블을 생성해주는 어노테이션

import javax.persistence.Entity;
@Entity // User 클래스가 MySql에 테이블이 생성이 된다.
public class User {
	
	@Id
	private int id; // 시퀀스, auto-increment
	private String username; //아이디
	private String password;
	private String email;
	private Timestamp createDate;
}

 

MySql에 User 테이블이 생성 된다.


@Id

DB에 primary key 생성

import javax.persistence.Id;
	@Id //primary key
	private int id; // 시퀀스, auto-increment
	private String username; //아이디
	private String password;
	private String email;
	private Timestamp createDate;

@GeneratedValue

넘버링 전략

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
	@Id //primary key
	@GeneratedValue(strategy = GenerationType.IDENTITY) //프로젝트에 연결된 DB의 전략을 따라감
	private int id; // 시퀀스, auto-increment
	private String username; //아이디
	private String password;
	private String email;
	private Timestamp createDate;

 

4가지 타입이 있다.

그 중 IDENTITY를 사용하면 프로젝트에 연결된 DB(필자는 mysql) 의 전략을 따라감

 

ex) 오라클의 경우 -> sequence , MySql의 경우 -> Auto Increment

 


 

 

 

@Column

DB에 Column에 해당하는 속성을 지정

import javax.persistence.Column;

속성은 다음과 같다.

 

결론

 JPA =

자바 프로그래밍을 할 때 영구적으로 데이터를 저장하기 위해 필요한 인터페이스 이다.

  • JPA는 ORM 기술이다.
  • JPA는 반복적인 CRUD 작업을 생략하게 해준다.
  • JPA는 DB와 OOP의 불일치성을 해결하기 위한 방법론을 제공한다.
  • JPA는 OOP의 관점에서 모델링을 할 수 있게 해준다. (상속, 콤포지션,연관관계)
  • 방언 처리가 용이하며 Migration하기 좋음, 유지 보수에도 좋다
  • JPA는 쉽지만 어렵다

 

출처 : https://getinthere.tistory.com/10

@AllArgsConstructor

lombok 어노테이션 의 하나로,

 

모든 매개변수가 있는 생성자를 의미한다.

 

 

@AllArgsConstructor

=

 

Member.java

public class Member {
	
	private int id;
	private String username;
	private String password;
	private String email;
	
	
	
	public Member(int id, String username, String password, String email) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
	}

으로 @AllArgsConstructor 어노테이션을 사용하면 굳이 이렇게 생성자를 만들 필요가 없다.

 

 

대신, 매개변수를 3개만 받는 경우

 

즉, 생성자 오버로드로 필요에 따른 매개변수를 설정을 할 때는 오류가 발생한다.

@RestController
public class HttpControllerTest {
	
	@GetMapping("/http/lombok")
	public String lombokTest() {
		Member m =  new Member(100,"hello","ddd@naver.com"); 
        //오류 발생
		
		
		
		return "lombok test 완료
	}
	
}

 

그렇기 때문에 그에 맞는 매개변수가 있는 생성자가 필요하다.

 


	public Member(int id, String username, String email) {
		super();
		this.id = id;
		this.username = username;
		this.email = email;
	}

 

매번 이렇게 생성자를 추가해야 할까?

 

이때 사용하는 것이 @Builder 어노테이션이다.

 


@Builder

생성자가 여러개 있을 필요 없이,

 

모든 매개변수가 있는 생성자를 하나 만든다.

 

	@Builder
	public Member(int id, String username, String password, String email) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
	}

생성자 위에 어노테이션을 입력한다.

 

※ @AllArgsConstructor은 모든 매개변수가 있는 생성자와 같은 역할이기때문에 중복이된다.

오류가 나기 때문에 Builder 패턴을 사용할 땐 지워야 한다.

 

 

이제 Builder 패턴을 사용해보자

 

Member m =  new Member(100,"hi","ddd@naver.com");

대신 ->

 

Member m =   Member.builder().email("ddd@").password("1122").build();

.builder()로 열고 .build()로 닫는다.

 

핵심은 !!

id,username,password,email 다 값을 넣어도 되고 일부분만 값을 넣어도 된다.

그리고 제일 좋은 점은 순서에 구애 받지 않는다는 것이다.

 

생성자로 초기화 할 때는 id,username, password, email 순서로 초기화를 시켜야 하지만

Builder 패턴은 순서에 상관없이 초기화 할 수 있는 것이 큰 장점이다.

 

 

projectlombok.org/download

 

Download

 

projectlombok.org

다운로드

 

 

 

설정

 

경로:

C:\Users\사용자이름\.m2\repository\org\projectlombok\lombok\1.18.16

 

git bash here

 

 

 

 

 

 

select 한 뒤

 

install 완료

 

 

 

 

 

 

+ Recent posts