app14
<!-- spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.6.1</version>
</dependency>
<!-- spring security > BCryptPasswordEncoder -->
<beans:bean id="pwdEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
</beans:bean>
<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.app14.member.entity.MemberDto" alias="memberDto"/>
</typeAliases>
</configuration>
<mapper namespace="member">
<insert id="insertMember" parameterType="memberDto">
INSERT INTO MEMBER
VALUES(#{id}, #{pwd}, #{nick}, #{addr}, #{age}, SYSDATE)
</insert>
<select id="login" parameterType="memberDto" resultMap="mDto">
SELECT * FROM MEMBER
WHERE ID = #{id} and PWD = #{pwd}
</select>
<select id="search" parameterType="memberDto" resultMap="mDto">
SELECT * FROM MEMBER
<if test="t != null and v != '' ">
<choose>
<when test="t eq 'age' ">
<!-- ***나이검색*** -->
WHERE AGE IN
<foreach collection="v" item="a" open="(" close=")" separator=",">
<!-- (20,21,22 같은 거) -->
#{a}
</foreach>
</when>
<otherwise>
<!-- ***다른검색*** -->
<bind name="pvp" value=" '%' + v + '%' "/>
WHERE ${t} like #{pvp}
</otherwise>
</choose>
</if>
<!-- <choose>
<when test="t != null and v != '' ">
<bind name="pvp" value=" '%' + v + '%' "/>
WHERE ${t} like #{pvp}
</when>
</choose> -->
<!-- <if test="t != null and v != '' ">
WHERE ${t} = #{v}
</if> -->
</select>
<resultMap type="memberDto" id="mDto">
<result column="ID" property="id"/>
<result column="PWD" property="pwd"/>
<result column="NICK" property="nick"/>
<result column="ADDR" property="addr"/>
<result column="AGE" property="age"/>
<result column="ENROLL_DATE" property="enrollDate"/>
</resultMap>
</mapper>
@Controller
@RequestMapping("member")
public class MemberController {
@Autowired
private SqlSession sqlSession;
@Autowired
private PasswordEncoder passwordEncoder;
//회원가입***********
@GetMapping("join")
public String join() {
return "member/join";
}
@PostMapping("join")
public String join(@ModelAttribute MemberDto m) {
//m 객체를 DB에 insert
//암호화
String newPwd = passwordEncoder.encode(m.getPwd());
System.out.println("회원가입 pwd : "+newPwd);
m.setPwd(newPwd);
sqlSession.insert("member.insertMember", m);
return "redirect:/";
}
//로그인************
@GetMapping("login")
public String login() {
return "member/login";
}
@PostMapping("login")
public String login(String id, String pwd) {
String newPwd = passwordEncoder.encode(pwd);
System.out.println("로그인 pwd : "+newPwd);
Map<String, String> map = new HashMap<>();
map.put("id", id);
map.put("pwd", newPwd);
//DB에서 현재 ID의 PWD 가져오기
//그리고 그 PWD와 화면에서 입력받은 pwd를 비교하기
//비교한 결과값이 로그인 성공 여부
boolean x = passwordEncoder.matches(pwd, newPwd);
System.out.println(x);
MemberDto loginUser = sqlSession.selectOne("member.login", map);
System.out.println("loginUser : "+loginUser);
return "redirect:/";
}
//회원목록************
@GetMapping("list")
public String list(Model model, String searchType, String searchValue) {
Map<String, Object> map = new HashMap<>();
map.put("t", searchType);
map.put("v", searchValue);
if("age".equals(searchType)) {
map.put("v", searchValue.split(","));
}
//모든회원 조회
List<MemberDto> memberList = sqlSession.selectList("member.search", map);
//memberList를 화면에 전달해주기
model.addAttribute("memberList", memberList);
return "member/list";
}
}
package com.kh.app14.member.entity;
import java.util.Date;
public class MemberDto {
private String id;
private String pwd;
private String nick;
private Date enrollDate;
private String addr;
private int age;
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;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "MemberDto [id=" + id + ", pwd=" + pwd + ", nick=" + nick + "]";
}
}
<body>
<h1>회원 목록</h1>
<form action="list" method="get">
<select name="searchType">
<option value="id">아이디</option>
<option value="nick">닉네임</option>
<option value="addr">주소</option>
<option value="age">나이</option>
</select>
<input type="text" name="searchValue">
<input type="submit" value="검색">
</form>
<br><hr><br>
<table border="1">
<thead>
<tr>
<th>아이디</th>
<th>닉네임</th>
<th>주소</th>
<th>나이</th>
<th>가입일</th>
</tr>
</thead>
<tbody>
<%
List<MemberDto> memberList = (List)request.getAttribute("memberList");
for(MemberDto m : memberList){
%>
<tr>
<td><%=m.getId() %></td>
<td><%=m.getNick() %></td>
<td><%=m.getAddr() %></td>
<td><%=m.getAge() %></td>
<td><%=m.getEnrollDate() %></td>
</tr>
<%
}
%>
</tbody>
</table>
</body>