스프링20_종합
- ✨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 void join() throws Exception{
// int x = 1/0;
//
// }
@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>
<!--
<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>
mybatis-config.xml
<typeAliases>
<typeAlias type="com.kh.app25.member.entity.MemberDto" alias="member"/>
</typeAliases>
MemberErrorProcessor
//@ControllerAdvice(basePackages = "com.kh.app25.member.controller")
@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";
}
}
- daoImpl 에서 예외 발생시킴
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>
Author And Source
이 문제에 관하여(스프링20_종합), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@lecharl/스프링20저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)