각진 세상에 둥근 춤을 추자

[Spring] MyBatis (간단) 회원 정보 입력, 목록, 수정, 삭제 본문

Spring

[Spring] MyBatis (간단) 회원 정보 입력, 목록, 수정, 삭제

circle.j 2023. 1. 6. 01:54

이전 글 참고 

2023.01.05 - [Spring] - [Spring] MyBatis 설정 + DB 연결

 

[Spring] MyBatis 설정 + DB 연결

1. MyBatis MyBatis는 객체와 SQL을 자동 Mapping을 지원하는 ORM 프레임워크 MyBatis는 SQL을 별도의 파일로 분리해서 관리하고, 객체-SQL 사이의 파라미터 Mapping 작업을 자동으로 해주기 때문에 가장 많이

this-circle-jeong.tistory.com

 


 

 

 

1. index 페이지 + MainController

(1) src - main - webapp - WEB-INF - views 폴더 생성 - index.jsp 생성

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>index</title>
	</head>
<body>
	<h3>5장 MyBatis 실습하기</h3>
	<a href="/Ch05/user1/list">user1 목록</a>
</body>
</html>

 

(2) src/main/java - kr.co.ch05.controller 패키지 생성 - MainController.java 생성

package kr.co.ch05.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {
	
	@GetMapping(value= {"/", "/index"})
	public String index() {
		return "/index";
	}
}

 


2. list, register, modify 페이지 

- src - main - webapp - WEB-INF - views - user1 폴더 생성 - list.jsp, register.jsp, modify.jsp 생성

 

[list.jsp]

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>user1::list</title>
	</head>
<body>
	<h3>user1 목록</h3>
	<a href="/Ch05">Ch05 메인</a>
	<a href="/Ch05/user1/register">user1 등록</a>
	
	<table border="1">
		<tr>
			<td>아이디</td>
			<td>이름</td>
			<td>휴대폰</td>
			<td>나이</td>
			<td>관리</td>
		</tr>
		<c:forEach var="user" items="${users}">
			<tr>
				<td>${user.uid}</td>
				<td>${user.name}</td>
				<td>${user.hp}</td>
				<td>${user.age}</td>
				<td>
					<a href="/Ch05/user1/modify?uid=${user.uid}">수정</a>
					<a href="/Ch05/user1/delete?uid=${user.uid}">삭제</a>
				</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

 

[register.jsp]

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>user1::register</title>
	</head>
<body>
	<h3>user1 등록</h3>
	<a href="/Ch05">Ch05 메인</a>
	<a href="/Ch05/user1/list">user1 목록</a>
	
	<form action="/Ch05/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]

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

 


3. Controller, Service, DAO, mapping file 

  • src/main/java - kr.co.ch05.controller - User1Controller.java 생성
  • src/main/java - kr.co.ch05.service 생성 - User1Service 생성
  • src/main/java - kr.co.ch05.dao 생성 - User1DAO 생성
  • src/main/resources - mappers - user1.xml

 

(1) list

 

[User1Controller]

@GetMapping("/user1/list")
      public String list(Model model) {
          List<User1VO> users = service.selectUser1s();
          model.addAttribute("users", users);
          return "/user1/list";
      }

 

[User1Service]

public List<User1VO> selectUser1s() {
          return dao.selectUser1s();
      }

 

[User1DAO]

public List<User1VO> selectUser1s() {
          return mybatis.selectList("user1.selectUser1s");
      }

 

[user1.xml]

<select id="selectUser1s" resultType="kr.co.ch05.vo.User1VO">
          SELECT * FROM `user1`;
      </select>

 

(2) register

 

[User1Controller]

@GetMapping("/user1/register")
    public String register() {
        return "/user1/register";
    }

    @PostMapping("/user1/register")
    public String register(User1VO vo) {
        service.insertUser1(vo);
        return "redirect:/user1/list";
    }

 

[User1Service]

public void insertUser1(User1VO vo) {
        dao.insertUser1(vo);
    }

 

[User1DAO]

public void insertUser1(User1VO vo) {
        mybatis.insert("user1.insertUser1", vo);
    }

 

[user1.xml]

<insert id="insertUser1">
        INSERT INTO `user1` VALUES (#{uid}, #{name}, #{hp}, #{age});
    </insert>

 

(3) modify

 

[User1Controller]

@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";
    }

 

[User1Service]

public User1VO selectUser1(String uid) {
        return dao.selectUser1(uid);
    }
    public void updateUser1(User1VO vo) {
        dao.updateUser1(vo);
    }

 

[User1DAO]

public User1VO selectUser1(String uid) {
        return mybatis.selectOne("user1.selectUser1", uid);
    }
    public void updateUser1(User1VO vo) {
        mybatis.update("user1.updateUser1", vo);
    }

 

[user1.xml]

<select id="selectUser1" resultType="kr.co.ch05.vo.User1VO">
    SELECT * FROM `user1` WHERE `uid`=#{uid};
</select>
<update id="updateUser1">
    UPDATE `user1` set 
        `name`=#{name},
        `hp`=#{hp},
        `age`=#{age}
    WHERE 
        `uid`=#{uid};
</update>

 

(4) delete

 

[User1Controller]

@GetMapping("/user1/delete")
    public String delete(String uid) {
        service.deleteUser1(uid);
        return "redirect:/user1/list";
    }

 

[User1Service]

public void deleteUser1(String uid) {
        dao.deleteUser1(uid);
    }

 

[User1DAO]

public void deleteUser1(String uid) {
        mybatis.delete("user1.deleteUser1", uid);
    }

 

[user1.xml]

<delete id="deleteUser1">
        DELETE FROM `user1` WHERE `uid`=#{uid};
    </delete>

 


4. 전체 코드 

아래 더 보기 버튼 클릭

더보기

(1) [User1Controller]

package kr.co.ch05.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 kr.co.ch05.service.User1Service;
import kr.co.ch05.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";
	}
	
}

 

(2) [User1Service]

package kr.co.ch05.service;

import java.util.List;

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

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


@Service
public class User1Service {
	
	// DAO를 실행
	@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);
	}
}

 

(3) [User1DAO]

package kr.co.ch05.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.ch05.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);
	}
	
	
}

 

(4) [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.ch05.vo.User1VO">
  	SELECT * FROM `user1` WHERE `uid`=#{uid};
  </select>
  <select id="selectUser1s" resultType="kr.co.ch05.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>