1. 환경설정
VS code 로 진행할것이기에 Extention 에서 필요한 확장팩들을 설치한다.


이외에 Gradle, Lombok 등 필요한것들을 설치. Install 만 누르면 알아서 설치됨
이외에 자바같은건 알아서 설치
2. 프로젝트 생성
Crtl+Shift+P 누르면

검색창 나오는데 이곳에 Spring Initializr gradle Project 로 생성
버전은 3.3.1, Java, War, gradle, Java 17로 생성하였고 패키지 및 프로젝트 명은 임의로 지정
마지막에 dependencies 를 골라야하는데
Lombok, Spring boot DevTools, Spring Web, MyBatis Framwork 를 골랐음.
3. 기본 폴더(패키지) 생성
게시판을 만들것이기에 post 로 프로젝트를 만들었다.
아래와 같이 우선 기본 폴더들을 생성해준다.

src/main/java/com/example/post 아래
spring MVC 패턴을 위한 기본 패키지(폴더)를 생성해준다.
controller, service, dao 패키지(폴더)를 만들어주고, VO 객체가 들어갈 model 패키지를 생성하였다.
이후 추가될 예정임
src/main/resources 아래는
Mybatis 에서 사용할 mapper 폴더를 만들어주었다
src/main 아래는 웹 프로그래밍을 위한 webapp 폴더를 만들어준다.
그 아래 WEB-INF/jsp 폴더까지 만들어주어 jsp 파일을 위치시킬 준비를 해준다. (보안을 위하 WEB-INF 를 만들어주는것이 좋음)
4. 기본 application properties 생성
src/main/resources 아래보면 application.properties라는 파일이 있다.
properties 를 yaml 형식으로 관리할 수 있지만.. 우선 properties 로 관리 하겠다.
application.properties 아래 다음과 같은 설정값들을 입력한다.
#내프로젝트 이름 spring.application.name=post #MyBatis 에서 사용할 Mapper 위치 지정 mybatis.mapper-locations=classpath:mapper/*.xml #테스트할 내장 톰캣 포트 지정 server.port=8082 # DB 설정값 입력 (MSSQL 사용) spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.url=jdbc:sqlserver://${IP}:${PORT};databaseName=${DBNAME};;encrypt=true;trustServerCertificate=true spring.datasource.username=${USERNAME} spring.datasource.password=${PASSWORD} # Context Path 지정 server.servlet.context-path=/ # 기본 JSP 위치 지정 spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
5. gradle 에서 dependency 확인
src 말고 아래 더 보면 build.gradle 이라는 파일이 있다.
여기서 디펜던시를 관리하는데, 관련디펜던시는 mvn repository 에서 추가할 내용들을 확인하면된다.
현재 내 디펜던시
dependencies { // implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' 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.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3' // testImplementation 'org.springframework.security:spring-security-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' //file 업로드 용 implementation 'org.springframework:spring-webmvc:5.3.9' implementation 'commons-fileupload:commons-fileupload:1.4' implementation 'commons-io:commons-io:2.11.0' implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.apache.tomcat.embed:tomcat-embed-jasper' implementation 'jakarta.servlet:jakarta.servlet-api' implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api' implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl' runtimeOnly group: 'com.microsoft.sqlserver', name: 'mssql-jdbc' }
우선 아까 프로젝트 생성 시 적용한 것만으로도 작동은 되니 프로젝트 기능 추가와 함께 gradle dependencies 를 관리해도 된다.
6. Spring 프로젝트 기동
웹 페이지를 띄우기 위해 webapp/WEB-INF/jsp 아래 hello.jsp 를 하나 생성해주자.
hello.jsp
hello spring!
추가로 dispatcher servlet 이 url 를 넘겨주기 위해 controller 를 하나 생성한다.
위치는 src/main/java/com/example/post/controller (앞으로는 com.example부터 기재 예정)
helloController.java 를 만들어준다.
package com.example.post.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class helloController { @GetMapping("/hello") public String requestMethodName() { return "hello"; } }
이렇게 작성하면 IP:Port/hello 로 들어오는 요청을 hello.jsp 로 보내준다. (application.properties 에 context-path 를 / 로 설정하여서 프로젝트 명이 붙지 않음)
둘다 저장 후 PostApplication.java 를 클릭한다.
메인 Application 을 클릭하면

VS Code 의 경우 위에 Run 이 생기는데 저걸 클릭하면 실행이 된다.
terminal 항목에서 로그 output 내용을 확인할 수 있다.

접속확인
