- ✨dao에서 나오는 모든 예외는 service로, service에서 나오는 모든 예외는 controller로 던진다.
-> 모든 예외는 controller로 모이게
MemberController
@Controller
@RequestMapping("member")
public class MemberController {
@Autowired
private MemberService service;
@GetMapping("join")
public String join() throws Exception {
return "member/join";
}
@PostMapping("join")
public String join(MemberDto dto) throws Exception {
int result = service.join(dto);
return "member/join";
}
}
MemberService
public interface MemberService {
int join(MemberDto dto) throws Exception;
}
MemberServiceImpl
@Service
public class MemberServiceImpl implements MemberService{
@Autowired
private MemberDao dao;
@Override
public int join(MemberDto dto) throws Exception {
return dao.join(dto);
}
}
MemberDao
public interface MemberDao {
int join(MemberDto dto) throws Exception;
}
MemberDaoImpl
@Repository
public class MemberDaoImpl implements MemberDao{
@Autowired
private SqlSession session;
@Override
public int join(MemberDto dto) throws Exception {
return session.insert("member.join", dto);
}
}
member-mapper.xml
<mapper namespace="member">
<insert id="join" parameterType="member">
INSERT INTO MEMBER
(ID, PWD, NICK)
VALUES(#{id}, #{pwd}, #{nick})
</insert>
</mapper>
mybatis-config.xml
<typeAliases>
<typeAlias type="com.kh.app25.member.entity.MemberDto" alias="member"/>
</typeAliases>
MemberErrorProcessor
@ControllerAdvice(annotations = Controller.class)
@Slf4j
public class MemberErrorProcessor {
@ExceptionHandler(Exception.class)
public String exceptionProcess(Exception e) {
log.error("컨트롤러 어딘가에서 예외 발생함\n {}\n", e.getMessage());
return "common/error/exception";
}
}
MemberDto
@Data
public class MemberDto {
private String id;
private String pwd;
private String nick;
}
join.jsp
<body>
<h1>회원가입 페이지</h1>
<form action="" method="post">
아이디 : <input type="text" name="id" placeholder="아이디 입력"><br>
비밀번호 : <input type="text" name="pwd" placeholder="비밀번호 입력"><br>
닉네임 : <input type="text" name="nick" placeholder="닉네임 입력"><br>
<input type="submit" value="가입하기">
</form>
</body>