본문 바로가기
개발/Spring Boot

2. MySql 연결

by 카리3 2021. 2. 6.

MySql 연결

1. pom.xml 설정

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.1.4</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.22</version>
  <!-- <scope>runtime</scope> -->
</dependency>

2. application.properties 설정

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/SECKIMDB?useSSL=false&serverTimezone=UTC
spring.datasource.username=SECKIM
spring.datasource.password=1234
mybatis.mapper-locations=classpath:mapper/mapping*.xml

3. /resorces/mapper/mapping-query-test.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="kangong.test">

    <select id="selectNow" resultType="String">
        SELECT NOW()
    </select>
</mapper>

4. SampleService 설정

package com.kangong.sample.service;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SampleService {

	@Autowired
	private SqlSession sqlSession;
	
	public String gettNow() {
        return sqlSession.selectOne("kangong.test.selectNow");
    }
}

5. SampleController 설정

package com.kangong.sample.controller;

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

import com.kangong.sample.service.SampleService;

@RestController
public class SampleController {
	
	@Autowired
	SampleService sampleService;
	
	@GetMapping("/hello")
	public String[] hello() {
		
		
		System.out.println(sampleService.gettNow());
		return new String[] {"Hello","World",sampleService.gettNow()};
	}

}

6. http://localhost:8080/hello 호출

 

'개발 > Spring Boot' 카테고리의 다른 글

6.thymeleaf 설정  (0) 2021.02.11
5. Lombok 설정  (0) 2021.02.11
4. log4j2 설정  (0) 2021.02.07
3. jsp 연동  (0) 2021.02.07
1.Spring Boot 환경 셋팅  (0) 2021.02.06