Spring Boot와 MySQL을 이용한 간단한 데이터 조회 구현
프로젝트 아키텍처 구성
1. 컨트롤러 계층 (Controller)
package com.example.controller;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/home")
public String showHome() {
return "home";
}
@GetMapping("/login")
public String showLogin() {
return "login";
}
@PostMapping("/authenticate")
public String authenticateUser(@RequestParam("studentId") String studentId,
@RequestParam("password") String password,
Model model,
HttpSession session) {
User userData = userService.findByStudentId(studentId);
if (userData == null || !userData.getPassword().equals(password)) {
model.addAttribute("error", "아이디 또는 비밀번호가 올바르지 않습니다.");
return "login";
}
session.setAttribute("currentUser", userData);
return "redirect:/home";
}
}
2. 매퍼 계층 (Mapper)
package com.example.mapper;
import com.example.model.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User findByStudentId(String studentId);
}
3. 도메인 모델 (POJO)
package com.example.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int userId;
private String studentId;
private String name;
private String password;
private String email;
private String phone;
private int followerCount;
private int videoCount;
private String avatarUrl;
private int blogCount;
private int likeCount;
private int status;
private String registrationDate;
}
4. 서비스 계층 (Service)
package com.example.service;
import com.example.model.User;
public interface UserService {
User findByStudentId(String studentId);
}
4.1 서비스 구현체
package com.example.service.impl;
import com.example.mapper.UserMapper;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByStudentId(String studentId) {
return userMapper.findByStudentId(studentId);
}
}
5. MyBatis XML 매핑 파일
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.example.mapper.UserMapper">
<select id="findByStudentId" resultType="com.example.model.User">
SELECT
user_id AS userId,
student_id AS studentId,
name,
password,
email,
phone,
follower_count AS followerCount,
video_count AS videoCount,
avatar_url AS avatarUrl,
blog_count AS blogCount,
like_count AS likeCount,
status,
registration_date AS registrationDate
FROM users
WHERE student_id = #{studentId}
</select>
</mapper>
6. 설정 파일 (application.yml)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/university_db?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: admin123
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mvc:
static-path-pattern: /static/**
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.model
7. Maven 의존성 관리 (pom.xml)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
</parent>
<groupId>com.example</groupId>
<artifactId>user-management</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
태그:
Spring Boot
MySQL
MyBatis
Druid
Lombok
6월 9일 19:05에 게시됨