1.创建实体类模块
2.导入lombok
XML
复制代码
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
3.添加实体类
java
复制代码
package com.cx;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
/**
* @author Jiang
* @date 2025/11/7
*/
@Data
public class Order {
private Long id;
private BigDecimal totalAmount;
private Long userId;
private String nickName;
private String address;
private List<Product> productList;
}
java
复制代码
package com.cx;
import lombok.Data;
import java.math.BigDecimal;
/**
* @author Jiang
* @date 2025/11/7
*/
@Data
public class Product {
private Long id;
private BigDecimal price;
private String productName;
private int num;
}
4.在services的pom文件中导入model
XML
复制代码
<dependency>
<groupId>com.cx</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5.基本流程
6.product模块代码
java
复制代码
package com.cx.product.controller;
import com.cx.Product;
import com.cx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Jiang
* @date 2025/11/7
*/
@RestController
public class ProductController {
@Autowired
ProductService productService;
@GetMapping(value = "/productId/{id}")
public Product getProductById(@PathVariable("id") Long productId) {
return productService.getProductById(productId);
}
}
java
复制代码
package com.cx.product.service;
import com.cx.Product;
/**
* @author Jiang
* @date 2025/11/7
*/
public interface ProductService {
Product getProductById(Long productId);
}
java
复制代码
package com.cx.product.service.impl;
import com.cx.Product;
import com.cx.product.service.ProductService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
/**
* @author Jiang
* @date 2025/11/7
*/
@Service
public class ProductServiceImpl implements ProductService {
@Override
public Product getProductById(Long productId) {
Product product = new Product();
product.setId(productId);
product.setPrice(new BigDecimal("99"));
product.setProductName("苹果-" + productId);
product.setNum(11);
return product;
}
}
7.order模块代码
java
复制代码
package com.cx.order.controller;
import com.cx.Order;
import com.cx.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Jiang
* @date 2025/11/7
*/
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@GetMapping(value = "/create")
public Order createOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId) {
return orderService.createOrder(userId, productId);
}
}
java
复制代码
package com.cx.order.service;
import com.cx.Order;
/**
* @author Jiang
* @date 2025/11/7
*/
public interface OrderService {
Order createOrder(Long userId, Long productId);
}
java
复制代码
package com.cx.order.service.impl;
import com.cx.Order;
import com.cx.order.service.OrderService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
/**
* @author Jiang
* @date 2025/11/7
*/
@Service
public class OrderServiceImpl implements OrderService {
@Override
public Order createOrder(Long userId, Long productId) {
Order order = new Order();
order.setId(1L);
//TODO 总金额
order.setTotalAmount(new BigDecimal("0"));
order.setUserId(userId);
order.setNickName("张三");
order.setAddress("火星");
//TODO 远程查询商品列表
order.setProductList(null);
return order;
}
}