@GetMapping("search")
public String search(Model model) {
//모든 회원 정보 보여주기
List<MemberVo> memberList = sqlSession.selectList("member.selectMemberSearch");
model.addAttribute("list", memberList);
return "member/search";
}
@PostMapping("search")
public String search(SearchVo svo, Model model) {
List<MemberVo> memberList = sqlSession.selectList("member.selectMemberSearch", svo);
model.addAttribute("list", memberList);
return "member/search";
}
package com.kh.app13.member.entity;
public class SearchVo {
private String searchType;
private String searchValue;
public String getSearchType() {
return searchType;
}
public void setSearchType(String searchType) {
this.searchType = searchType;
}
public String getSearchValue() {
return searchValue;
}
public void setSearchValue(String searchValue) {
this.searchValue = searchValue;
}
}
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="autoMappingBehavior" value="FULL"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
<!-- 경로의 별칭 지정 -->
<typeAliases>
<typeAlias type="com.kh.app13.member.entity.MemberVo" alias="memberVo"/>
<typeAlias type="com.kh.app13.member.entity.SearchVo" alias="searchVo"/>
</typeAliases>
</configuration>
<!-- 회원조회 -->
<select id="selectMemberSearch" parameterType="searchVo" resultType="memberVo">
SELECT * FROM MEMBER
<if test="searchType != null and searchValue != null">
<bind name="vvv" value=" '%' + searchValue + '%' "/>
WHERE ${searchType} like #{vvv}
</if>
</select>
<!-- 매번 별칭짓기 귀찮아서.. 왼쪽 디비컬럼이름이 오른쪽으로 변한다-->
<resultMap type="memberVo" id="abc">
<result column="ID" property="id"/>
<result column="PWD" property="pwd"/>
<result column="NICK" property="nick"/>
<result column="ENROLL_DATE" property="enrollDate"/>
</resultMap>
#
: 옆에 '' 알아서 붙이기 또는 안붙이기 등 동적으로 처리함
-> 예: #{searchValue}
-> 'aa'
$
: 내가 입력한 그대로
<choose>
<when test="searchType == 'id'">
WHERE ID = #{searchValue}
</when>
<when test="searchType eq 'nick'">
WHERE NICK = #{searchValue}
</when>
<otherwise>
WHERE ID = 'XXX' AND NICK = 'XXX'
</otherwise>
</choose>
separator
: 구분자, 배열 요소 하나하나에 들어감open
: 시작close
: 끝-> 예: (apple, orange, grape)
이런 식으로 할 때!
<body>
<h1>회원 검색 페이지</h1>
<form action="search" method="post">
<!-- 아이디, 닉네임 검색 -->
<select name="searchType">
<option value="id">아이디</option>
<option value="nick">닉네임</option>
<option value="zz">??</option>
</select>
<input type="text" name="searchValue">
<input type="submit" value="검색">
</form>
<table border="1">
<tr>
<td>아이디</td>
<td>닉네임</td>
<td>가입일</td>
</tr>
<c:forEach items="${list}" var="mvo">
<tr>
<td>${mvo.id}</td>
<td>${mvo.nick}</td>
<td>${mvo.enrollDate}</td>
</tr>
</c:forEach>
</table>
</body>