Skip to content
Snippets Groups Projects
Commit cdb175b1 authored by 장 무현's avatar 장 무현
Browse files

spring

parent b34bd4e1
Branches
No related tags found
No related merge requests found
......@@ -12,9 +12,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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping(value = "/test")
public UserDto test(){
UserDto dto = new UserDto();
dto.setAge(17);
dto.setName("Jane");
return dto;
}
}
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.JsonArray;
import com.google.gson.JsonObject;
@RestController
public class TestController{
@RequestMapping(value = "/", method = RequestMethod.GET)
public String test(@RequestParam("id") String 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();
}
}
package com.example.demo;
package com.example.demo.controller;
import lombok.Getter;
import lombok.Setter;
......
......@@ -11,5 +11,13 @@ import com.example.demo.entity.Item;
// @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
......@@ -26,6 +26,10 @@ public class ItemRepositoryTest {
@Autowired
ItemRepository itemRepository;
@PersistenceContext
EntityManager em;
@Test
@DisplayName("상품 저장 테스트")
public void createItemTest(){
......@@ -41,7 +45,91 @@ public class ItemRepositoryTest {
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());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment