// Spring Boot + Mybatis
- New > Spring Starter Project
~ Name > boot-mybatis
~ Type > Maven
~ Packaging > Jar
~ Java Version > 11
~ Language > Java
~ Group > com.test
~ Aritfact > boot-mybatis
~ Package > com.test.mybatis
~ Spring Boot Version > 2.7.13
~ Spring Web
~ Oracle Driver
~ MyBatis Framework
~ Lombok
~ Spring Boot DevTools
- jsp 설정
1. pom.xml
- 의존성 2개 추가
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>boot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. application.properties > ViewResolver
# 톰캣 포트 번호
server.port = 8092
# JSP Veiw Resolver
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
# HikariCP settings
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username='사용자명'
spring.datasource.password='비밀번호'
3. src> main > "webapp" > "WEB-INF" > "views" 폴더 추가
- 파일 생성
※ com.test.mybatis.controller > 기본 패키지의 하위 패키지로 넣으면 스캔을 따로 할 필요 x
- 스캔할 거면 main 파일로 이동해서 @ComponentScan 어노테이션 활용
- @MapperScan > 매퍼 파일 스캔
~ "com.test.controller" > "MyBatisController.java"
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MyBatisController {
@GetMapping("/ex01.do")
public String ex01() {
return "ex01";
}
}
~ "com.test.mapper" > "MyBatisMapper.java"(I)
package com.test.mapper;
public interface MyBatisMapper {
String time();
}
~ views > "ex01.jsp"
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="http://pinnpublic.dothome.co.kr/cdn/example-min.css">
<style>
</style>
</head>
<body>
<h1>MyBatis</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
~ com.test.mybatis > BootMybatisApplication > 매퍼, 컨트롤러 스캔
package com.test.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
//스프링 부트 > 기본 스캔 대상 > OOOApplication 파일이 위치한 패키지
@SpringBootApplication
@ComponentScan(basePackages = {"com.test.controller"})
@MapperScan(basePackages = {"com.test.mapper"})
public class BootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(BootMybatisApplication.class, args);
}
}
~ src/main/resources > "com" > "test" > "mapper" > "MyBatisMapper.xml" > 테스트용 쿼리 짜기
<?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.test.mapper.MyBatisMapper">
<select id="time" resultType="String">
select sysdate from dual
</select>
</mapper>
~ src/test/java > "MapperTest.java" > 테스트 파일
package com.test.mybatis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.test.mapper.MyBatisMapper;
import lombok.extern.slf4j.Slf4j;
//대상 & 테스트 > 패키지 동일해야 함!
@SpringBootTest
@Slf4j
public class MapperTest {
@Autowired
private MyBatisMapper mapper;
@Test
public void testMaper() {
log.info(mapper.time());
}
}
'서버 > SprintBoot' 카테고리의 다른 글
[스프링부트(Spring Boot)] 검색 기능 구현 (0) | 2023.07.27 |
---|---|
[스프링부트(Spring Boot)] 페이징(Paging) (0) | 2023.07.27 |
[스프링부트(Spring Boot)] JPA (0) | 2023.07.02 |
[스프링부트(Spring Boot)] Thymeleaf (0) | 2023.06.27 |
Spring Boot 설치 및 환경 설정 (0) | 2023.06.26 |