본문 바로가기

개발/Backend

[Spring Boot] 예제로 배우는 스프링부트 입문 | JdbcTemplate

JdbcTemplate 기초 

JDBC?

DB에 접근할 수 있도록
즉 DB connection을 지원하는 Java에서 제공되는 API

 

 

 

@Repository, @Controller 차이 

  • 둘다 bean임
  • 그러나 DAO에는 @Repository를 붙여서 데이터베이스로 사용하는것을 표시하기 위한 것 

 

@Autowired

  • spring이 자동으로 db객체를 만들고 jdbcTemplate에 주입 

application.properties

spring.application.name=Ex13_jdbcTemplate

# JSP
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

# MySQL 설정
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/xe
spring.datasource.username=scott
spring.datasource.password=tiger

 

build.gradle

plugins {
	id 'java'
	id 'war'
	id 'org.springframework.boot' version '3.3.1'
	id 'io.spring.dependency-management' version '1.1.5'
}

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

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(17)
	}
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	runtimeOnly 'com.mysql:mysql-connector-j'
	annotationProcessor 'org.projectlombok:lombok'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
	implementation 'jakarta.servlet:jakarta.servlet-api'
	implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api'
	implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl'
	implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
}

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

 

MyUserDAO

package com.study.springboot.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class MyUserDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<MyUserDTO> list() {
        String query = "select * from myuser";
        List<MyUserDTO> list = jdbcTemplate.query(
                query, new BeanPropertyRowMapper<>(MyUserDTO.class));

        //for(UserDTO my : list) {
        //	System.out.println(my);
        //}

        return list;
    }

}

 

MyController.java

package com.study.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.springboot.jdbc.MyUserDAO;

@Controller
public class MyController
{
    @Autowired
    private MyUserDAO userDao;

    @RequestMapping("/")
    public @ResponseBody String root() throws Exception{
        return "JdbcTEmplate 사용하기";
    }

    //@GetMapping("/user")
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String userlistPage(Model model) {
        model.addAttribute("users", userDao.list());
        return "userlist";
    }

}