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

Target

Select target project
  • wonbin/spring-basic
1 result
Select Git revision
Show changes
Commits on Source (2)
......@@ -2,12 +2,18 @@ package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping(value="/")
public String HelloWorld(){
return "Hello World";
}
}
\ No newline at end of file
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;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter @Setter
@ToString
public class UserDto {
private String name;
private Integer age;
}
package com.example.demo.constant;
public enum ItemSellStatus {
SELL, SOLD_OUT
}
package com.example.demo.entity;
import com.example.demo.constant.ItemSellStatus;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;
import javax.persistence.*;
import org.springframework.stereotype.Component;
@Table(name="item")
@Getter
@Setter
@ToString
@Entity //테이블과 맵핑 되는 객체
public class Item {
@Id
@Column(name="item_id")
@GeneratedValue(strategy = GenerationType.AUTO) //숫자 자동 증가
private Long id;
@Column(nullable = false, length= 50) //값이 있어야됨
private String itemNm;
@Column(name="price", nullable = false)
private int price;
@Column(nullable = false)
private int stockNumber;
@Lob
@Column(nullable = false)
private String itemDetail;
@Enumerated(EnumType.STRING)
private ItemSellStatus itemSellStatus;
private LocalDateTime regTime;
private LocalDateTime updateTime;
}
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.jpa.repository.Query;
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>{
List<Item> findByItemNm(String itemNm);
List<Item> findByItemNmOrItemDetail(String itemNm, String itemDetail);
List<Item> findByPriceLessThan(Integer price);
List<Item> findByPriceLessThanOrderByPriceDesc(Integer price);
}
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql = true
spring.application.name=demo
server.port = 3000
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/shop?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=web1
# spring.datasource.driver-class-name=org.h2.Driver
# spring.datasource.url=jdbc:h2:mem:test
# spring.datasource.username=sa
# spring.datasource.password=
# spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql = true
spring.jpa.properties.hibernate.connection.autocommit = true
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.type.descriptor.sql = trace
package com.example.demo.repository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
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 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 javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@SpringBootTest
@TestPropertySource(locations="classpath:application-test.properties")
@RunWith( SpringJUnit4ClassRunner.class )
public class ItemRepositoryTest {
@Autowired
ItemRepository itemRepository;
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());
}
}
}