이 글에서는 스프링 부트를 사용하여 포트폴리오를 만드는 과정을 기술한다.

개발환경은 다음과 같다.

- IDE : STS4

- 자바 : JDK 11.0.7

- 데이터베이스 : MySQL 8.0.29

먼저 STS4를 이용하여 프로젝트를 생성한다.

STS4에서 메뉴 File->New->Spring Start Project를 선택한다.

아래 그림과 같이 설정하고 Next 버튼을 클릭한다.

다음과 같이 dependencies를 설정하고 Finish 버튼을 클릭한다.

Spring Boot DevTools, Lombok, Spring Data JPA, Spring Security, Thymeleaf, Spring Web을 설정해 주었다. 예를 들어 Available에서 security를 입력하면 Security->Spring Security 항목을 입력할 수 있다.

아래 그림과 같은 폴더가 Package Explorer에서 생성됨을 알 수 있다.

build.gradle 파일을 열어보면 다음과 같은 정보가 있음을 알 수 있다.

plugins {
	id 'java'
	id 'war'
	id 'org.springframework.boot' version '2.7.6'
	id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.security:spring-security-test'
}

tasks.named('test') {
	useJUnitPlatform()
}

추가로 MySQL connector-java 라이브러리를 build.gradle dependency 부분에 추가한다.

	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.security:spring-security-test'
	
	implementation("mysql:mysql-connector-java:8.0.29")	// 추가된 부분
}

 

데이터베이스 연동을 위해 MySQL 데이터베이스에서 database myapp을 생성하고, 사용자 'bootuser'@'localhost'와 'bootuser'@'%'를 추가하고, 사용자에게 테입터베이스 접근 권한을 부여한다.

-- 데이터베이스 생성

create database myapp;

-- 사용자 생성
create user 'bootuser'@'localhost' identified by 'bootuser';	-- 서버에서 접속할 수 있는 사용자
create user 'bootuser'@'%' identified by 'bootuser';			-- 다른 컴퓨터에서 접속할 수 있는 사용자

-- 권한 부여
grant all privileges on myapp.* to 'bootuser'@'localhost';
grant all privileges on myapp.* to 'bootuser'@'%';

-- 디스크에 저장
commit;

프로젝트의 설정 파일 application.properties 파일은 다음과 같다. JDBC 연동을 위한 Database 설정과 Spring Data JPA를 위한 설정을 추가하고, security 로그를 위한 설정을 추가한다.

# Database
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myapp
spring.datasource.username=bootuser
spring.datasource.password=bootuser

# Spring Data JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true

spring.thymeleaf.cache=false

# security
logging.level.org.springframework.security.web= debug
logging.level.kr.talanton.sboot.security = debug

아직 security를 위한 설정은 추가되지 않은 상태이다.

마우스로 프로젝트를 선택하고 우클릭 후 Gradle->Refresh Gradle Project를 선택하여 필요한 라이브러리를 다운로드 받는다. Project and External Dependencies에서 build.gradle에 설정한 라이브러리가 존재하는지 확인해 본다.

아래와 같이 기본 테스트 코드를 추가한 후 테스트 코드를 실행한다.

package com.example.sboot;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbootApplicationTests {

	@Test
	void contextLoads() {
		System.out.println("context loaded");
	}
}

간단하게 context가 로딩이 되는지 확인할 수 있는 코드이다. 테스트 코드를 마우스로 선택한 후 우클릭하고 Run AS->JUnit Test를 선택한다.

위 그림과 같이 security를 위한 user의 password가 추력되고, 테스트가 수행되어 context loaded가 출력됨을 알 수 있다.

또한 JUnit 테스트 수행결과가 표시됨을 알 수 있다.

소스 코드는 다음과 같다.

sboot.zip
0.08MB

Posted by 세상을 살아가는 사람
,
Posted by 세상을 살아가는 사람
,

1조 파비 중고 거래 플랫폼

2조 골라줘 점심즈

3조 All Food In The World

4조 momentum_ex

1조 파비 중고 거래 플랫폼 기획서

기획서1.pdf
0.51MB

1조 파비 중고 거래 플랫폼 발표자료

Pa-Bi-발표자료.pptx
4.07MB

2조 골라줘 점심즈 기획서

골라줘 점심즈- 수정(발표용).pptx
1.79MB

2조 골라줘 점심즈 결과 발표자료

골라줘 점심즈- 최종(발표용).pptx
19.09MB

3조 All Food In The World 기획서

계획서_자바 포토폴리오 3조(최종본).pptx
9.12MB

3조 All Food In The World 결과 발표

FIT-발표자료-최종.pptx
16.57MB

4조 momentum_ex 기획서

Momentum_extension 학원.pdf
0.25MB

4조 momentum_ex 결과 발표

김정현_포트폴리오_학원.pdf
2.47MB

Posted by 세상을 살아가는 사람
,