스프링05_mybatis selectList(), selectOne()

charl hi·2022년 1월 18일
0

Spring

목록 보기
6/25

✨selectList()

<mapper namespace="menu">
	<!-- 실행결과의 타입이 menuVo로 -->
	<select id="selectMenuList" resultType="menuVo">
		SELECT * FROM MENU	
	</select>
</mapper> 


package com.kh.app11.menu.model.vo;

public class MenuVo {

	private String menu;
	private int price;
	
	public String getMenu() {
		return menu;
	}
	public void setMenu(String menu) {
		this.menu = menu;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "MenuVo [menu=" + menu + ", price=" + price + "]";
	}
	
}


	@GetMapping("list")
	public String list(Model model) {
//		데이터를 전달해줘야 하므로(req.setAttribute() ... )
		//DB에서 메뉴 리스트를 가져와야 함, 전체를 가져온다면 아래처럼
		List<MenuVo> menuList = sqlSession.selectList("menu.selectMenuList");
		for(MenuVo x : menuList) {
			System.out.println(x);
		}
		//가져온 리스트를 화면에 전달
		//req.setAttribute("list", menuList);
		model.addAttribute("list", menuList);
		
		return "menu/list";
	}


list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>menu list</h1>
	
	<table border="1">
		<c:forEach items="${list}" var="l">
			<tr>
				<td>${l.menu}</td>
				<td>${l.price}</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>
  • menu/list




회원가입

✨update()

MemberController

	@GetMapping("/join")
	public String join() {
		return "member/join";
	}
	
	@PostMapping("/join")
	public String join(MemberVo memberVo) {
		//insert
		sqlSession.update("member.insertMember", memberVo);
		return "redirect:/member/join";
	}


member-mapper.xml

<mapper namespace="member">
	<!-- parameterType="별칭" -->
	<!-- 회원가입 -->
	<insert id="insertMember" parameterType="memberVo">
	INSERT INTO MEMBER
	VALUES(#{id}, #{pwd}, #{nick}, SYSDATE)
	</insert>
</mapper>


mybatis-config.xml

	<settings>
		<setting name="cacheEnabled" value="true"/>
		<setting name="autoMappingBehavior" value="FULL"/>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
<!-- 경로의 별칭 지정 -->
	<typeAliases>
		<typeAlias type="com.kh.app13.member.entity.MemberVo" alias="memberVo"/>
	</typeAliases> 


MemberVo

package com.kh.app13.member.entity;

import java.util.Date;

public class MemberVo {

	private String id;
	private String pwd;
	private String nick;
	private Date enrollDate;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public Date getEnrollDate() {
		return enrollDate;
	}
	public void setEnrollDate(Date enrollDate) {
		this.enrollDate = enrollDate;
	}
	@Override
	public String toString() {
		return "MemberVo [id=" + id + ", pwd=" + pwd + ", nick=" + nick + ", enrollDate=" + enrollDate + "]";
	}
	
}


join.jsp

<body>
	<h1>join page</h1>
	
	<form action="join" method="post">
		id : <input type="text" name="id"><br>
		pwd : <input type="text" name="pwd"><br>
		nick : <input type="text" name="nick"><br>
		<input type="submit" value="가입하기">	
	</form>
</body>




로그인

✨selectOne()

MemberController

	@GetMapping("login")
	public String login() {
		return "member/login";
	}
	
	@PostMapping("login")
	public String login(MemberVo memberVo, HttpSession session) {
		MemberVo loginUser = sqlSession.selectOne("member.selectMemberLogin", memberVo);
		System.out.println("loginUser : "+loginUser);
		//세션에다가 로그인 유저 저장
		if(loginUser != null) {
			//success
			session.setAttribute("loginUser", loginUser);
			return "redirect:/";
		} else {
			//fail
			return "common/errorPage";
		}
	}


member-mapper.xml

	<!-- 로그인 -->
	<!-- 전달받기, 리턴하기 -->
	<!-- 멤버를 조회(아이디&&패스워드 일치) : 아이디, 패스워드 전달받아야 됨 -->
	<!-- 조회결과 돌려줘야됨 : MemberVo -->
	<select id="selectMemberLogin" parameterType="memberVo" resultType="memberVo">
		SELECT *
		FROM MEMBER
		
		<if test="id != null and pwd != null">
			WHERE ID = #{id} AND PWD = #{pwd}	
		</if>
		
	</select>


login.jsp

<body>
	<h1>login page</h1>
	
	<form action="login" method="post">
		id : <input type="text" name="id"><br>
		pwd : <input type="text" name="pwd"><br>
		<input type="submit" value="로그인">	
	</form>
</body>


index.jsp

<body>
	<h1>home page</h1>
<%-- 	<h1>로그인 성공 여부 : ${sessionScope.loginUser != null}</h1> --%>
<%-- 	<h1>로그인 성공 여부 : ${loginUser != null}</h1> --%>
	<h1>로그인 성공 여부 : ${not empty loginUser}</h1>
	
	<h1>${loginUser.nick}님 환영합니다!</h1>
</body>
  • not empty : null 체크 && 빈 문자열 체크

0개의 댓글