본문 바로가기

프로젝트/스프링 & 타임리프

35. 쇼핑몰 :: JPA 설정용 Customer.java, CustomerDTO.java, CustomerController.java, CustomerRepository.java

Customer.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