-
[SpringBoot #3] 고객 생성하기Java/SpringBoot 2022. 6. 20. 20:09
1. Lombok 설치하기
1) pom.xml 파일을 열어 dependency를 추가한다.
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2) 파일 저장
3) 프로젝트 클린
- Project > Clean... > Clean
4) Maven Update
- 프로젝트 우클릭 > Maven > Update Project... > OK
2. Entity
package com.egurishun.domain.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Entity @Data @AllArgsConstructor @NoArgsConstructor @Builder public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String name; @Column private String address; }
3. Repository
package com.egurishun.domain.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.egurishun.domain.entity.Customer; public interface CustomerRepository extends JpaRepository<Customer, Long> { }
4. Service
package com.egurishun.service; import com.egurishun.domain.entity.Customer; public interface CustomerService { public Customer createCustomer(Customer customer); }
5. ServiceImpl
package com.egurishun.service; import org.springframework.stereotype.Service; import com.egurishun.domain.entity.Customer; import com.egurishun.domain.repository.CustomerRepository; @Service public class CustomerServiceImpl implements CustomerService { private final CustomerRepository repository; public CustomerServiceImpl(CustomerRepository repository) { this.repository = repository; } @Override public Customer createCustomer(Customer customer) { return repository.save(customer); } }
6. Contoller
package com.egurishun.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.egurishun.domain.entity.Customer; import com.egurishun.service.CustomerService; @RestController public class CustomerController { @Autowired CustomerService service; @PostMapping("/customer") public int createCustomer(@RequestBody Customer customer) { if (service.createCustomer(customer) != null) { return 1; } return 0; } }
Test Environment Info.
- OS : macOS Catalina 10.15.2
- JDK : 1.8.0_321
- STS : 4.13.0.RELEASE'Java > SpringBoot' 카테고리의 다른 글
[SpringBoot #2] Package, Class 생성 (0) 2022.06.17 [SpringBoot #1-2] Project 생성(DB Driver 포함_PostgreSQL) (0) 2022.06.17 [SpringBoot #1-1] Project 생성(Spring Web 기본) (0) 2022.06.15