본문 바로가기
Back-End/Spring Boot

Spring Boot - 실습 test파일 수정

by 코젼 2022. 7. 27.
728x90
반응형


shop.zip
9.68MB


💁‍♀️ @Param보다 Map이 더 좋은 이유를 잠깐 보고 가겠습니다.

@Param을 사용하면 xml파일에서 값을 하나하나 다 가져와야한다.
ex) id = #{0} id값을 주어야한다.

◾ 그러나 Map을 사용하게 되면 Key, Value값으로 되어있기 때문에 Key값만으로도 접근할 수 있다.
ex) #{item1}, #{item2}, #{item3}

📝test01.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge; chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="/js/jquery.js"></script> -->
</head>
<body>
    <div class="login_wrap">
    <h1>로그인</h1>
    <form class="login_f" method='post' action='/test/test02'>
    <p>
    <label for="user_id">ID</label>
    <input type="text" name="user_id" id="user_id" value="hunter"/>
    </p>
    <p>
    <label for="user_pw">PW</label>
    <input type="password" name="user_pw" id="user_pw" value="12345" />
    </p>
    <p><input type="submit" value="로그인" class="login_btn" /></p>
    </form>
    </div>
	<div>
        User_ID : <span th:text="${result}"></span>
    </div>
</body>
</html>

📝TestController.java

package com.shop.test.controller;

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 com.shop.test.service.TestService;
import com.shop.test.vo.TestVO;

@Controller
@RequestMapping("/test")
public class TestController {
	@Autowired
	TestService service;
	
	@GetMapping("/test01")
	public String test01() {
		return "test/test01";
	}
	
	@PostMapping("/test02")
	public String test02(TestVO vo, Model model) {
		int result = service.memberCount(vo);
//		vo.setId(vo.getUser_id() + " : " + result);
		model.addAttribute("result", result);
		return "test/test01";
	}
}

📝TestService.java

package com.shop.test.service;

import javax.servlet.http.HttpServletRequest;

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

import com.shop.test.dao.ITestDAO;
import com.shop.test.vo.TestVO;

@Service
public class TestService {
	@Autowired
	ITestDAO testDao;
	
	public int memberCount(TestVO formDto) {
		String userId = formDto.getUser_id();
		int result = testDao.memberCount();
		return result;
	}
}

📝TestVO.java

package com.shop.test.vo;

import lombok.Data;

@Data
public class TestVO {
	private String user_id;
	private String user_pw;
	private String id;
}

📝ITestDAO.java

package com.shop.test.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ITestDAO {
	public int memberCount();
}

📝ITestDAO.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.shop.test.dao.ITestDAO">
	<select id="memberCount" resultType="_int">
        select count(*) from member
    </select>
</mapper>
728x90
반응형

댓글