티스토리 뷰
9. 프로그램
(타슈켄트) JPA> 쇼핑몰플젝 Customer.java, CustomerDTO.java, CustomerController.java, CustomerRepository.java
패스트코드블로그 2020. 5. 10. 16:34Customer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package sbr.iowa.demo.customer; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.ToString; @NoArgsConstructor(access = AccessLevel.PROTECTED) @Getter @ToString @Entity @Table(name = "customers") public class Customer implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "customer_id") private String customerId; @Column(name = "customer_name") private String customerName; @Column(name = "password") private String password; @Column(name = "ssn") private String ssn; @Column(name = "phone") private String phone; @Column(name = "city") private String city; @Column(name = "address") private String address; @Column(name = "postalcode") private String postalcode; @Column(name = "photo") private String photo; // @Column(name = "created_at") private Date createAt; // @Column(name = "updated_at") private Date updatedAt; @Override public String toString() { return String.format("Customer[customerId=%d, password='%s', customerName='%s']", customerId, password, customerName); } @Builder private Customer(String customerId, String password, String customerName, String ssn, String phone, String city, String address, String postalcode, String photo) { this.customerId = customerId; this.password = password; this.customerName = customerName; this.ssn = ssn; this.phone = phone; this.city = city; this.address = address; this.postalcode = postalcode; this.photo = photo; } @Builder private Customer(String customerId, String password, String customerName) { this.customerId = customerId; this.password = password; this.customerName = customerName; } } | cs |
CustomerDTO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package sbr.iowa.demo.customer; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import lombok.Data; import lombok.NoArgsConstructor; @Data @Component @Lazy @NoArgsConstructor public class CustomerDTO { private String customerId, customerName, password, ssn, phone, city, address, postalcode, photo; public CustomerDTO(Customer customer) { this.customerId = customer.getCustomerId(); this.customerName = customer.getCustomerName(); this.password = customer.getPassword(); this.ssn = customer.getSsn(); this.phone = customer.getPhone(); this.city = customer.getCity(); this.postalcode = customer.getPostalcode(); } } | cs |
CustomerController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | package sbr.iowa.demo.customer; import java.util.HashMap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import sbr.iowa.demo.util.Pager; import sbr.iowa.demo.util.Printer; @RestController @RequestMapping("/customers") public class CustomerController { @Autowired ICustomerService customerService; @Autowired CustomerDTO customer; @Autowired Pager pager; @Autowired Printer printer; @PostMapping("") public HashMap<String,Object> join(@RequestBody CustomerDTO param){ HashMap<String,Object> map = new HashMap<>(); return map; } @GetMapping("/page/{pageNum}") public HashMap<String, Object> list(@PathVariable String pageNum){ HashMap<String, Object> map = new HashMap<>(); map.put("totalCount", customerService.countAll()); map.put("page_num", pageNum); map.put("page_size", "5"); map.put("block_size", "5"); map.put("list", customerService.findCustomers(pager)); return map; } @GetMapping("/count") public String count() { System.out.println("CustomerController count() 경로로 들어옴"); int count = customerService.countAll(); printer.accept("람다가 출력한 고객의 총인원 : "+count); return count+""; } @GetMapping("/{customerId}/{password}") public CustomerDTO login(@PathVariable("customerId")String id, @PathVariable("password")String pass){ customer.setCustomerId(id); customer.setPassword(pass); return customerService.login(customer); } @GetMapping("/{customerId}") public CustomerDTO getCustomer(@PathVariable String customerId) { System.out.println("ID 검색 진입 : "+customerId); return customerService.findCustomerByCustomerId(customerId); } @PutMapping("/{customerId}") public CustomerDTO updateCustomer(@RequestBody CustomerDTO param) { System.out.println("수정 할 객체: "+param.toString()); int res = customerService.updateCustomer(param); System.out.println("====> "+res); if(res == 1){ customer = customerService.findCustomerByCustomerId(param.getCustomerId()); }else{ System.out.println("컨트롤러 수정 실패"); } return customer; } @DeleteMapping("/{customerId}") public HashMap<String,Object> deleteCustomer(@PathVariable String customerId) { HashMap<String, Object> map = new HashMap<>(); customer.setCustomerId(customerId); customerService.deleteCustomer(customer); map.put("result","탈퇴성공"); return map; } } | cs |
CustomerRepository.java
1 2 3 4 5 6 7 8 9 10 11 | package sbr.iowa.demo.customer; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface CustomerRepository extends CrudRepository<Customer, Long> , JpaSpecificationExecutor<Customer>{ } | cs |
'9. 프로그램' 카테고리의 다른 글
JPA OneToMany WoonUser.java (0) | 2020.08.02 |
---|---|
JPA Thomas Woon User, Group, JoinGroup (0) | 2020.08.02 |
Intellij에서 JPA 사용시 Querydsl 에러 해결 (0) | 2020.07.16 |
프로그램/VS코드/2020-06-20/ 오픈소스 프로젝트 인기 1위는 'VScode' (0) | 2020.06.20 |
JPA import com.sun.istack.NotNull 로 인한 에러 발생 (0) | 2020.05.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- FLASK
- SpringBoot
- mariadb
- tensorflow
- JUnit
- SQLAlchemy
- vscode
- docker
- maven
- Django
- Git
- intellij
- database
- Mlearn
- Python
- ERD
- Algorithm
- AWS
- nodejs
- COLAB
- jQuery
- Mongo
- Oracle
- springMVC
- JPA
- terms
- Eclipse
- React
- KAFKA
- Java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함