본문 바로가기

스프링부트(Spring Boot)

Member 생성 및 CRUD 작업 (mmsapi 프로젝트)

1. MemberEntity, MemberDTO, MemberController, MemberService 클래스 MemberRepository 인터페이스 생성

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "member")
public class MemberEntity {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@Column(nullable = false, unique = true)
	private String username;
	
	@Column(nullable = false)
	private String nickName;
	
	@Column(nullable = false)
	private String password;
	
	private Date createDate;
	
	private Date updateDate;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MemberDTO {
	private long id;
	private String username;
	private String nickName;
	private String password;
	private String password2;
	private String orgPassword;
	private Date createDate;
	private Date updateDate;
}
@RestController
@RequestMapping("/member")
public class MemberController {
	@Autowired
	private MemberService memberService;
}
@Service
public class MemberService {
	@Autowired
	private MemberRespository memberRespository;
}
public interface MemberRespository extends JpaRepository<MemberEntity, Long> {

}

 

2. Create 작업 (POST)

<Controller>	
	@PostMapping("")
	public ResponseEntity<?> save(@RequestBody MemberDTO memberDTO){
		Map<String, Object> map = new HashMap<>();
        
		if(!(memberDTO==null || memberDTO.getId()!=0 
				|| memberDTO.getPassword()==null || memberDTO.getPassword2()==null
				|| !(memberDTO.getPassword().equals(memberDTO.getPassword2())))) {
			try {
				memberService.save(memberDTO);
				map.put("result", "입력 성공");
				return ResponseEntity.ok().body(map);
			} catch (Exception e) {
				e.printStackTrace();
				map.put("err", "입력 실패");
				return ResponseEntity.badRequest().body(map);
			}
		}else {
			return ResponseEntity.badRequest().body("에러 발생");
		}
	}
<Service>
	public MemberDTO save(MemberDTO memberDTO) {
		
		MemberEntity memberEntity = new ModelMapper().map(memberDTO, MemberEntity.class);
		
		memberEntity.setCreateDate(new Date());
		memberEntity.setUpdateDate(new Date());
		
		memberRespository.save(memberEntity);
		
		return new ModelMapper().map(memberEntity, MemberDTO.class);
	}

<Postman>

Postman에서 테스트

3. Read 작업 (GET)

<Controller>
	@GetMapping("/all")
	public ResponseEntity<?> findAll(){
		Map<String, Object> map = new HashMap<>();
		
		try {
			List<MemberDTO> list = memberService.findAll();
			map.put("list", list);
			return ResponseEntity.ok().body(map);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err", "에러 발생");
			return ResponseEntity.badRequest().body(map);
		}
	}
<Service>	
    public List<MemberDTO> findAll() {
		
		List<MemberEntity> list_entity = memberRespository.findAll();
		
		List<MemberDTO> list_dto = new ArrayList<>();
		
		for(MemberEntity e : list_entity) {
			list_dto.add(new ModelMapper().map(e, MemberDTO.class));
		}
		
		return list_dto;
	}

<Postman>

Postman에서 테스트

4. Update 작업 (PUT)

<Controller>
	@PutMapping("")
	public ResponseEntity<?> update(@RequestBody MemberDTO memberDTO){
		Map<String, Object> map = new HashMap<>();
		
		MemberDTO db_memberDTO = 
        memberService.findByUsernameAndPassword(memberDTO.getUsername(), memberDTO.getOrgPassword());
		
		if(!(memberDTO==null || memberDTO.getOrgPassword()==null 
				|| memberDTO.getUsername()==null || db_memberDTO == null
				|| memberDTO.getPassword()==null || memberDTO.getPassword2()==null 
				|| !(memberDTO.getPassword().equals(memberDTO.getPassword2())))) {
			try {
				db_memberDTO.setPassword(memberDTO.getPassword());
				memberDTO = memberService.update(db_memberDTO);
				map.put("result", memberDTO);
				return ResponseEntity.ok().body(map);
			} catch (Exception e) {
				e.printStackTrace();
				map.put("err", "수정 실패");
				return ResponseEntity.ok().body(map);
			}
		}else {
			return ResponseEntity.badRequest().body("에러 발생");
		}
	}
<Service>
	public MemberDTO findByUsernameAndPassword(String username, String orgPassword) {
		
		MemberEntity entity = memberRespository.findByUsernameAndPassword(username, orgPassword);
		
		return new ModelMapper().map(entity, MemberDTO.class);
	}

	public MemberDTO update(MemberDTO memberDTO) {
    
		MemberEntity entity = new ModelMapper().map(memberDTO, MemberEntity.class);
		
		entity.setUpdateDate(new Date()); // 수정 시각 설정
		
		entity = memberRespository.save(entity);
		
		return new ModelMapper().map(entity, MemberDTO.class);
	}

<Postman>

Postman에서 테스트

5. Delete 작업 (DELETE)

<Controller>
	@DeleteMapping("")
	public ResponseEntity<?> delete(@RequestBody MemberDTO memberDTO){
		Map<String, Object> map = new HashMap<>();
		
		if(!(memberDTO.getUsername()==null || memberDTO.getUsername().equals("") 
				|| memberDTO.getPassword() == null || memberDTO.getPassword().equals(""))) {
			try {
				memberService.delete(memberDTO);
				map.put("result", "삭제 성공");
				return ResponseEntity.ok().body(map);
			} catch (Exception e) {
				e.printStackTrace();
				map.put("result", "삭제실패");
	            return ResponseEntity.badRequest().body(map);
			}
		}else {
			return ResponseEntity.badRequest().body("해당 데이터는 없습니다");
		}
	}
<Service>
	public void delete(MemberDTO memberDTO) {
    
		MemberEntity entity
        = memberRespository.findByUsernameAndPassword(memberDTO.getUsername(), memberDTO.getPassword());
		
		if(entity == null) {
			throw new RuntimeException("해당 스탭은 없습니다.");
		}
        
		memberRespository.delete(entity);
	}

<Postman>

Postman에서 테스트