@Controller
public class HomeController {
//홈화면 보여주기
@RequestMapping("/")
public String home() {
return "home";
}
}
http://127.0.0.1:8888/app15/
<body>
<h1>홈 화면</h1>
<% if(session.getAttribute("loginUser") != null){ %>
<h1>${loginUser.id}님 환영합니다!</h1>
<h1>${loginUser.nick}님 환영합니다!</h1>
<h1>나이 : ${loginUser.age}</h1>
<a href="member/logout">로그아웃</a>
<% }else{ %>
로그인 해주세요!
<a href="member/login">로그인페이지</a>
<% } %>
</body>
@Controller
@RequestMapping("member")
public class MemberController {
@Autowired
private SqlSession sqlSession;
@Autowired
private PasswordEncoder pe;
// ***회원가입***
@GetMapping("join")
public String join() {
System.out.println("join~~");
return "member/join";
}
@PostMapping("join")
public String join(MemberDto m) {
//암호화
String newPwd = pe.encode(m.getPwd());
m.setPwd(newPwd);
//insert
int result = sqlSession.insert("member.join", m);
//view
if(result > 0) {
return "redirect:/";
} else {
return "redirect:/member/join";
}
}
// ***로그인***
@GetMapping("login")
public String login() {
return "member/login";
}
@PostMapping("login")
public String login(MemberDto m, HttpSession session) {
//아이디 갖고 디비가서 비번 가져오기
MemberDto loginUser = sqlSession.selectOne("member.login", m);
if(loginUser == null) {
return "redirect:/member/login";
}
String dbPwd = loginUser.getPwd();
//가져온 비번과, 사용자입력 비번이랑 일치하는지 확인
String userPwd = m.getPwd();
if(pe.matches(userPwd, dbPwd)) {
//success
//session에 담기
session.setAttribute("loginUser", loginUser);
return "redirect:/";
} else {
//fail
return "redirect:/member/login";
}
}
// ***로그아웃***
@GetMapping("logout")
public String logout(HttpSession session) {
session.invalidate();
return "redirect:/";
}
}
-> ✨✨상위인 PasswordEncoder로 한다!
<mapper namespace="member">
<!-- select 가 아닌 dml(insert, delete, update)은 resultType==int 이다!! -->
<insert id="join" 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}
</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>
-> ✨✨ @Component
@AutoWired
로 MemberService를 부르자-> ✨✨ @AutoWired private MemberService memberService
-> memberService로 다루자
@Component
public class MemberService implements MemberServiceInterface{
@Autowired
private SqlSession sqlSession;
@Autowired
private PasswordEncoder pe;
@Autowired
private MemberRepositoryInterface memberRepositoryInterface;
public int join(MemberDto m) {
//암호화
String newPwd = pe.encode(m.getPwd());
m.setPwd(newPwd);
//DB에 insert
return memberRepositoryInterface.join(m);
}
}
@Component
public class MemberServiceImpl implements MemberServiceInterface{
@Autowired
private MemberRepositoryInterface memberRepositoryInterface;
@Autowired
private PasswordEncoder pe;
@Override
public int join(MemberDto m) {
//암호화
// String newPwd = pe.encode(m.getPwd());
// m.setPwd(newPwd);
//암호화 사용하지 않고 그냥 insert
//DB에 insert
return memberRepositoryInterface.join(m);
}
}
public interface MemberServiceInterface {
public int join(MemberDto m);
}
@Component
public class MemberRepository implements MemberRepositoryInterface{
@Autowired
private SqlSession sqlSession;
//회원가입
public int join(MemberDto m) {
return sqlSession.insert("member.join", m);
}
}
public interface MemberRepositoryInterface {
public int join(MemberDto m);
}
@Controller
@RequestMapping("member")
public class MemberController {
@Autowired
private SqlSession sqlSession;
@Autowired
private PasswordEncoder pe;
@Autowired
private MemberServiceInterface memberServiceInterface;
// ***회원가입***
@GetMapping("join")
public String join() {
System.out.println("join~~");
return "member/join";
}
@PostMapping("join")
public String join(MemberDto m) {
// ***모듈화 시작!
//1. 값을 받기 : 파라미터(MemberDto)로 받음
//2. 나머지 작업(암호화, db에 insert)은 서비스 레이어한테 맡기기
int result = memberServiceInterface.join(m);
//3. 화면 선택
//view
if(result > 0) {
return "redirect:/";
} else {
return "redirect:/member/join";
}
}
// ***로그인***
@GetMapping("login")
public String login() {
return "member/login";
}
@PostMapping("login")
public String login(MemberDto m, HttpSession session) {
//아이디 갖고 디비가서 비번 가져오기
MemberDto loginUser = sqlSession.selectOne("member.login", m);
if(loginUser == null) {
return "redirect:/member/login";
}
String dbPwd = loginUser.getPwd();
//가져온 비번과, 사용자입력 비번이랑 일치하는지 확인
String userPwd = m.getPwd();
if(pe.matches(userPwd, dbPwd)) {
//success
//session에 담기
session.setAttribute("loginUser", loginUser);
return "redirect:/";
} else {
//fail
return "redirect:/member/login";
}
}
// ***로그아웃***
@GetMapping("logout")
public String logout(HttpSession session) {
session.invalidate();
return "redirect:/";
}
}