각진 세상에 둥근 춤을 추자

[Spring Boot] 스프링부트 DB 연동 + Lombok 설치 +간단 회원 정보 입력, 목록, 수정 본문

Spring

[Spring Boot] 스프링부트 DB 연동 + Lombok 설치 +간단 회원 정보 입력, 목록, 수정

circle.j 2023. 1. 10. 12:03

스프링부트와 DB 연동하기 

 

Thymeleaf Plugin for Eclipse 3.0.1 설치

[Help] - [Eclipse MarketPlace]

 

Lombok 설치하기

https://projectlombok.org/download

 

Download

 

projectlombok.org

 

Specify location 클릭

 

SpringToolSuite4.app - Contents - Eclipse - SpringToolSuite4.ini 클릭

 

Install/Update 클릭 - Quit Installer 클릭 

 

Eclipse EnterPrise Java and Web Developer Tools 3.28 설치

[Help] - [Eclipse MarketPlace]

 


1. 프로젝트 생성하기 

New - Spring Starter Project

 

  • Name: 프로젝트명
  • Type: Gradle - Groovy
  • Packaging: Jar
  • Java Version: 11
  • Language: Java
  • Group, Package: kr.co.패키지명

 

  • Spring Boot Version: 2.7.7
  • Selected: Lombok, MyBatis FrameWork, MySQL Driver, Spring Boot DevTools, Spring Web, Thymeleaf

 

 


2. 인코딩 설정

 

# JSP 이클립스 인코딩 설정

다국어를 지원하는 프로그램 개발을 위해서는 유니코드 기반인 UTF-8 의 사용이 권장됨.
안드로이드 등 모바일 개발에도 유니코드가 기본임.

텍스트 인코딩 설정
➊ [General] → [Workspace] → Text file encoding 항목을 Other 로 변경한 뒤 UTF-8로 설정

자바 클래스 인코딩 설정
➋ [General] → [Content Types] → Java Class File에 대한 Default encoding 값을 UTF-8 로 입력 후 <Update> 버튼

JSP 파일 인코딩 설정
➌ [General]→[Content Types] → [Text] → JSP 항목을  UTF-8 로 변경 후 <Update> 버튼

HTML / JSP 코드 인코딩 설정
➍ [Web] → [HTML Files], [JSP Files] 항목을 ISO 10646/Unicode(UTF-8)로 변경

 


3. application.properties

프로젝트 - src/main/resources - application.properties

 

server.servlet.context-path=/User
server.port=8080
spring.thymeleaf.cache=false

# Mybatis 설정 (DB 연동)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/java2db
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#MyBatis Mapper 경로설정 -> Ch06Application 클래스 상단에 @MapperScan("kr.co.ch06.persistence") 추가
mybatis.mapper-locations=classpath:mappers/**/*.xml

 

 


3. UserApplication.java

프로젝트 - src/main/java - kr.co.user (프로젝트 생성 시 패키지명) - UserApplication.java (프로젝트명+Application.java)

 

package kr.co.user;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("kr.co.user.dao")
@SpringBootApplication
public class UserApplication {

	public static void main(String[] args) {
		SpringApplication.run(UserApplication.class, args);
	}

}

 

 


4. 정적 페이지 생성

프로젝트 - src/main/resources - templates.user (폴더) 생성 - list.html, register.html, modify.html 생성

<html xmlns:th="http://www.thymeleaf.org">

 

 

[list.html]

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>user1::list</title>
	</head>
<body>
	<h3>user1 목록</h3>
	<a th:href="@{/}">User 메인</a>
	<a th:href="@{/user1/register}">user1 등록</a>
	
	<table border="1">
		<tr>
			<td>아이디</td>
			<td>이름</td>
			<td>휴대폰</td>
			<td>나이</td>
			<td>관리</td>
		</tr>
		<tr th:each="user:${users}">
			<td>[[${user.uid}]]</td>
			<td>[[${user.name}]]</td>
			<td>[[${user.hp}]]</td>
			<td>[[${user.age}]]</td>
			<td>
				<a th:href="@{/user1/modify(uid = ${user.uid})}">수정</a>
				<a th:href="@{/user1/delete(uid = ${user.uid})}">삭제</a>
			</td>
		</tr>
	</table>
</body>
</html>

 

[register.html]

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>user1::register</title>
	</head>
<body>
	<h3>user1 등록</h3>
	<a th:href="@{/}">User 메인</a>
	<a th:href="@{/user1/list}">user1 목록</a>
	
	<form action="/User/user1/register" method="post">
		<table border="1">
			<tr>
				<td>아이디</td>
				<td><input type="text" name="uid"></td>
			</tr>
			<tr>
				<td>이름</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>휴대폰</td>
				<td><input type="text" name="hp"></td>
			</tr>
			<tr>
				<td>나이</td>
				<td><input type="number" name="age"></td>
			</tr>
			<tr>
				<td colspan="2" align="right"><input type="submit" value="등록"></td>
				
			</tr>
		</table>	
	</form>
	
</body>
</html>

 

[modify.html]

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>user1::modify</title>
	</head>
