ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring JPA(3) CRUD 기본 예제
    Spring 2020. 1. 30. 14:39

    Github

    https://github.com/hodolee246/on-my-own/tree/master/JPASample03

     

    MemberController.java

    package com.example.JPAsample03.member.controller;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Optional;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    
    import com.example.JPAsample03.member.entity.Member;
    import com.example.JPAsample03.member.repository.MemberRepository;
    
    import lombok.extern.java.Log;
    
    @Log
    @Controller
    public class MemberController {
    	
    	@Autowired
    	MemberRepository memberRepository;
    	
    	@GetMapping(value = "/insert")
    	public ResponseEntity<?> insert() {
    		
    		log.info("memberController insert() ");
    		
    		Member mem = new Member();
    		mem.setName("inwoo");
    		mem.setAge(11);
    		
    		memberRepository.save(mem);
    		
    		Map<String, Object> resultMap = new HashMap<>();
    		List<Member> memList = memberRepository.findAll();
    		
    		resultMap.put("memList", memList);
    		
    		return ResponseEntity.ok().body(resultMap);
    	}
    	
    	@GetMapping(value = "/select")
    	public ResponseEntity<?> select() {
    		
    		log.info("memberController select() ");
    		
    		HashMap<String, Object> resultMap = new HashMap<>();
    		Optional<Member> mem = memberRepository.findById(1L);
    		
    		if(mem.isPresent()) {
    			resultMap.put("mem", mem);
    		}
    		
    		return ResponseEntity.ok().body(resultMap);
    	}
    	
    	@GetMapping(value = "/update")
    	public ResponseEntity<?> update() {
    		
    		log.info("memberController update() ");
    		
    		Member mem = new Member();
    		mem.setId(1L);
    		mem.setName("changeInwoo");
    		mem.setAge(22);
    		
    		memberRepository.save(mem);
    		
    		HashMap<String, Object> resultMap = new HashMap<>();
    		Optional<Member> Optinalmem = memberRepository.findById(1L);
    		
    		if(Optinalmem.isPresent()) {
    			resultMap.put("Optinalmem", Optinalmem);
    		}
    		
    		return ResponseEntity.ok().body(resultMap);
    	}
    	
    	@DeleteMapping(value = "/delete")
    	public ResponseEntity<?> delete() {
    		
    		log.info("memberController delete () ");
    		
    		memberRepository.deleteById(1L);
    		
    		HashMap<String, Object> resultMap = new HashMap<>();
    		Optional<Member> Optinalmem = memberRepository.findById(1L);
    		
    		if(Optinalmem.isPresent()) {
    			resultMap.put("Optinalmem", Optinalmem);
    		}
    		
    		return ResponseEntity.ok().body(resultMap);
    	}
    	
    }
    

     

    실행결과

     

     

    'Spring' 카테고리의 다른 글

    Spring Boot HikariCP  (0) 2020.02.04
    Spring JPA(4) 연관관계 매핑  (0) 2020.01.30
    Spring JPA(2) - 엔티티 매핑  (0) 2020.01.29
    Spring JPA(1) ORM, Hibernate(JPA), 영속성  (0) 2020.01.22
    Spring Boot DB 오류해결  (0) 2020.01.20

    댓글

Designed by Tistory.