본문 바로가기

스프링부트(Spring Boot)

Item 생성 (imsapi 프로젝트)

1. imsapi(item management system api) 프로젝트 생성

 - Type: Maven

 - Packaging: Jar

 - Java Version: 11

 - Language: Java

 - Spring Boot Version: 2버전

<Dependencies>

groupId
artifactId
org.springframework.boot
spring-boot-starter-test
spring-boot-devtools
spring-boot-starter-web
spring-boot-starter-data-jpa
org.projectlombok
lombok
org.modelmapper
modelmapper
mysql
mysql-connector-java
javax.validation
validation-api

 

2.application.properties  설정

server.port=9001
server.servlet.context-path=/
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/item?serverTimezone=Asia/Seoul
spring.datasource.username=tj705
spring.datasource.password=tj705

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.show-sql=true

 

3. ItemEntity 클래스 생성

@Entity
@Data // Getter + Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "item") // item 이라는 table을 만든다
public class ItemEntity {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@Column(nullable = false) // 값이 존재해야 함.
	private String itemName;
	
	@Column(nullable = false)
	private long price;
	
	private int discount;
	
	@Column(nullable = false)
	private int ea;
	
	@Column(nullable = false)
	private String itemDescribe;
	
	@Column(nullable = false)
	private String staff;
	
	private Date createDate;
	
	private Date updateDate;
}

 

4. ItemDTO 클래스 생성

- salePrice 추가

salePrice = price*(100-discount) / 100

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ItemDTO {

	private long id;
	private String itemName;
	private long price;
	private int discount;
	private long salePrice; // 판매가격 정보 추가
	private int ea;
	private String itemDescribe;
	private String staff;
	private Date createDate;
	private Date updateDate;
	
}

 

5. ItemRepository 인터페이스 생성

public interface ItemRepository extends JpaRepository<ItemEntity, Long>{

}

 

6. ItemController 클래스 생성

@RestController // @Controller + @ResponseBody
@RequestMapping("/item/api")
public class ItemController {
	
    @Autowired
	private ItemService itemService;
    
}

 

7. ItemService 클래스 생성

@Service
public class ItemService {
	
	@Autowired
	private ItemRepository itemRepository;
	
}