<form class="form-inline" action="/action_page.php">
<label for="username">User ID</label>
<input type="text" class="form-control" placeholder="Enter username" name="username" id="username">
<label for="password">Password:</label>
<input type="password" class="form-control" placeholder="Enter password" name="password" id="password">
<!-- 삭제해도 무관 -->
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox"> Remember me
</label>
</div>
<!-- 삭제해도 무관 -->
<button type="submit" class="btn btn-primary">로그인</button>
</form>
넥스트 > 비번 > 넥스트 > ai > 넥스트> 넥스트 > 익스큐드 > 넥스트 > 피니쉬
이전에 만들어 진거라 표기된 것임.
테이블 데이터 내보내기
넥스트 > 비번 입력 >
비번 입력 > 클로즈
package org.example.entity;
import lombok.Data;
@Data
public class Member {
private String username;
private String password;
private String name;
private String email;
}
package org.example.controller;
import org.example.entity.Member;
import org.example.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@Autowired
private LoginService service;
@PostMapping("/login")
public String login(Member member, HttpSession session){
Member dbmember = service.login(member);
// null 이면 실패! / not null 이면 성공!
if(dbmember != null){ //성공시
session.setAttribute("member", dbmember);
}
// 다시 시작페이지로 전환
return "redirect:/list";
}
}
(해당 교육에서는 impl은 안 배움)
package org.example.service;
import org.example.entity.Member;
import org.example.repersitoty.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class LoginService {
@Autowired
private MemberMapper mapper;
@Transactional
public Member login(Member member){
// 새로운 비즈니스 로직처리... -> commit, rollback, 동기화처리
return mapper.login(member);
}
}
package org.example.repersitoty;
import org.example.entity.Board;
import org.example.entity.Member;
import java.util.List;
public interface MemberMapper { // public class SqlSessionFactory implements MemberMapper
// SqlSessionFactory
public Member login(Member member);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.repersitoty.MemberMapper">
<select id="login" resultType="org.example.entity.Member">
SELECT *
FROM MEMBER
WHERE USERNAME = #{username}
AND PASsWORD = #{password}
</select>
</mapper>
<!-- 로그인이 필요한 화면 -->
<c:if test="${empty member}">
<form class="form-inline" action="${cpath}/login" method="post">
<label for="username">User ID</label>
<input type="text" class="form-control" placeholder="Enter username" name="username" id="username">
<label for="password">Password:</label>
<input type="password" class="form-control" placeholder="Enter password" name="password" id="password">
<button type="submit" class="btn btn-primary">로그인</button>
</form>
</c:if>
<!-- 로그인이 완료된 화면 -->
<c:if test="${!empty member}">
<form class="form-inline" action="${cpath}/logout" method="get">
<label>${member.username}(${member.name})</label>
<button type="submit" class="btn btn-primary">로그아웃</button>
</form>
</c:if>
@GetMapping("/logout")
public String logout(HttpSession session){
// 세션을 무효화 시킴
// -> 로그아웃 방법은 여러가지
// (강제[invalidate()], 세션 타임아웃(1800s), 브라우저 종료, 서버 종료)
session.invalidate();
return "redirect:/list"; // 로그아웃된 화면으로
}