Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • fix_branch
  • master
2 results

Target

Select target project
  • kosandisu/spring-basic
1 result
Select Git revision
  • fix_branch
  • master
2 results
Show changes
Commits on Source (2)
......@@ -104,6 +104,9 @@
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......@@ -122,6 +125,7 @@
<artifactId>mybatis-spring</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
......@@ -149,15 +153,24 @@
<executions>
<execution>
<goals>
<goal>process</goal>
<goal>
process
</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
<outputDirectory>
target/generated-sources/java
</outputDirectory>
<processor>
com.querydsl.apt.jpa.JPAAnnotationProcessor
</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
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; }
}
\ No newline at end of file
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 {
@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;
import lombok.ToString;
......
......@@ -6,8 +6,20 @@ import org.springframework.data.repository.query.Param;
import java.util.List;
import com.example.demo.entity.Item;
// @NoRepositoryBean
public interface ItemRepository extends JpaRepository<Item, Long>{
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);
*
*/
}
......@@ -6,24 +6,101 @@ 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 org.junit.jupiter.api.DisplayName;
import java.time.LocalDateTime;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.runner.RunWith;
import com.example.demo.constant.ItemSellStatus; import java.util.List;
import com.querydsl.jpa.impl.JPAQueryFactory; import com.querydsl.jpa.impl.JPAQuery;
import com.example.demo.constant.ItemSellStatus;
import java.util.List;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.jpa.impl.JPAQuery;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@SpringBootTest
@TestPropertySource(locations="classpath:application-test.properties")
@RunWith( SpringJUnit4ClassRunner.class )
public class ItemRepositoryTest {
@Autowired
ItemRepository itemRepository;
@Test
@DisplayName("상품 저장 테스트") public void createItemTest(){
Item item = new Item(); item.setItemNm("테스트 상품"); item.setPrice(100000); item.setItemDetail("테스트 상품 상세 설명"); item.setStockNumber(100); item.setRegTime(LocalDateTime.now()); item.setUpdateTime(LocalDateTime.now());
Item savedItem = itemRepository.save(item);
System.out.println(savedItem.toString());
@PersistenceContext
EntityManager em;
public void createItemList() {
for (int i = 1; i <= 10; i++) {
}
}
@Test
@DisplayName("상품명 조회 테스트")
public void findByItemNmTest() {
this.createItemList();
List<Item> itemList = itemRepository.findByItemNm("테스트 상품 1");
for (Item item : itemList) {
System.out.println(item.toString());
}
}
}
\ No newline at end of file
@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());
}
}
/*
pg 26
* @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());
}
}
}