<body>
	<h3>user1 수정</h3>
	<a th:href="@{/}">User 메인</a>
	<a th:href="@{/user1/list}">user1 목록</a>
	
	<form action="/User/user1/modify" method="post">
		<table border="1">
			<tr>
				<td>아이디</td>
				<td><input type="text" name="uid" th:value="${user.uid}" readonly="readonly"></td>
			</tr>
			<tr>
				<td>이름</td>
				<td><input type="text" name="name" th:value="${user.name}" ></td>
			</tr>
			<tr>
				<td>휴대폰</td>
				<td><input type="text" name="hp" th:value="${user.hp}" ></td>
			</tr>
			<tr>
				<td>나이</td>
				<td><input type="number" name="age" th:value="${user.age}" ></td>
			</tr>
			<tr>
				<td colspan="2" align="right"><input type="submit" value="수정"></td>
				
			</tr>
		</table>	
	</form>
	
</body>
</html>

 

 


5. mapper

프로젝트 - src/main/resources - mappers 폴더 생성 - user1.xml 생성

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="user1">
	<insert id="insertUser1">
		INSERT INTO `user1` VALUES (#{uid}, #{name}, #{hp}, #{age});
	</insert>
	<select id="selectUser1" resultType="kr.co.user.vo.User1VO">
		select * from `user1` where `uid`=#{uid};
	</select>
	<select id="selectUser1s" resultType="kr.co.user.vo.User1VO">
		select * from `user1`;
	</select>
	<update id="updateUser1">
		update `user1` set
			`name`=#{name},
			`hp`=#{hp},
			`age`=#{age}
		where
			`uid`=#{uid};		
	</update>
	<delete id="deleteUser1">
		delete from `user1` where `uid`=#{uid};
	</delete>
</mapper>

 

 


6. VO

프로젝트 - src/main/java - kr.co.user - vo 패키지 생성 - User1VO.java 생성

 

package kr.co.user.vo;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User1VO {
	private String uid;
	private String name;
	private String hp;
	private int age;
}

 

 


7. DAO

프로젝트 - src/main/java - kr.co.user - dao 패키지 생성 - User1DAO.java 생성

 

package kr.co.user.dao;

import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import kr.co.user.vo.User1VO;

@Repository
public class User1DAO {

	@Autowired
	private SqlSessionTemplate mybatis;
	
	public void insertUser1(User1VO vo) {
		mybatis.insert("user1.insertUser1", vo);
	}
	public User1VO selectUser1(String uid) {
		return mybatis.selectOne("user1.selectUser1", uid);
	}
	public List<User1VO> selectUser1s() {
		return mybatis.selectList("user1.selectUser1s");
	}
	public void updateUser1(User1VO vo) {
		mybatis.update("user1.updateUser1", vo);
	}
	public void deleteUser1(String uid) {
		mybatis.delete("user1.deleteUser1", uid);
	}
	
}

 

 


8. Service

프로젝트 - src/main/java - kr.co.user - service 패키지 생성 - User1Service.java 생성

 

package kr.co.user.service;

import java.util.List;

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

import kr.co.user.dao.User1DAO;
import kr.co.user.vo.User1VO;


@Service
public class User1Service {
	@Autowired
	private User1DAO dao;

	public void insertUser1(User1VO vo) {
		dao.insertUser1(vo);
	}
	public User1VO selectUser1(String uid) {
		return dao.selectUser1(uid);
	}
	public List<User1VO> selectUser1s() {
		return dao.selectUser1s();
	}
	public void updateUser1(User1VO vo) {
		dao.updateUser1(vo);
	}
	public void deleteUser1(String uid) {
		dao.deleteUser1(uid);
	}
}

 

 


9. controller

프로젝트 - src/main/java - kr.co.user - controller 패키지 생성 - User1Controller.java 생성

 

package kr.co.user.controller;

import java.util.List;

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.RequestMapping;

import kr.co.user.service.User1Service;
import kr.co.user.vo.User1VO;


@Controller
public class User1Controller {

	@Autowired
	private User1Service service;
	
	@GetMapping("/user1/list")
	public String list(Model model) {
		List<User1VO> users = service.selectUser1s();
		model.addAttribute("users", users);
		return "/user1/list";
	}
	
	@GetMapping("/user1/register")
	public String register() {
		return "/user1/register";
	}
	
	@PostMapping("/user1/register")
	public String register(User1VO vo) {
		service.insertUser1(vo);
		return "redirect:/user1/list";
	}
	
	@GetMapping("/user1/modify")
	public String modify(String uid, Model model) {
		User1VO user = service.selectUser1(uid);
		model.addAttribute("user", user);
		return "/user1/modify";
	}
	@PostMapping("/user1/modify")
	public String modify(User1VO vo) {
		service.updateUser1(vo);
		return "redirect:/user1/list";
	}
	
	@GetMapping("/user1/delete")
	public String delete(String uid) {
		service.deleteUser1(uid);
		return "redirect:/user1/list";
	}
	
}

 

 


10. 실행하기

프로젝트 마우스 우클릭 - Run As - Spring Boot App