Java Spring Boot,JPA,订单

Java Spring Boot,JPA,订单,java,spring,spring-boot,spring-data-jpa,thymeleaf,Java,Spring,Spring Boot,Spring Data Jpa,Thymeleaf,我想用spring boot制作一个订单,在这里我可以用更多的订单项保存订单。 我不知道如何实现这个服务、类,甚至是thymeleaf页面。 任何暗示都很好! 这是我想拍的照片 这是我的两个实体类(没有getter和setter,为了简洁起见,还有customer) @实体 @表(name=“订单项目”) 公共类OrderItem{ @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) 私有int-id; @manytone(fetch=FetchT

我想用spring boot制作一个订单,在这里我可以用更多的订单项保存订单。 我不知道如何实现这个服务、类,甚至是thymeleaf页面。 任何暗示都很好! 这是我想拍的照片

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

@实体
@表(name=“订单项目”)
公共类OrderItem{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私有int-id;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“订单号”)
私人秩序;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“产品标识”)
私人产品;
私人整数数量;
私人双倍金额;
public OrderItem(){}
公共订单项(整数id、订单、产品、整数数量、双倍金额){
超级();
this.id=id;
这个。顺序=顺序;
本产品=产品;
该数量=数量;
这个。金额=金额;
}
@实体
@表(name=“order”)
公共阶级秩序{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私有int-id;
私人日期时间;
私人双总;
私人内部支付状态;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“客户id”)
私人客户;
@OneToMany(mappedBy=“customOrder”)
私人物品清单;

您只需创建一个存储库、服务和控制器

1.首先,让我们为模型创建存储库。

public interface CustomerRepository extends JpaRepository<Customer, Long> {}
4.这里,客户和产品来自您的数据库。

public interface CustomerRepository extends JpaRepository<Customer, Long> {}
“提交表单”按钮将把此处选择的实体id发送到
insertOrder
方法。(您可以用类似的方式复制其他字段,我建议您检查中的示例以动态复制此产品选择区域。)



选择客户

客户名称

精选产品

品名

提交表格

我建议您这样做,因为它可以进行必要的库和spring设置。

非常感谢您!它真的很有帮助!
public interface OrderRepository extends JpaRepository<Order, Long> {}
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();
    }
}
@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";
    }
}
<!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>