[스프링SPRING]스프링게시판1: 기초

프로젝트 시작전

  • DB

    • 사용 계정 생성 (root/1234)
    • 디비 생성 (springdb)
    • 테이블 생성 + 설계 (관계 설정)
    • 더미데이터 추가
  • MVC

    • 패키지 구조 결정
    • 개발역활 구분 -> 할당
    • 테스트 처리 여부
  • 화면
    • 화면 출력 방식 결정 : HTML+JS 또는 JSP
    • 경로설정 : 절대경로와 상대경로




기본설정 : pom.xml

  • 기존 pom.xml에 Servlet주석부분에 아래 코드를 추가한다.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- 추가 라이브러리 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
</dependencies>




기본설정 : log4jdbc 설정

  • pom.xml로 가져왔으므로 아래 파일들을 src/main/resources에 추가해준다.




기본설정 : DB연결

DB 연결이 잘되었는지 테스트를 헤야한다.

  1. root-context.xmlnamespace탭에서 필요한 것들 tick하기
  2. root-context.xml에 DB객체 생성
1
2
3
4
5
6
7
8
9
10
11
<!-- DB연결 객체 -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
<property name="url" value="jdbc:log4jdbc:mysql://localhost:3306/springdb?useSSL=false"/>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
</bean>




기본설정 : DB연결 테스트하기

DB 연결이 잘되었는지 테스트를 헤야한다.

  1. src/test/java/controller에 test class를 생성한다
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
29
package com.itwiilbs.controller;

import java.sql.Connection;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
public class boardDataSourceTest {
//datasource객체 생성(DI)
//@Inject
private DataSource ds;

@Test
public void testDataSource() throws Exception{
System.out.println("ds 테스트 "+ds); //ds의 객체생성이 안되면 null로 출력됨
try (Connection con = ds.getConnection()){
System.out.println("Conntect확인 " + con);
}catch(Exception e) {
e.printStackTrace();
}
}
}
  1. Junit으로 run을 해준다.
  2. 작동이 정상인지 확인한다.

Comments