✨selectList()
<mapper namespace="menu">
<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) {
List<MenuVo> menuList = sqlSession.selectList("menu.selectMenuList");
for(MenuVo x : menuList) {
System.out.println(x);
}
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>
회원가입
✨update()
MemberController
@GetMapping("/join")
public String join() {
return "member/join";
}
@PostMapping("/join")
public String join(MemberVo memberVo) {
sqlSession.update("member.insertMember", memberVo);
return "redirect:/member/join";
}
member-mapper.xml
<mapper namespace="member">
<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) {
session.setAttribute("loginUser", loginUser);
return "redirect:/";
} else {
return "common/errorPage";
}
}
member-mapper.xml
<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 체크 && 빈 문자열 체크