[Spring] 스프링 프로젝트 간단 DB조회 로직 (백엔드 ver)

류넹·2025년 5월 16일
1

Project

목록 보기
12/14

* Eclipse Spring 프로젝트 생성 및 초기 개발환경 구축하는 방법 참고


# 개발환경

항목내용
LanguageJava 11
Backend IDEEclipse 2023-12
Spring5.3.36
Tomcat9.0.105
DataBaseMySQL 8.0.33 (MyBatis 3.5.15)
Build ToolMaven 3.8.x (설정파일 : pom.xml)
Frontend IDEVSCode
Vue.js3.16.4


📌 디렉토리 구조





📌 간단한 DB 조회 전체 코드

0. SQL

  • MySQL 워크벤치 등에서 쿼리 실행
CREATE DATABASE vue_calendar CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE vue_calendar;


DROP TABLE schedule;

CREATE TABLE schedule (
	id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL
);

INSERT INTO schedule
	(title)
VALUES
	('제목1');

        select
            id          as id,
            title       as title
        from
        	schedule;

1. Controller

package com.calendarapp.controller;

import java.util.List;

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

import com.calendarapp.service.ScheduleService;
import com.calendarapp.vo.Schedule;

@RestController
@RequestMapping("/api/schedules")
public class ScheduleController {
	
	@Autowired
	private ScheduleService scheduleService;
	
	@GetMapping
	public List<Schedule> getSchedules() {
		
		return scheduleService.getSchedules();
	}
}

2. Service

package com.calendarapp.service;

import java.util.List;

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

import com.calendarapp.mapper.ScheduleMapper;
import com.calendarapp.vo.Schedule;

@Service
public class ScheduleService {
	
	@Autowired
	private ScheduleMapper scheduleMapper;
	
	public List<Schedule> getSchedules() {
		
		return scheduleMapper.getSchedules();
	}
}

3. Mapper

package com.calendarapp.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.calendarapp.vo.Schedule;

@Mapper
public interface ScheduleMapper {
	
	List<Schedule> getSchedules();
}

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">
<mapper namespace="com.calendarapp.mapper.ScheduleMapper">

	<select id="getSchedules" resultType="com.calendarapp.vo.Schedule">
        select
            id          as id,
            title       as title
        from
        	schedule
	</select>
</mapper>

5. vo

package com.calendarapp.vo;

public class Schedule {

	private int id;
	private String title;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}


💡 실행화면


* Spring + Vue.js 연동까지 구현해보기 :
https://velog.io/@ryuneng2/SpringVue.js-스프링-Vue.js-연동-및-DB-조회-간단-구현

profile
학습용 커스터마이징 간단 개발자 사전

0개의 댓글