1.创建实体类模块

2.导入lombok
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency></dependencies>
3.添加实体类
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;
}
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
<dependency><groupId>com.cx</groupId><artifactId>model</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>
5.基本流程


6.product模块代码
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 {@AutowiredProductService productService;@GetMapping(value = "/productId/{id}")public Product getProductById(@PathVariable("id") Long productId) {return productService.getProductById(productId);}
}
package com.cx.product.service;import com.cx.Product;/*** @author Jiang* @date 2025/11/7*/
public interface ProductService {Product getProductById(Long productId);
}
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 {@Overridepublic 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模块代码
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 {@AutowiredOrderService orderService;@GetMapping(value = "/create")public Order createOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId) {return orderService.createOrder(userId, productId);}
}
package com.cx.order.service;import com.cx.Order;/*** @author Jiang* @date 2025/11/7*/
public interface OrderService {Order createOrder(Long userId, Long productId);
}
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 {@Overridepublic 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;}
}