Skip to content
Snippets Groups Projects
Commit 5b7498f5 authored by 정 원빈's avatar 정 원빈
Browse files

spring-basic

parent 877e0fe4
Branches
No related tags found
No related merge requests found
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
\ No newline at end of file
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());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment