MyBatis DB연결 세팅 (3) : 영속화를 이용한 update

bethe·2022년 9월 4일
0

Springboot

목록 보기
29/46

update 코드 리팩토링

[문제점]

	<update id="update">
		UPDATE users
		SET username = #{username},
		password = #{password},
		email = #{email}
		WHERE id = #{id}
	</update>

[원리]
만약 패스워드만 받으면 username과 email이 null이 들어오면 다 터지니 username과 email을 안받았으면 실행안하게 설정하거나
업데이트를 여러개 만들어야 한다.
(null이 들어가면 테이블의 값이 사라지므로 터지게 만들어 놓음)

=>select로 기본 테이블에 있는 데이터를 오브젝트로 받는다. 나머지 값이 있는 상태에서 set으로 update할 값만 받는다.

그럼 어떻게 해야 하나로 처리할 수 있을까?

  1. 영속화
@PutMapping("users/{id}")
	public RespDto<?> update(@PathVariable Integer id, UpdateDto updateDto){
		//1번 : id로 select 하자. (영속화 퍼시스턴스)
		//DB에 있는 하나의 row를 java object로 옮기는 것을 영속화라고 함
		Users usersPS = usersDao.findById(id);
		
		//2번 : 변경 (User Entity에 업데이트 책임 수만큼 메서드를 만들어두고 적용)
		//=의미있는 메서드(수행해야 할 업데이트만을 만든다)를 만듬 
		usersPS.전체수정(updateDto);
		
		//3번 : 영속화된 오브젝트로 update하기 (전체 업데이트)
		//3번은 영속화되어 모든 값이 있으므로 전체 업데이트가 가능하다.
		usersDao.update(usersPS);
		
		//통신으로 Users를 받지 않고 메서드에도 set으로 값을 받지 않으므로
		//이 메서드의 경우 Users에 setter가 없어도 됨
		
		return new RespDto<>(1,"회원수정완료",null);
	}

Users

import java.sql.Timestamp;

import lombok.Getter;
import lombok.Setter;
import site.metacoding.red.web.dto.reqest.users.UpdateDto;

@Setter
@Getter
public class Users {
	private Integer id;
	private String username;
	private String password;
	private String email;
	private Timestamp createdAt;
	
	public void 전체수정(UpdateDto updateDto) {
		//영속화가 되어 있으므로 수정할 body값만 받으면 됨
		this.username=updateDto.getUsername();
		this.password=updateDto.getPassword();
		this.email=updateDto.getEmail();
	}

}

패스워드 수정

public void 패스워드수정(String password) {
		this.password=password;
	}
	@PutMapping("users/{id}/password")
	public RespDto<?> updatePassword(@PathVariable Integer id, String password){
		//1번 : 영속화
		Users usersPS = usersDao.findById(id);
		
		//2번 : 변경(Users에서 메서드 만들기)
		usersPS.패스워드수정(password);
		//사실은 이런 1번, 2번 코드가 Service에 있어야 함
		
		//3번 : 영속화된 오브젝트로 update하기 (전체 업데이트)
		usersDao.update(usersPS);

		return new RespDto<>(1,"회원패스워드수정완료",null);
	}

//사실은 이런 1번, 2번 코드가 Service에 있어야 함

profile
코딩을 배우고 기록합니다. 읽는 사람이 이해하기 쉽게 쓰려고 합니다.

0개의 댓글