# 20230605後端
# CustomerBean
```
package com.ctbcbank.demo.lab.bean;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class CustomerBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String password;
@ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinTable(name="product_cart",
joinColumns = {@JoinColumn(name="customer_id")},
inverseJoinColumns = {
@JoinColumn(name = "product_id")
})
private List<ProductBean> products = new ArrayList<ProductBean>();
// @OneToMany(mappedBy = "customer", cascade = CascadeType.REMOVE)
// private List<OrderBean> orders = new ArrayList<>();
public CustomerBean(String name, String password) {
super();
this.name = name;
this.password = password;
// this.cart=cart;
}
}
```
# OrderBean
```
package com.ctbcbank.demo.lab.bean;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@Table
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class OrderBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int total;
private Date createDate = new Date();
public OrderBean(String name, int total) {
super();
this.name = name;
this.total = total;
}
}
```
# ProductBean
```
package com.ctbcbank.demo.lab.bean;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class ProductBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int price;
private String filePath;
private String detail;
private Date updateDate = new Date();
@ManyToOne
@JoinColumn(name = "PRODUCTCATEGORY_ID_FK")
@JsonIgnoreProperties(ignoreUnknown = true,value = {"products"})
ProductCategoryBean productCategory;
@ManyToMany(cascade = CascadeType.ALL, mappedBy="products")
@JsonIgnore
private List<CustomerBean> customers;
public ProductBean(String name, int price, String filePath, String detail) {
super();
this.name = name;
this.price = price;
this.filePath = filePath;
this.detail = detail;
}
}
```
# ProductCategoryBean
```
package com.ctbcbank.demo.lab.bean;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class ProductCategoryBean{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany(mappedBy = "productCategory", cascade = CascadeType.REMOVE)
private List<ProductBean> products = new ArrayList<>();
}
```
# CustomerBeanController
```
package com.ctbcbank.demo.lab.controller.rest;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.lab.bean.CustomerBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.service.CustomerBeanService;
import com.ctbcbank.demo.lab.service.ProductBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/customer")
public class CustomerBeanController {
@Autowired
private CustomerBeanService service;
@Autowired
private ProductBeanService PBservice;
// 取得所有帳戶
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<CustomerBean> getAllCustomers() {
return service.getAllCustomerBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CustomerBean getCustomerBeanById(@PathVariable long id) {
CustomerBean customerBean = service.getCustomerBeanById(id);
return customerBean;
}
// 刪除指定id
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void removeCustomerBean(@PathVariable long id) {
service.removeCustomerBean(id);
}
// 新增
@RequestMapping(value = "/all", method = RequestMethod.POST)
public String createCustomer(@RequestBody CustomerBean bean) {
service.saveOrUpdateCustomerBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public CustomerBean modifyCustomer(@PathVariable("id") Long id, @RequestBody CustomerBean bean) {
bean.setId(id);
return service.updateCustomerBean(bean);
}
// 比對是否存在
@RequestMapping(value = "/{name}", method = RequestMethod.POST)
public Boolean compareCustomerBeanByName(@PathVariable String name) {
Boolean exist = false;
// 將現有帳戶資料查出
List<CustomerBean> allCustomerList = service.getAllCustomerBean();
// 將每筆資料與目前想加入的帳號比對
for (CustomerBean customerBean : allCustomerList) {
String customerName = customerBean.getName();
// 如果相同代表帳戶已存在
if (name.equals(customerName)) {
System.out.println("使用者:" + name + "已存在");
exist = true;
}
}
return exist;
}
// 新增
@RequestMapping(value = "/{customer_id}/{product_id}", method = RequestMethod.POST)
public CustomerBean addProduct(
@PathVariable("customer_id") Long customer_id, @PathVariable("product_id") Long product_id,
@RequestBody ProductBean bean) {
CustomerBean customer1 = service.getCustomerBeanById(customer_id);
ProductBean product1 = PBservice.getProductBeanById(product_id);
customer1.getProducts().add(product1);
service.saveOrUpdateCustomerBean(customer1);
return service.saveOrUpdateCustomerBean(customer1);
}
}
```
# OrderBeanController
```
package com.ctbcbank.demo.lab.controller.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.lab.bean.OrderBean;
import com.ctbcbank.demo.lab.service.OrderBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/order")
public class OrderBeanController {
@Autowired
private OrderBeanService service;
// 取得所有
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<OrderBean> getAllCarts() {
return service.getAllOrderBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public OrderBean getCartBeanById(@PathVariable long id) {
OrderBean cartBean = service.getOrderBeanById(id);
return cartBean;
}
// 刪除指定id
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void removeCartBean(@PathVariable long id) {
System.out.println("DeleteNum=>"+id);
service.removeOrderBean(id);
}
// 刪除全部
@RequestMapping(value = "/all", method = RequestMethod.DELETE)
public void removeAllOrderBean() {
service.removeAllOrderBean();
}
// 新增
@RequestMapping(value = "/all", method = RequestMethod.POST)
public String createCart(@RequestBody OrderBean bean) {
service.saveOrUpdateOrderBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}",method=RequestMethod.PUT)
public OrderBean modifyCart(@PathVariable("id")Long id,@RequestBody OrderBean bean) {
bean.setId(id);
return service.updateOrderBean(bean);
}
}
```
# ProductBeanController
```
package com.ctbcbank.demo.lab.controller.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.lab.bean.CustomerBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.service.CustomerBeanService;
import com.ctbcbank.demo.lab.service.ProductBeanService;
import com.ctbcbank.demo.lab.service.ProductCategoryBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/product")
public class ProductBeanController {
@Autowired
private ProductBeanService service;
@Autowired
private ProductCategoryBeanService categoryService;
@Autowired
private CustomerBeanService customerService;
// 取得所有
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<ProductBean> getAllProducts() {
return service.getAllProductBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ProductBean getProductBeanById(@PathVariable long id) {
ProductBean productBean = service.getProductBeanById(id);
return productBean;
}
// 刪除指定id
@RequestMapping(value = "{customer_id}/{id}", method = RequestMethod.DELETE)
public void removeProductBean(@PathVariable long customer_id,@PathVariable long id) {
CustomerBean customer1 = customerService.getCustomerBeanById(customer_id);
ProductBean product1 = service.getProductBeanById(id);
customer1.getProducts().remove(product1);
service.saveOrUpdateProductBean(product1);
}
// 新增
@RequestMapping(value = "/all/{id}", method = RequestMethod.POST)
public String createProduct(@RequestBody ProductBean bean, @PathVariable("id") Long id) {
bean.setProductCategory(categoryService.getProductCategoryBeanById(id));
service.saveOrUpdateProductBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}/{cate_id}", method = RequestMethod.PUT)
public ProductBean modifyProduct(@PathVariable("id") Long id, @PathVariable("cate_id") Long cate_id,
@RequestBody ProductBean bean) {
bean.setProductCategory(categoryService.getProductCategoryBeanById(cate_id));
bean.setId(id);
return service.saveOrUpdateProductBean(bean);
}
// 比對商品是否存在
// @RequestMapping(value = "/{name}", method = RequestMethod.POST)
// public Boolean compareProductBeanByName(@PathVariable String name) {
// Boolean exist =false;
//
// //將現有資料查出
// List<ProductBean> allCartList = service.getAllProductBean();
//
// //將每筆資料與目前想加入的商品名稱比對
// for (ProductBean productBean : allCartList) {
// String productName = productBean.getName();
//
// //如果相同代表已存在購物車了
// if(name.equals(productName)) {
// System.out.println("商品:"+name+"已存在");
// exist=true;
// }
// }
// return exist;
// }
// 刪除全部
@RequestMapping(value = "{customer_id}/all", method = RequestMethod.DELETE)
public void removeAllCartBean(@PathVariable long customer_id) {
CustomerBean customer1 = customerService.getCustomerBeanById(customer_id);
List<ProductBean> product1 = service.getAllProductBean();
customer1.getProducts().removeAll(product1);
customerService.saveOrUpdateCustomerBean(customer1);
}
}
```
# ProductCategoryBeanController
```
package com.ctbcbank.demo.lab.controller.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
import com.ctbcbank.demo.lab.service.ProductCategoryBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/productCategory")
public class ProductCategoryBeanController {
@Autowired
private ProductCategoryBeanService service;
// 取得所有
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<ProductCategoryBean> getAllProducts() {
return service.getAllProductCategoryBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ProductCategoryBean getProductCategeryBeanById(@PathVariable long id) {
ProductCategoryBean productCategoryBean = service.getProductCategoryBeanById(id);
return productCategoryBean;
}
// 刪除指定id
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void removeProductCategeryBean(@PathVariable long id) {
System.out.println("DeleteNum=>" + id);
service.removeProductCategoryBean(id);
}
// 新增
@RequestMapping(value = "/all", method = RequestMethod.POST)
public String createProductCategery(@RequestBody ProductCategoryBean bean) {
service.saveOrUpdateProductCategoryBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ProductCategoryBean modifyProductCategory(@PathVariable("id") Long id, @RequestBody ProductCategoryBean bean) {
bean.setId(id);
return service.updateProductCategoryBean(bean);
}
}
```
# CustomerBeanRepository
```
package com.ctbcbank.demo.lab.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.CustomerBean;
public interface CustomerBeanRepository extends CrudRepository<CustomerBean, Long> {
CustomerBean getReferenceById(long id);
List<CustomerBean> findAll();
}
```
# OrderBeanRepository
```
package com.ctbcbank.demo.lab.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.OrderBean;
public interface OrderBeanRepository extends CrudRepository<OrderBean, Long> {
OrderBean getReferenceById(long id);
List<OrderBean> findAll();
}
```
# ProductBeanRepository
```
package com.ctbcbank.demo.lab.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import com.ctbcbank.demo.lab.bean.ProductBean;
public interface ProductBeanRepository extends CrudRepository<ProductBean, Long> {
// List<ProductBean> findByTitleContainingIgnoreCase(String name);
}
```
# ProductCategoryBeanRepository
```
package com.ctbcbank.demo.lab.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
public interface ProductCategoryBeanRepository extends CrudRepository<ProductCategoryBean, Long> {
ProductCategoryBean getReferenceById(long id);
List<ProductCategoryBean> findAll();
}
```
# addProductToCart
```
package com.ctbcbank.demo.lab.runner;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ctbcbank.demo.lab.bean.CustomerBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.repository.CustomerBeanRepository;
import com.ctbcbank.demo.lab.repository.ProductBeanRepository;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@Order(6)
public class addProductCart implements CommandLineRunner {
@Autowired
CustomerBeanRepository repository;
@Autowired
ProductBeanRepository PBrepository;
@Override
public void run(String... args) throws Exception {
log.info("test1");
CustomerBean c1=repository.findById(11l).get();
ProductBean p1=PBrepository.findById(1l).get();
ProductBean p2=PBrepository.findById(2l).get();
List<ProductBean> test = new ArrayList<ProductBean>();
test.add(p2);
c1.setProducts(test);
c1.getProducts().add(p1);
log.info("test2");
repository.save(c1);
}
}
```
# addProductToCategory
```
package com.ctbcbank.demo.lab.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
import com.ctbcbank.demo.lab.repository.ProductBeanRepository;
import com.ctbcbank.demo.lab.repository.ProductCategoryBeanRepository;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@Order(5)
public class addProductToCategory implements CommandLineRunner{
@Autowired
ProductCategoryBeanRepository repositoryC;
@Autowired
ProductBeanRepository repository;
@Override
public void run(String... args) throws Exception {
ProductBean product = null;
ProductBean product2 = null;
ProductBean product3 = null;
ProductBean product4 = null;
ProductBean product5 = null;
ProductBean product6 = null;
ProductBean product7 = null;
ProductBean product8 = null;
ProductCategoryBean productCategory = null;
ProductCategoryBean productCategory2 = null;
try
{
product = repository.findById(1l).get();
product2 = repository.findById(2l).get();
product3 = repository.findById(3l).get();
product4 = repository.findById(4l).get();
product5 = repository.findById(5l).get();
product6 = repository.findById(6l).get();
product7 = repository.findById(7l).get();
product8 = repository.findById(8l).get();
log.info("[V]get object sucess");
}catch(
Exception e)
{
log.info("[X]get object fail");
}
try
{
productCategory = repositoryC.findById(10l).get();
productCategory2 = repositoryC.findById(9l).get();
log.info("[V]get object sucess");
}catch(
Exception e)
{
log.info("[X]get object fail");
}
try {
product.setProductCategory(productCategory);
product2.setProductCategory(productCategory);
product3.setProductCategory(productCategory);
product4.setProductCategory(productCategory);
product5.setProductCategory(productCategory2);
product6.setProductCategory(productCategory2);
product7.setProductCategory(productCategory2);
product8.setProductCategory(productCategory2);
repository.save(product);
repository.save(product2);
repository.save(product3);
repository.save(product4);
repository.save(product5);
repository.save(product6);
repository.save(product7);
repository.save(product8);
log.info("[V]get object sucess");
}catch (Exception e) {
log.info("[X]get object fail");
}
}
}
```
# CustomerBeanRunner
```
package com.ctbcbank.demo.lab.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ctbcbank.demo.lab.bean.OrderBean;
import com.ctbcbank.demo.lab.bean.CustomerBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.repository.CustomerBeanRepository;
import com.ctbcbank.demo.lab.repository.ProductBeanRepository;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@Order(4)
public class CustomerBeanRunner implements CommandLineRunner {
@Autowired
CustomerBeanRepository repository;
@Autowired
ProductBeanRepository PBrepository;
@Override
public void run(String... args) throws Exception {
insertData();
}
private void insertData() {
CustomerBean c1 = new CustomerBean("name1","pass1");
CustomerBean c2 = new CustomerBean("name2","pass2");
CustomerBean c3 = new CustomerBean("name3","pass3");
CustomerBean[] CustomerBean = { c1, c2, c3 };
for (CustomerBean c : CustomerBean) {
repository.save(c);
}
}
}
```
# OrderBeanRunner
```
package com.ctbcbank.demo.lab.runner;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ctbcbank.demo.lab.bean.OrderBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
import com.ctbcbank.demo.lab.repository.OrderBeanRepository;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@Order(3)
public class OrderBeanRunner implements CommandLineRunner {
@Autowired
OrderBeanRepository repository;
@Override
public void run(String... args) throws Exception {
insertData();
}
private void insertData() {
ProductCategoryBean pc1 = new ProductCategoryBean();
pc1.setName("cate1");
ProductCategoryBean pc2 = new ProductCategoryBean();
pc2.setName("cate2");
ProductCategoryBean pc3 = new ProductCategoryBean();
pc3.setName("cate3");
}
}
```
# ProductBeanRunner
```
package com.ctbcbank.demo.lab.runner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ctbcbank.demo.lab.bean.OrderBean;
import com.ctbcbank.demo.lab.bean.CustomerBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
import com.ctbcbank.demo.lab.repository.OrderBeanRepository;
import com.ctbcbank.demo.lab.repository.ProductBeanRepository;
import com.ctbcbank.demo.lab.repository.ProductCategoryBeanRepository;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@Order(1)
public class ProductBeanRunner implements CommandLineRunner {
@Autowired
ProductBeanRepository repository;
@Autowired
ProductCategoryBeanRepository categoryRepository;
@Autowired
OrderBeanRepository cartRepository;
@Override
public void run(String... args) throws Exception {
insertData();
}
private void insertData() {
ProductBean p1 = new ProductBean("Iphone 14 Pro Max", 39688,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/iphone-14-pro-finish-select-202209-6-7inch-silver?wid=5120&hei=2880&fmt=p-jpg&qlt=80&.v=1663703841892", "256GB");
ProductBean p2 = new ProductBean("Iphone 14 Pro", 34900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/iphone-14-pro-finish-select-202209-6-1inch-spaceblack?wid=5120&hei=2880&fmt=p-jpg&qlt=80&.v=1663703840510", "128GB");
ProductBean p3 = new ProductBean("Iphone 14 Plus", 31900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/iphone-14-finish-select-202209-6-7inch-purple?wid=5120&hei=2880&fmt=p-jpg&qlt=80&.v=1661027938735", "256GB");
ProductBean p4 = new ProductBean("Iphone 14 ", 27900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/iphone-14-finish-select-202209-6-1inch-midnight?wid=5120&hei=2880&fmt=p-jpg&qlt=80&.v=1661026579503", "128GB");
repository.save(p1);
repository.save(p2);
repository.save(p3);
repository.save(p4);
ProductBean p5 = new ProductBean("iPad Pro", 27900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/ipad-pro-finish-select-202212-11inch-space-gray-wifi?wid=5120&hei=2880&fmt=p-jpg&qlt=95&.v=1670865951347", "128GB");
ProductBean p6 = new ProductBean("iPad Air 5", 19900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/ipad-air-finish-select-gallery-202211-space-gray-wifi?wid=5120&hei=2880&fmt=p-jpg&qlt=95&.v=1670948364698", "64GB");
ProductBean p7 = new ProductBean("iPad 10", 14900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/ipad-10th-gen-finish-select-202212-blue-wifi?wid=5120&hei=2880&fmt=p-jpg&qlt=95&.v=1670856033679", "64GB");
ProductBean p8 = new ProductBean("iPad mini", 16900,
"https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/ipad-mini-storage-select-202207-purple-wifi?wid=5120&hei=2880&fmt=p-jpg&qlt=95&.v=1670950640688", "64GB");
repository.save(p5);
repository.save(p6);
repository.save(p7);
repository.save(p8);
}
}
```
# h1 ProductCategoryBeanRunner
```
package com.ctbcbank.demo.lab.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
import com.ctbcbank.demo.lab.repository.ProductCategoryBeanRepository;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@Order(2)
@SpringBootApplication
public class ProductCategoryBeanRunner implements CommandLineRunner {
@Autowired
ProductCategoryBeanRepository repository;
@Override
public void run(String... args) throws Exception {
insertData();
}
private void insertData() {
ProductCategoryBean pc1 = new ProductCategoryBean();
pc1.setName("Ipad");
ProductCategoryBean pc2 = new ProductCategoryBean();
pc2.setName("Iphone");
// ProductCategoryBean pc3 = new ProductCategoryBean();
// pc3.setName("cate3");
repository.save(pc1);
repository.save(pc2);
// repository.save(pc3);
log.info("ProductsCategoryBean:{}",pc1);
log.info("ProductsCategoryBean:{}",pc2);
// log.info("ProductsCategoryBean:{}",pc3);
}
}
```
# CustomerBeanService
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.CustomerBean;
public interface CustomerBeanService {
List<CustomerBean> getAllCustomerBean();
CustomerBean getCustomerBeanById(long id);
CustomerBean saveOrUpdateCustomerBean(CustomerBean bean);
void removeCustomerBean(long id);
CustomerBean updateCustomerBean(CustomerBean bean);
}
```
# CustomerBeanServiceJPAImplement
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.CustomerBean;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.repository.CustomerBeanRepository;
@Service
public class CustomerBeanServiceJPAImplement implements CustomerBeanService {
@Autowired
private CustomerBeanRepository repository;
@Override
public List<CustomerBean> getAllCustomerBean() {
return repository.findAll();
}
@Override
public CustomerBean getCustomerBeanById(long id) {
return repository.getReferenceById(id);
}
@Override
public CustomerBean saveOrUpdateCustomerBean(CustomerBean bean) {
return repository.save(bean);
}
@Override
public void removeCustomerBean(long id) {
repository.deleteById(id);
}
@Override
public CustomerBean updateCustomerBean(CustomerBean bean) {
return repository.save(bean);
}
}
```
# OrderBeanService
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import com.ctbcbank.demo.lab.bean.OrderBean;
public interface OrderBeanService {
List<OrderBean> getAllOrderBean();
OrderBean getOrderBeanById(long id);
OrderBean saveOrUpdateOrderBean(OrderBean bean);
void removeOrderBean(long id);
OrderBean updateOrderBean(OrderBean bean);
void removeAllOrderBean();
}
```
# OrderBeanServiceJPAImplement
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.OrderBean;
import com.ctbcbank.demo.lab.repository.OrderBeanRepository;
@Service
public class OrderBeanServiceJPAImplement implements OrderBeanService {
@Autowired
private OrderBeanRepository repository;
@Override
public List<OrderBean> getAllOrderBean() {
return repository.findAll();
}
@Override
public OrderBean getOrderBeanById(long id) {
return repository.getReferenceById(id);
}
@Override
public OrderBean saveOrUpdateOrderBean(OrderBean bean) {
return repository.save(bean);
}
@Override
public void removeOrderBean(long id) {
repository.deleteById(id);
}
@Override
public OrderBean updateOrderBean(OrderBean bean) {
return repository.save(bean);
}
@Override
public void removeAllOrderBean() {
repository.deleteAll(getAllOrderBean());
}
}
```
# ProductBeanService
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.ProductBean;
public interface ProductBeanService {
List<ProductBean> getAllProductBean();
ProductBean getProductBeanById(long id);
ProductBean saveOrUpdateProductBean(ProductBean bean);
void removeProductBean(long id);
// ProductBean updateProductBean(ProductBean bean);
void removeAllCartBean();
}
```
# ProductBeanServiceJPAImplements
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.repository.ProductBeanRepository;
@Service
public class ProductBeanServiceJPAImplements implements ProductBeanService {
@Autowired
private ProductBeanRepository repository;
@Override
public List<ProductBean> getAllProductBean() {
return (List<ProductBean>) repository.findAll();
}
@Override
public ProductBean getProductBeanById(long id) {
return repository.findById(id).get();
}
@Override
public ProductBean saveOrUpdateProductBean(ProductBean bean) {
return repository.save(bean);
}
@Override
public void removeProductBean(long id) {
repository.deleteById(id);
}
// @Override
// public ProductBean updateProductBean(ProductBean bean) {
// return repository.save(bean);
// }
public void removeAllCartBean() {
repository.deleteAll(getAllProductBean());
}
}
```
# ProductCategoryBeanService
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.ProductBean;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
public interface ProductCategoryBeanService {
List<ProductCategoryBean> getAllProductCategoryBean();
ProductCategoryBean getProductCategoryBeanById(long id);
ProductCategoryBean saveOrUpdateProductCategoryBean(ProductCategoryBean bean);
void removeProductCategoryBean(long id);
ProductCategoryBean updateProductCategoryBean(ProductCategoryBean bean);
}
```
# ProductCategoryServiceJPAImplement
```
package com.ctbcbank.demo.lab.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.lab.bean.ProductCategoryBean;
import com.ctbcbank.demo.lab.repository.ProductCategoryBeanRepository;
@Service
public class ProductCategoryServiceJPAImplement implements ProductCategoryBeanService {
@Autowired
private ProductCategoryBeanRepository repository;
@Override
public List<ProductCategoryBean> getAllProductCategoryBean() {
return repository.findAll();
}
@Override
public ProductCategoryBean getProductCategoryBeanById(long id) {
return repository.getReferenceById(id);
}
@Override
public ProductCategoryBean saveOrUpdateProductCategoryBean(ProductCategoryBean bean) {
return repository.save(bean);
}
@Override
public void removeProductCategoryBean(long id) {
repository.deleteById(id);
}
@Override
public ProductCategoryBean updateProductCategoryBean(ProductCategoryBean bean) {
return repository.save(bean);
}
}
```