Skip to content
Snippets Groups Projects
Commit e2bb62d8 authored by Jingeun Lee's avatar Jingeun Lee
Browse files

rest controller complate

parent 3a078270
No related branches found
No related tags found
No related merge requests found
......@@ -11,9 +11,5 @@ public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping(value="/")
public String HelloWorld(){
return "Hello World";
}
}
package com.example.demo;
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.JsonObject;
@RestController
public class TestController {
@GetMapping(value = "/test")
......@@ -10,4 +15,15 @@ public class TestController {
dto.setName("Jane");
return dto;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String test(@RequestParam("id") String id){
System.out.println(id);
JsonObject obj = new JsonObject();
obj.addProperty("title", "산사와 아가싸");
obj.addProperty("content", "로맨틱 코메디");
JsonObject data = new JsonObject();
data.addProperty("time", "토일 8시");
obj.add("data", data);
return obj.toString();
}
}
\ No newline at end of file
package com.example.demo;
package com.example.demo.controller;
import lombok.Getter;
import lombok.Setter;
......
......@@ -9,5 +9,10 @@ import com.example.demo.entity.Item;
import java.util.List;
// @NoRepositoryBean
public interface ItemRepository extends JpaRepository<Item, Long>{
List<Item> findByItemNm(String itemNm);
List<Item> findByItemNmOrItemDetail(String itemNm, String itemDetail);
List<Item> findByPriceLessThan(Integer price);
List<Item> findByPriceLessThanOrderByPriceDesc(Integer price);
@Query("select i from Item i where i.itemDetail like %:itemDetail% order by i.price desc")
List<Item> findByItemDetail(@Param("itemDetail") String itemDetail);
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import com.example.demo.entity.Item;
//import com.example.demo.entity.QItem;
import com.example.demo.entity.QItem;
import com.example.demo.repository.ItemRepository;
import org.junit.jupiter.api.DisplayName;
......@@ -24,6 +24,9 @@ import javax.persistence.PersistenceContext;
public class ItemRepositoryTest {
@Autowired
ItemRepository itemRepository;
@PersistenceContext
EntityManager em;
@Test
@DisplayName("상품 저장 테스트")
public void createItemTest() {
......@@ -37,4 +40,80 @@ public class ItemRepositoryTest {
Item savedItem = itemRepository.save(item);
System.out.println(savedItem.toString());
}
public void createItemList(){
for(int i=1; i <=10; i++){
Item item = new Item();
item.setItemNm(("테스트 상품") + i);
item.setPrice(10000 + i);
item.setItemDetail("테스트 상품 상세 설명" + i);
item.setItemSellStatus(ItemSellStatus.SELL);
item.setStockNumber(100);
item.setRegTime(LocalDateTime.now());
item.setUpdateTime(LocalDateTime.now());
Item savedItem = itemRepository.save(item);
}
}
@Test
@DisplayName("상품명 조회 테스트")
public void findByItemNmTest(){
this.createItemList();
List<Item> itemList = itemRepository.findByItemNm("테스트 상품1");
for(Item item : itemList){
System.out.println(item.toString());
}
}
@Test
@DisplayName("상품명, 상품상세 설명 OR 테스트")
public void findByItemNmOrItemDetailTest(){
this.createItemList();
List<Item> itemList = itemRepository.findByItemNmOrItemDetail("테스트 상품1",
"테스트 상품 상세 설명5");
for(Item item : itemList){
System.out.println(item.toString());
}
}
@Test
@DisplayName("가격 LessThan 테스트")
public void findByPriceLessThanTest(){
this.createItemList();
List<Item> itemList = itemRepository.findByPriceLessThan(10005);
for(Item item: itemList){
System.out.println(item.toString());
}
}
@Test
@DisplayName("가격 내람차순 조회 테스트")
public void findByPriceLessThanOrderByPriceDesc(){
this.createItemList();
List<Item> itemList = itemRepository.findByPriceLessThanOrderByPriceDesc(10005);
for(Item item: itemList){
System.out.println(item.toString());
}
}
@Test
@DisplayName("@Query를 이용한 상품 조회 테스트")
public void findByItemDetailTest(){
this.createItemList();
List<Item> itemList = itemRepository.findByItemDetail("테스트 상품 상세 설명");
for(Item item : itemList){
System.out.println(item.toString());
}
}
@Test
@DisplayName("Querydsl 조회테스트1")
public void queryDslTest(){
this.createItemList();
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
QItem qItem = QItem.item;
JPAQuery<Item> query = queryFactory.selectFrom(qItem)
.where(qItem.itemSellStatus.eq(ItemSellStatus.SELL))
.where(qItem.itemDetail.like("%" + "테스트 상품 상세 설명" + "%"))
.orderBy(qItem.price.desc());
List<Item> itemList = query.fetch();
for(Item item : itemList){
System.out.println(item.toString());
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment