스프링20_종합

charl hi·2022년 2월 7일
0

Spring

목록 보기
21/25

  • ✨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>

0개의 댓글