티스토리 뷰

반응형

 

 

| Spring-Boot ?


 

Spring에서 Application 개발 시

library 추가, dependency 설정, 여러 구성 및 설정파일 등사전에 많은 작업이 필요한 단점을 해결

 

Spring Boot프로젝트에 다라 자주 사용되는 Library들이 미리 조합되어있고, 자동으로 설정을 처리내장 서버가 있어서 WAS를 추가로 설치하지 않아도 개발 가능내부적으로 가지고 있는 tomcat을 실행

 

 

 

 

 

|| Project 생성


 

1. New - Spring Starter Project 로 생성

 

2. project 속성 입력

Spring Starter Project 를 생성할 수 없는 

intellij와 같은 경우는https://start.spring.io/ 에서 똑같이 설정 후 생성되는 zip File을 import 해주면 된다.

 

 

3. dependency 설정

 

spring Application 개발 시 자주 사용하는 Library

jstl, mybatis, mabatis-spring, DB Driver 등을 미리 조합할 수 있다.

Spring Boot DebTools Library는 코드 변경 시 자동적으로 서버를 재실행준다 ! 

꼭 추가해 주길!!

1
2
3
4
5
6
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
cs

 

 

 

 




 

 

 

|| 기본 설정

 


 

 

 

 /src/main/java

 java source directory

 Application.java

 application을 시작할 수 있는 main method가 존재하는 spring 구성 Main Class

 static

 html, css, js, img 등 정적 resource directory

 templates

Spring Boot에서 사용 가능한 여러가지 View Template(Thymeleaf, Velocity, FreeMarker, jsp  등) 위치 

 application.properties

application 및 spring의 설정 등에서 사용할 여러 가지 property를 정의한 file 

 src/main 

jsp 등의 resource directory 

 

 

spring boot 에서 모든 설정(servlet-context, root-context 등)은 application.properties 에 작성된다.외부에서 읽어들일 필요가 없으므로 web.xml도 필요가 없고webapp에는 결국 화면에 보여줄 View 파일만 존재한다.
또한, main()을 가지고 있는 XXXApplication.java 는 @SpringBootApplication annotaion 이 적용되어있는데,annotation을 살펴보면 @EnableAutoConfiguration, 

@ComponentScan 도 함께 적용된 것을 알 수 있다.

따로 명시를 해주지 않아도 하위 패키지(controller, model 등)는 자동으로 Scan 해준다.

/src/main/java/com/cristoval/web/Application.java

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@MapperScan(value = "com.cristoval.web.model.repo")
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
 
}
cs

 

 

 

Spring boot는 JSP 사용을 권장하지 않으므로

JSP 사용을 위해 /src/main/webapp/WEB-INF/views/ 폴더를 만들고 

여기에 jsp 파일을 만들어주어야 한다.

 

폴더와 jsp 파일을 만들어주고,

application.properties 파일에 WAS server setting, View Resolver Setting 이 필요하다.spring boot의 설정은 xml 대신 application.properties 에서 해준다./src/main/resources/application.properties

1
2
3
4
5
6
7
# WAS server setting
server.port=8080
server.servlet.context-path=/product
 
# View Resolver setting
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
cs

 

 

 

다음으로, 아래 Library를 dependency에 추가해준다.

jstl

tomcat-embed-jasper

version 체크를 해주지 않는 이유는 spring boot가 알아서 최신 버전을 등록해준다.

특정 version 을 설정하고 싶다면 properties 에 설정해주면 된다.

/pom.xml

1
2
3
4
5
6
7
8
9
10
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
cs

 

 

 

|| Database, MyBatis 설정

 


 

Spring Boot에서도 

mybatis-config.xml

mapper.xml

파일은 필요하다.

/src/main/resources/maybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
<configuration>
     
    <!-- 사용하려는 DTO에 대한 축약 이름 -->
    <typeAliases>
        <typeAlias type="com.cristoval.web.model.dto.Product" alias="product" />    
    </typeAliases>
    
    <!-- 사용할 쿼리에 대한 등록 -->
    <mappers>
        <mapper resource="product.xml" />
    </mappers>
    
</configuration>
cs

 

 

namespace는 Repo Interface를 활용한다.

/src/main/resources/product.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace : package + classname -->
<mapper namespace="com.cristoval.web.model.repo.ProdRepository">
 
    <!-- Product select(String prodId); -->
    <select id="select" parameterType="string" resultType="product">
        select *
        from product
        where id = #
    </select>
 
    <!-- ... -->
    
</mapper>
cs

 

 

 

 

다음으로, application.properties에 DB 접속 설정과 MyBatis 관련 설정을 추가로 해주자.
spring.datasource.* DB 접속 관련 설정과 함께 DataSource도 자동으로 생성된다.mybatis.config-location sqlSession이 자동으로 생성된다./src/main/resources/application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# WAS server setting
server.port=8080
server.servlet.context-path=/product
 
# View Resolver setting
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:0000/dbname?serverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8
spring.datasource.username=userpw
spring.datasource.password=userid
 
mybatis.config-location=classpath:/mybatis-config.xml
 
# Check Log
logging.level.com.example=trace
cs

 

 

|| Spring to SpringBoot

 


 

spring -> Spring Boot 로 바꾸는 과정은 생각보다 간단하다 !

 

Controller, Service .. 와 같은 java 파일은 변경해줄 필요가 없다.ApplicationConfig 의 설정은 다 application.properties 로 이동하고,MVCConfig 의 ViewResolver 설정도 application.properties 로 이동한다.(정적 리소스는 모두 static에)
XXXApplication.java 파일에 

 

@MapperScan(value = "com.cristoval.web.model.repo") 을 추가해주면 끝 !!

 

 

 

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday