스프링06_mybatis_검색

charl hi·2022년 1월 19일
0

Spring

목록 보기
7/25

세션 vs. 모델

model

  • request에 종속

session

  • 서버에 있는 저장공간



회원 검색

✨selectList()

MemberController

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


SearchVo

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


mybatis-comfig.xml

<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>


✨member-mapper.xml

	<!--  회원조회 -->
	<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 사용

		<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>

foreach 사용

  • 그냥 배열일 땐

  • separator : 구분자, 배열 요소 하나하나에 들어감
  • open : 시작
  • close : 끝

-> 예: (apple, orange, grape)

이런 식으로 할 때!



search.jsp

<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>



0개의 댓글