Spring

Spring 일지 #75 (20211019) 시험 대비(물품 조회, 삭제, 수정 등록)

uni5948 2021. 10. 21. 16:53

75. 물품 조회, 삭제, 수정 등록

-물품 조회, 삭제, 수정

 * 저장소 생성(ItemRepository.java)

ItemRepository.java

 

package com.example.repository;

 

import java.util.List;

 

import com.example.entity.Item;

 

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

 

@Repository

public interface ItemRepository extends JpaRepository<Item, Long>{

   

     //SELECT * FROM ITEM8 WHERE NAME=?

     Item findByName(String name);

 

     //SELECT * FROM ITEM8 WHERE NO=?

     Item findByNo(long no);

 

     //전달 되는 가격보다 큰 것

     //SELECT * FROM ITEM8 WHERE PRICE >= ?

     List<Item> findByPriceGreaterThanEqual(long price);

}

 

 *서비스 등록(ItemService.java)

package com.example.service;

 

import java.util.List;

 

import com.example.entity.Item;

 

import org.springframework.stereotype.Service;

 

@Service

public interface ItemService {

    //물품 등록

    public int insertItem(Item item);

 

    //물품 전체 조회

    public List<Item> selectItemList();

 

    //물품 삭제

    public int deleteItem(long no);

 

    //물품 수정

    public int updateItem(Item item);

   

    //물품 1개 조회

    public Item selectItemOne(long no);

}

 

 * Impl 등록(ItemServiceImpl.java)

 * 저장소를 등록해 준다.

 

package com.example.service;

 

import java.util.List;

 

import com.example.entity.Item;

import com.example.mapper.ItemMapper;

import com.example.repository.ItemRepository;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

@Service

public class ItemServiceImpl implements ItemService{

 

    @Autowired

    ItemMapper iMapper;

 

    @Autowired

    ItemRepository iRepository;

 

    @Override

    public int insertItem(Item item) {

        //return iMapper.queryInsertItem(item);     마이바티스

        iRepository.save(item);

        return 0;

    }

 

    @Override

    public List<Item> selectItemList() {

        //return iMapper.querySelectList();     마이바티스

        return iRepository.findAll();

    }

 

    @Override

    public int deleteItem(long no) {

        iRepository.deleteById(no);

        return 0;

    }

 

    @Override

    public int updateItem(Item item) {

        iRepository.save(item);

        return 0;

    }

 

    @Override

    public Item selectItemOne(long no) {

        return iRepository.findByNo(no);

    }

}

 

* 삭제, 수정, 1개 조회 추가(RestItemController.java)

package com.example.controller;

더보기

 

import java.util.HashMap;

import java.util.Map;

 

import com.example.entity.Item;

import com.example.entity.Member;

import com.example.service.ItemService;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

 

@RestController

@RequestMapping(value = "/item")

public class RestItemController {

    @Autowired

    ItemService iService;

 

    //127.0.0.1:8080/ROOT/item/home.json

    @GetMapping(value = "/home.json",

        consumes = MediaType.ALL_VALUE,

        produces = MediaType.APPLICATION_JSON_VALUE)

    public Object homeGET(){

        Map<String, Object> map = new HashMap<>();

        map.put("status", 200);

        return map;

    }

 

     //127.0.0.1:8080/ROOT/item/insert.json

     @PostMapping(value = "/insert.json",

     consumes = MediaType.ALL_VALUE,

     produces = MediaType.APPLICATION_JSON_VALUE)

    public Object insertItemPOST(@RequestBody Item item){

        Map<String, Object> map = new HashMap<>();

        try{

            Member member = new Member();

            member.setId("a");

            item.setMember(member);

 

            iService.insertItem(item);

            map.put("status", 200);

        }

        catch(Exception e){

            e.printStackTrace();

            map.put("status", e.hashCode());

        }

        return map;

    }

 

    //127.0.0.1:8080/ROOT/item/select.json

    @GetMapping(value = "/select.json",

    consumes = MediaType.ALL_VALUE,

    produces = MediaType.APPLICATION_JSON_VALUE)

        public Object selectitemGET(){

    Map<String, Object> map = new HashMap<>();

    try{

        map.put("status", 200);

        map.put("list", iService.selectItemList());

    }

    catch(Exception e){

        e.printStackTrace();

        map.put("status", e.hashCode());

    }

    return map;

  }

 

        //127.0.0.1:8080/ROOT/item/update.json

        @PutMapping(value = "/update.json",

        consumes = MediaType.ALL_VALUE,

        produces = MediaType.APPLICATION_JSON_VALUE)

            public Object updatePUT(@RequestBody Item item){

        Map<String, Object> map = new HashMap<>();

        try{

        //판매정 정보가 없어도 a값으로 들어감

        Member member = new Member();  

        member.setId("a");

        item.setMember(member);

 

        map.put("status", 200);

        iService.updateItem(item);

        }

        catch(Exception e){

            e.printStackTrace();

            map.put("status", e.hashCode());

        }

        return map;

    }

 

        //127.0.0.1:8080/ROOT/item/delete.json

        @DeleteMapping(value = "/delete.json",

        consumes = MediaType.ALL_VALUE,

        produces = MediaType.APPLICATION_JSON_VALUE)

        public Object itemDelete(@RequestParam("no") Long no){

        Map<String, Object> map = new HashMap<>();

        try{

            map.put("status", 200);

            iService.deleteItem(no);

        }

        catch(Exception e){

            e.printStackTrace();

            map.put("status", e.hashCode());

        }

        return map;

    }

 

    //127.0.0.1:8080/ROOT/item/selectone.json

    @GetMapping(value = "/selectone.json",

        consumes = MediaType.ALL_VALUE,

        produces = MediaType.APPLICATION_JSON_VALUE)

    public Object selectItemOne(@RequestParam("no") Long no){

    Map<String, Object> map = new HashMap<>();

    try{

        map.put("status", 200);

        map.put("item", iService.selectItemOne(no));

    }

    catch(Exception e){

        e.printStackTrace();

        map.put("status", e.hashCode());

    }

    return map;

    }

}