파일명 BuyEntity.java
package com.example.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.format.annotation.DateTimeFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Entity
@Data
@Table(name = "BUY")
@SequenceGenerator(name = "SEQ_BUY",
sequenceName = "SEQ_BUY_NO",
allocationSize =1,
initialValue = 1)
public class BuyEntity {
@Id // 기본키
@Column(name = "BNO") // 컬럼명
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_BUY") // 시퀀스 적용
private Long bno;
@Column(name = "BCNT")
private Long bcnt;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@CreationTimestamp //CURRENT_DATE
@Column(name = "BREGDATE")
private Date bregdate;
// 물품테이블
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "icode")
private ItemEntity item;
// 회원테이블
@ManyToOne
@JoinColumn(name = "uemail")
private MemberEntity member;
}
파일명 MemberEntity.java
package com.example.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.format.annotation.DateTimeFormat;
import lombok.Data;
@Data
@Entity
@Table(name = "MEMBER")
public class MemberEntity {
// 이메일
@Id
private String uemail;
// 암호
@Column(nullable = false)
private String upw;
// 이름
private String uname;
// 연락처
private String uphone;
// 권한
private String urole;
// 등록일
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@CreationTimestamp //CURRENT_DATE
@Column(name = "UREGDATE")
private Date uregdate;
}
파일명 ItemEntity.java
package com.example.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.format.annotation.DateTimeFormat;
import lombok.Data;
@Entity
@Data
@Table(name = "ITEM")
@SequenceGenerator(name = "SEQ_ITEM",
sequenceName = "SEQ_ITEM_ICODE",
allocationSize =1,
initialValue = 1)
public class ItemEntity {
// 물품코드
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ITEM") // 시퀀스 적용
private Long icode;
// 물품이름
private String iname;
// 물품내용
@Lob
private String icontent;
// 물품가격
private Long iprice;
// 재고수량
private Long iquantity;
// 등록일
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@CreationTimestamp // CURRENT_DATE
@Column(name = "IREGDATE")
private Date iregdate;
// 이미지
@Lob
private byte[] iimage;
// 이미지크기
private Long iimagesize;
// 이미지타입
private String iimagetype;
// 이미지명
private String iimagename;
// 회원테이블(판매자)
@JsonBackReference
@ManyToOne
@JoinColumn(name = "uemail")
private MemberEntity member;
}
파일명 BuyRepository.java
package com.example.repository;
import java.util.List;
import com.example.entity.BuyEntity;
import com.example.entity.BuyProjection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface BuyRepository extends JpaRepository<BuyEntity, Long> {
// findBy변수
BuyProjection findByBno(Long bno);
// findBy변수_하위변수(Entity에 외래키로 되어있는 변수 찾을떄)
List<BuyProjection> findByMember_uemail(String uemail);
// SELECT* FROM 테이블명 WHERE 1 ORDER BY BNO ASC
List<BuyProjection> findByOrderByBnoAsc();
// SELECT * FROM 테이블명 WHERE BCNT >= ?
List<BuyProjection> findByBcntGreaterThanEqual(long bcnt);
// SELECT* FROM 테이블명 WHERE BNO=? AND BCNT=?
BuyProjection findByBnoAndBcnt(Long bno, Long bcnt);
// SELECT* FROM 테이블명 WHERE BNO IN (1,3,5)
// SELECT* FROM 테이블명 WHERE BNO = 1 OR BNO = 3 OR BNO = 5
List<BuyProjection> findByBnoIn(List<Long> bno);
// 네이티브는 사용법이 DB마다 달라서 잘 사용 안함
@Query(value = "SELECT * FROM BUY", nativeQuery = true)
public List<BuyProjection> selectBuyList();
@Query(value = "SELECT * FROM BUY WHERE BNO =:no", nativeQuery = true)
public BuyProjection selectBuyOne(@Param(value = "no")long bno);
// MYBATIS #{obj.bno} === JPA + HIBERNATE :#{#obj.bno}
@Query(value = "SELECT * FROM BUY WHERE BNO = :#{#obj.bno} AND BCNT = :#{#obj.bcnt}", nativeQuery = true)
public BuyProjection selectBuyOneAnd(@Param(value = "obj")BuyEntity buy);
}
파일명 BuyProjection.java
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 getItemInamw();
// 물품가격
@Value("#{target.item.iprice}")
Long getItemIprice();
// 회원이름
@Value("#{target.member.uname}")
String getMemberUname();
//회원연락처
@Value("#{target.member.uphone}")
String getMemberUphone();
}
파일명 BuyRestController.java
package com.example.restcontroller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.entity.BuyEntity;
import com.example.entity.BuyProjection;
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":22, "icode":12, "uemail":"ccc"}
@RequestMapping(
value = "/insert",
method = { RequestMethod.POST },
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> buyInsertPost(
@RequestBody Map<String, Object> buy
){
System.out.println("==========BuyRestController.java => buy.toString==========");
System.out.println(buy.toString());
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);
System.out.println(buy.toString());
Map<String, Object> map = new HashMap<String, Object>();
map.put("sataus", 200);
return map;
}
// 127.0.0.1:9090/ROOT/api/buy/insert1
// {"bcnt":22, "item":{"icode":12}, "member":{"uemail":"ccc"}}
@RequestMapping(
value = "/insert1",
method = { RequestMethod.POST },
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> buyInsertPost(
@RequestBody BuyEntity buyEntity
){
System.out.println("==========BuyRestController.java => buyEntity.toString==========");
System.out.println(buyEntity.toString());
Map<String, Object> map = new HashMap<String, Object>();
bRepository.save(buyEntity);
map.put("status",200);
return map;
}
// 주문 1개 조회
// 127.0.0.1:9090/ROOT/api/buy/selectone?bno=15
@RequestMapping(
value = "/selectone",
method = { RequestMethod.GET },
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> buySelectOneGET(
@RequestParam(name = "bno")long bno
){
Map<String, Object> map = new HashMap<String, Object>();
// BuyEntity buyEntity = bRepository.findById(bno).orElse(null);
BuyProjection buy = bRepository.findByBno(bno);
// System.out.println("==========BuyRestController.java => buy==========");
// System.out.println(buy);
map.put("result",buy);
map.put("status",200);
return map;
}
// 127.0.0.1:9090/ROOT/api/buy/selectlist?uemail=ccc
@RequestMapping(
value = "/selectlist",
method = { RequestMethod.GET },
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> buySelectListGET(
@RequestParam(name = "uemail") String uemail
){
Map<String, Object> map = new HashMap<String, Object>();
List<BuyProjection> list = bRepository.findByMember_uemail(uemail);
// System.out.println("==========BuyRestController.java => list==========");
// System.out.println(list.toString());
map.put("result",list);
map.put("status",200);
return map;
}
}