74. 물품 등록, 조회
-물품 등록, 조회
*service 생성(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();
}
*Impl 생성(ItemServiceImpl.java)
package com.example.service;
import java.util.List;
import com.example.entity.Item;
import com.example.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ItemServiceImpl implements ItemService{
@Autowired
ItemMapper iMapper;
@Override
public int insertItem(Item item) {
return iMapper.queryInsertItem(item);
}
@Override
public List<Item> selectItemList() {
return iMapper.querySelectList();
}
}
* insert추가(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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
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;
}
}
*Application 수정
package com.example.b20211019;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.repository")
@EntityScan(basePackages = {"com.example.entity"})
@ComponentScan(basePackages = { "com.example.controller",
"com.example.service", "com.example.config" })
@MapperScan(basePackages = "com.example.mapper")
public class B20211019Application {
public static void main(String[] args) {
SpringApplication.run(B20211019Application.class, args);
System.out.println("start");
}
}
*물품 등록.
* select 추가 (RestItemController.java)
... ...
//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;
}
}
*목록 조회
'Spring' 카테고리의 다른 글
Spring 일지 #75 (20211019) 시험 대비(물품 조회, 삭제, 수정 등록) (0) | 2021.10.21 |
---|---|
Spring 일지 #73 (20211019) 시험 대비(마이바티스, Mapper) (0) | 2021.10.20 |
Spring 일지 #72 (20211019) 시험 대비(member 등록) (0) | 2021.10.20 |
Spring 일지 #71 (20211019) 시험 대비(entity, 저장소, service) (0) | 2021.10.20 |
Spring 일지 #70 (20211019) 시험 대비(프로젝트 생성, 환경 설정) (0) | 2021.10.20 |