ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•
์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

new - Spring Starter Project
๋‹ค์„ฏ๊ฐœ ์ถ”๊ฐ€

DevTools๋Š” ์ž๋™์œผ๋กœ ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํˆด์ด๋‹ค.

 

JSP๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ pom.xml์— ์„ค์ •

๋ถ€ํŠธ์—์„œ JSP๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ dependency๋ถ€๋ถ„ ๋งˆ์ง€๋ง‰์— ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
<!-- JSP ์„ค์ • -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
    
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
 

 

application.properties ์†Œ์Šค์ฝ”๋“œ

JSP์˜ ๋ทฐ๋ฆฌ์กธ๋ฒ„ ์„ค์ •์„ ํ•ด์ค€๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
# server port change
server.port=8000
 
# Datasource setting
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/boot?useSSL=false&serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=mysql
 
 
# JSP viewResolver config
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
 

 

JSP ํด๋”์˜ ๊ตฌ์กฐ ๋งŒ๋“ค๊ธฐ

๋ถ€ํŠธ์—์„œ๋Š” JSP๋ฅผ ๊ถŒ์žฅํ•˜์ง€ ์•Š์•„์„œ ๋”ฐ๋กœ ํด๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋ž˜์„œ JSP๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํด๋”์˜ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผํ•œ๋‹ค.

 

์ปจํŠธ๋กค๋Ÿฌ ์ƒ์„ฑ

SampleController.java ์†Œ์Šค์ฝ”๋“œ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.springboot.boot02.sample;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller
public class SampleController {
 
    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("data""์•ˆ๋…•~~");
        return "hello";
    }
}
 
 

 

MyBatis ์—ฐ๋™ํ•˜๊ธฐ

1. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค DAO ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ

 

2. ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ

1
2
3
4
5
6
7
package com.springboot.boot02.repository;
 
public interface TimeDAO {
    
    String getTime() throws Exception;
 
}
 

 

3. ๋งคํผ ์ƒ์„ฑ

ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์ค€๋’ค xmlํŒŒ์ผ ์ƒ์„ฑ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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">
 
 
<mapper namespace="com.springboot.boot02.repository.TimeDAO">
 
    <select id="getTime" resultType="string">
        select now()
    </select>
 
</mapper>
 

 

4. ์„ค์ •ํŒŒ์ผ์—์„œ ๋งคํผ์œ„์น˜ ์ง€์ •

1
2
# Mapper location config
mybatis.mapper-locations=classpath:mappers/**/*Mapper.xml

 

ํ…Œ์ŠคํŠธํ•˜๊ธฐ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.springboot.boot02;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import com.springboot.boot02.repository.TimeDAO;
 
import lombok.extern.java.Log;
 
@Log
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisTest {
 
    @Autowired
    private TimeDAO dao;
    
    @Test
    public void testTime() throws Exception {
        log.info("==========================================");
        log.info("# ํ˜„์žฌ์‹œ๊ฐ„ : " + dao.getTime());
        
    }
}
 
 

์—๋Ÿฌ๊ฐ€ ๋‚˜๋„ค;

์™œ๋‚˜๋Š”๊ฑฐใ…‘ใ…‡!!!!!!!!!!!!!11

1
2
3
4
5
6
7
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'com.springboot.boot02.MybatisTest': 
Unsatisfied dependency expressed through field 'dao'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.springboot.boot02.repository.TimeDAO' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
 
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€