프로그래밍 일기/Java & Spring

[Spring Boot] 스프링부트 Mysql DB 연동

MakeMe 2023. 7. 5. 13:46
반응형

버전 정보

Java : 11

Mysql : 5.7.41.0 ( 8 이후 버전의 경우도 하단에 설명드리겠습니다. )

Spring Boot : 2.7.13


build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.13'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '11'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.3.1'
    implementation 'mysql:mysql-connector-java:5.1.30'
}

tasks.named('test') {
    useJUnitPlatform()
}

mysql 8버전 이후인 경우 

implementation 'mysql:mysql-connector-java:5.1.30' 해당 값을

ex) implementation 'mysql:mysql-connector-java:8.0.22' 로 수정

application.properties
# 자신에게 필요한 버전을 사용하시면 됩니다.
# 5.x 버전
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=/mapper/**/*.xml

# 8버전 이후
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=/mapper/**/*.xml

spring.datasource.url=jdbc:mysql://[host]:[port]/[db명] 

host, port, db명을 정확하게 입력하고

mybatis.mapper-locations=/mapper/**/*.xml

위 경로 설정 부분 또한 정확하게 맞춰주어야 합니다.

 

TestController
@RestController
public class TestController {

    private final TestService testService;

    public TestController(TestService testService) {
        this.testService = testService;
    }

    @GetMapping("/list/load")
    public Map loadList(@RequestParam Map params) throws Exception {
        return testService.loadList(params);
    }

}

 

TestService
package com.example.testapi.api.test;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TestService {

    private final TestMapper testMapper;

    public TestService(TestMapper testMapper) {
        this.testMapper = testMapper;
    }

    public Map loadList(Map params) throws Exception {
        Map result = new HashMap();

        result.put("code", 200);
        result.put("data", testMapper.loadList(params));
        result.put("message", "success");

        return result;
    }

}

 

TestMapper
package com.example.testapi.api.test;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

@Mapper
public interface TestMapper {

    List<Map> loadList(Map parmas) throws Exception;

}

 

TestMapper.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.example.testapi.api.test.TestMapper">

    <select id="loadList" resultType="Map">
        SELECT COLUMN1
             , COLUMN2
             , COLUMN3
          FROM TEST
    </select>

</mapper>

 

결과

 

반응형