프로젝트 환경
1. STS 3.9.17
2. Java-vresion 11.0.22
3. Apache Tomcat 9.0
4. Oracle DB
5. MyBatis
[스프링 첫 환경설정]
https://mh030128.tistory.com/7
▶lombok 설치 확인
sts-3.9.17.RELEASE 폴더에 lombk.jar 파일 넣어놓기
cmd 관리자 권한으로 실행 - sts-3.9.17.RELEASE 폴더로 이동 lombok.jar 실행
Installer 창이 나오면 sts.exe 파일 경로 설정한 후 Install / Update 누른 뒤, Install Successfull 나오면 Quit Installer 클릭
STS.ini 파일을 보면 \sts-bundle\sts-3.9.17.RELEASE\lombok.jar 생성된 것을 확인할 수 있음
▶ ojdbc8.jar 추가
- 해당 프로젝트 우클릭 - Build Path - Configure Build Path - Java Build Path - Libraries - Classpath - Add External JARs... - ojdbc8.jar 추가
- Deployment Assembly - Add - Java Build Path Entries - ojdbc8.jar 추가
▶ pom.xml 설정변경
▶ pom.xml에서 스프링 관련 라이브러리 + lombok 추가 :
각 스프링 버전에 따라 ${org.springframework-version} 작성하여 적용되게끔 함
https://mvnrepository.com/artifact/org.springframework/spring-tx/5.0.7.RELEASE
https://mvnrepository.com/artifact/org.springframework/spring-jdbc/5.3.18
https://mvnrepository.com/artifact/org.springframework/spring-test/5.3.9
https://mvnrepository.com/artifact/org.projectlombok/lombok/1.18.24
▶ pom.xml에서 mybatis 사용 위한 라이브러리 추가
https://mvnrepository.com/artifact/com.zaxxer/HikariCP/2.7.8
https://mvnrepository.com/artifact/org.mybatis/mybatis/3.4.6
https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.2
https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1/1.16
▶기타 라이브러리 추가 내용
https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8/21.8.0.0
https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core/2.0.1
https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api/2.0.1
에러가 났을 때 Log4j2 라이브러리를 찾을 수 없다는 내용이 있었음
maven에 Log4j-api와 Log4j-core를 추가해 줘야 함
[pom.xml 전체]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dev</groupId>
<artifactId>controller</artifactId>
<name>Project</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>11</java-version>
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- 스프링 관련 라이브러리 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</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/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<!-- <scope>runtime</scope> -->
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</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/com.oracle.database.jdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.8.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
log4jdbc.log4j2.properties : sql문을 콘솔창에서 직관적으로 볼 수 있게 함
File - New - Other - General - Untitled Text File 생성 후 해당 코드 작성한 뒤 log4jdbclog4jdbc.log4j2.properties 로 저장
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
▶ web.xml 인코딩 추가 (한글 환경설정)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 추가. 인코딩 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
▶ root-context.xml
▶ JDBC 라이브러리 추가 및 JDBC 연결 테스트
src / test / java 폴더에 com.dev.persistence 패키지 생성 후 JDBCTests 클래스 생성
테스트 코드 입력 후 testConnection 더블 클릭한 뒤, 우클릭 - Run as - Junit Test 실행
Junit에 초록색 불 들어오면 DB 연동 잘 된 것
package com.dev.persistence;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
import lombok.extern.log4j.Log4j;
@Log4j
public class JDBCTests {
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(Exception e) {
e.printStackTrace();
}
}
@Test
public void testConnection() {
try (Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe",
"project",
"1234")) {
log.info(conn);
} catch(Exception e) {
e.printStackTrace();
}
}
}
▶ 커넥션 풀(HikariCP) & DataSource 설정 및 테스트
HikariCP는 maven에도 추가하고, root-context.xml에도 DataSource에 대한 설명 추가해줘야 함(해당 코드는 위에 root-context.xml에 작성)
▷ 커넥션 풀(Connection Pool) 이란?
일정량의 Connection 객체를 미리 생성 후 pool에 저장해 둠. 프로그램에서 요청이 오면 Connection 객체를 빌려주고, 해당 객체의 임무가 완료되면 다시 반납 받아서 pool에 저장 하는 프로그래밍 기법
com.dev.persistence 패키지에 DataSourceTests 클래스 생성
해당 코드 작성한 뒤, testConnection 더블 클릭한 후 우클릭 - Run as - Junit Test 실행
package com.dev.persistence;
import java.sql.Connection;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class DataSourceTests {
@Autowired
private DataSource dataSource;
@Test
public void testConnection() {
try(Connection conn = dataSource.getConnection()) {
log.info(conn);
} catch(Exception e) {
e.printStackTrace();
}
}
}
▶ MyBatis 라이브러리 추가 및 SQLSessionFactory 설정
MyBatis 사용하기 위해 maybatis, mybatis-spring, spring-jdbc, spring-tx 라이브러리 필요(pom.xml 입력함)
mybatis-spring : 스프링과 MyBatis 연동시켜주는 라이브러리
spring-jdbc, spring-tx : 스프링에서 DB처리와 트랜잭션 처리하는 라이브러리
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.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-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
root-context.xml에서 SqlSessionFatoryBean 작성 (해당 코드는 위에 root-context.xml에 작성함)
▶ MyBatis란?
자바 어플리케이션과 DB 연동한 프로그래밍을 하기 위해 JDBC 사용함. JDBC 사용할 경우 코드 복잡해지고, 1개의 클래스에 반복되는 코드가 존재하고, 하나의 파일에 자바 언어와 SQL 언어가 같이 있어 재사용성이 안 좋은 것 등 단점 존재. MyBatis는 이러한 JDBC의 단점들 보완하여 DB연동하는 프로그래밍을 좀 더 쉽게 할 수 있도록 도와주는 개발 프레임워크
프로젝트 진행 전 환경설정 완료!
'Project > Spring' 카테고리의 다른 글
[프로젝트]_스프링과 깃허브 연동 (0) | 2024.08.25 |
---|---|
[스프링]_환경설정 (0) | 2024.08.19 |