spring启动,jpa,orderform

kknvjkwl  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(352)

我想用springboot做一个订单,在那里我可以用更多的订单项目保存订单。我不知道如何实现这个服务,类,甚至thymeleaf页。任何暗示都会很棒!这是我想拍的照片

这里是我的两个实体类(为了简洁起见,没有getter和setter,还有customer)

@Entity
@Table(name = "order_item")
public class OrderItem {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id")
    private Order order;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id")
    private Product product;

    private int qty;
    private double amount;

    public OrderItem() {}

    public OrderItem(int id, Order order, Product product, int qty, double amount) {
        super();
        this.id = id;
        this.order = order;
        this.product = product;
        this.qty = qty;
        this.amount = amount;
    }

    @Entity
@Table(name="order")
public class Order {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private Date dateTime;
    private double total;
    private int paidStatus;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="customer_id")
    private Customers customer;

    @OneToMany(mappedBy="customOrder")
    private List<OrderItem> orderItems;
jv4diomz

jv4diomz1#

您只需要创建一个存储库、服务和控制器。
1.首先,让我们为模型创建存储库。

public interface CustomerRepository extends JpaRepository<Customer, Long> {}
public interface ProductRepository extends JpaRepository<Product, Long> {}
public interface OrderRepository extends JpaRepository<Order, Long> {}

2.其次,让我们创建服务层(注意:我在这里收集了所有功能作为示例。您可以将其分发到不同的层。)

public interface OrderService {
    List<Customer> findAllCustomers();
    List<Product> findAllProducts();
    List<Order> findAllOrders();
}
@Service
public class OrderServiceImpl implements OrderService {

    private final CustomerRepository customerRepository;
    private final ProductRepository productRepository;
    private final OrderRepository orderRepository;

    public OrderServiceImpl(CustomerRepository customerRepository,
                            ProductRepository productRepository,
                            OrderRepository orderRepository) {
        this.customerRepository = customerRepository;
        this.productRepository = productRepository;
        this.orderRepository = orderRepository;
    }

    @Override
    public List<Customer> findAllCustomers() {
        return customerRepository.findAll();
    }

    @Override
    public List<Product> findAllProducts() {
        return productRepository.findAll();
    }

    @Override
    public List<Order> findAllOrders() {
        return orderRepository.findAll();
    }
}

3.现在添加一个控制器层,这将回复你的网址(注意:这里有一些简单的例子,只是为了帮助您理解操作。你可以想出许多不同的解决方案。)

@Controller
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/create")
    public String createOrder(Model model) {
        model.addAttribute("customers", orderService.findAllCustomers());
        model.addAttribute("products", orderService.findAllProducts());
        model.addAttribute("order", new Order());
        return "order-form";
    }

    @PostMapping("/insert")
    public String insertOrder(Model model, Order order) {
        // Save operations ..
        return "order-view";
    }
}

4.在这里,客户和产品来自您的数据库。
“提交表单”按钮将把这里选择的实体id发送到 insertOrder 方法(您可以以类似的方式复制其他字段,我建议您检查此链接中的示例以动态复制此产品选择区域。)

<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<div>
    <form action="/order/insert" method="post" th:object="${order}">
        <p>
            <label>Select Customer</label>
        </p>
        <p>
            <select name="customer.id">
                <option th:each="customer : ${customers}"
                        th:value="${customer.id}"
                        th:text="${customer.name}">Customer Name</option>
            </select>
        </p>
        <p>
            <label>Select Product</label>
        </p>
        <p>
            <select name="orderItems[0].product.id">
                <option th:each="product : ${products}"
                        th:value="${product.id}"
                        th:text="${product.name}">Product Name</option>
            </select>
            <input type="text" name="orderItems[0].quantity" />
        </p>
        <button type="submit">Submit Form</button>
    </form>
</div>
</body>
</html>

我建议您阅读这个示例,它提供了必要的库和spring设置。

相关问题