엔티티, 컨트롤러, 저장소(Repository) 생성
package com.example.restcontroller;
import java.util.HashMap;
import java.util.Map;
import com.example.entity.BuyEntity;
import com.example.entity.ItemEntity;
import com.example.entity.MemberEntity;
import com.example.repository.BuyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/api/buy")
public class BuyRestController {
@Autowired
BuyRepository bRepository;
// 주문하기
// 127.0.0.1:9090/ROOT/api/buy/insert
// {"bcnt" : 2, "item":{ "icode" : 20}, "member" : {"uemail" : "C"}}
@RequestMapping(value = "/insert1", method = { RequestMethod.POST }, consumes = {
MediaType.ALL_VALUE }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> buyInsertPOST(
@RequestBody BuyEntity buy) {
System.out.println("=== buy.toString() === " + buy.toString());
Map<String, Object> map = new HashMap<String, Object>();
bRepository.save(buy);
map.put("status", 200);
return map;
}
// 주문하기
// 127.0.0.1:9090/ROOT/api/buy/insert
// {"bcnt" : 2, "item":{ "icode" : 20}, "member" : {"uemail" : "C"}}
@RequestMapping(value = "/insert", method = { RequestMethod.POST }, consumes = {
MediaType.ALL_VALUE }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> buyInsert1POST(
@RequestBody Map<String, Object> buy) {
System.out.println("=== buy.toString() === " + buy.toString());
long bcnt = Long.parseLong((buy.get("bcnt")).toString());
System.out.println("=== bcnt === " + bcnt);
BuyEntity buyEntity = new BuyEntity();
buyEntity.setBcnt(Long.parseLong(buy.get("bcnt").toString()));
ItemEntity itemEntity = new ItemEntity();
itemEntity.setIcode(
Long.parseLong(buy.get("icode").toString()));
buyEntity.setItem(itemEntity);
MemberEntity memberEntity = new MemberEntity();
memberEntity.setUemail((String) buy.get("uemail"));
buyEntity.setMember(memberEntity);
bRepository.save(buyEntity);
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", 200);
return map;
}
// 주문 한개 조회
// 127.0.0.1:9090/ROOT/api/buy/selectone?bno=26
@RequestMapping(value = "/selectone", method = { RequestMethod.GET }, consumes = {
MediaType.ALL_VALUE }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> selectOneGET(
@RequestParam(name = "bno") long bno) {
Map<String, Object> map = new HashMap<String, Object>();
BuyEntity buyEntity = bRepository.findById(bno).orElse(null);
map.put("result", buyEntity);
map.put("status", 200);
return map;
}
}
package com.example.entity;
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
// 필요한 항목만 가져오기
public interface BuyProjection {
Long getBno();
Long getBcnt();
Date getBregdate();
// 물품명
@Value("#{target.item.iname}")
String getItemIname();
// 물품가격
@Value("#{target.item.iprice}")
Long getItemIPrice();
// 회원이름
@Value("#{target.member.uname}")
String getMemberUname();
// 회원연락처
@Value("#{target.member.uphone}")
String getMemberUphone();
}
package com.example.repository;
import com.example.entity.BuyEntity;
import com.example.entity.BuyProjection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BuyRepository extends JpaRepository<BuyEntity, Long> {
// projection 사용
BuyProjection findByBno(long bno);
}