SpringBoot 에서 MyBatis 설정하기

Honghun Yun·2021년 6월 3일
1

Springboot-Mybatis

목록 보기
1/2
post-thumbnail

SpringBoot 에서도 MyBatis 를 사용해보고 싶어서 여기저기 훑고다니면서 공부한 내용을 정리해보았다. 언젠간 JPA 를 접할 날이 오겠지...

프로젝트 생성


프로젝트 오픈 & 코드 작성

1. application.properties 에 datasource 정보 작성

# datasource 설정 (mariadb, 기본 포트 3306)
# [] 안에 있는 부분을 수정하면 됨
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://[URL]:[PORT]/[DB]?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=[USER_NAME]
spring.datasource.password=[USER_PASSWORD]

# datasource 설정 (portgresql, 기본 포트 5432)
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://[URL]:[PORT]/[DB]
spring.datasource.username=[USER_NAME]
spring.datasource.password=[USER_PASSWORD]

# 기타 다른 DBMS 들도 위 형식과 크게 다르지 않다

2. DTO (Data Transfer Object) Class 생성

  • Getter, Setter 직접 생성이 귀찮으면 Lombok 사용을 권장한다.
package com.example.demo.web;

public class TableDTO {
    Long id;
    String name;

    public Long getId() {
    	return id;
    }
    public void setId(Long id) {
    	this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

3. application.properties 에 DTO 별명 및 mapper XML 위치 작성

# [] 안에 있는 부분을 수정하면 됨

# Mybatis XML 에서 사용할 DTO 별명 설정 (parameterType or resultType 등에서 사용)
# 별명은 클래스의 이름이 된다
mybatis.type-aliases-package=[DTO가 위치한 PACKAGE 이름] 

# 매퍼 위치 설정
# src/main/resources 하위에 있는 경로를 설정해주면 된다.
mybatis.mapper-locations=[mybatis/**/*.xml]

4. mapper 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">

<!-- namespace 에 5번에서 만드는 매퍼 인터페이스의 패키지 경로 넣어줘야 함 -->
<mapper namespace="com.example.demo.web.MyMapper">
	<select id="selectTest" resultType="TableDTO">
	</select>
</mapper>

5. mapper Interface 생성

package com.example.demo.web;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface MyMapper {
    List<TableDTO> selectTable();
}

6. Service Class 생성

package com.example.demo.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    @Autowired
    private MyMapper mapper;
    
    public List<TableDTO> selectTable(){
        return mapper.selectTable();
    }
}

7. Controller Class 생성 및 Service 호출

package com.example.demo.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.RestController;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class MyController {
	@Autowired
	private MyService service;
	
	@GetMapping("/")
	public String index() {
		List<TableDTO> list = service.selectTable();
		return list;
	}
}
profile
SI Developer (since 2019.06.20)

0개의 댓글