1 搭建项目
1.1 父项目的搭建
1.2 子项目的搭建
2 项目展示
步骤:
1 选择maven项目作为父项目,修改为1.8之后直接点击next
2 定义父项目的名称,修改Artifct Coordinates ---> GroupId 然后点击finsh就可以啦
3 修改pom.xml
1 选择maven项目作为父项目,修改为1.8之后直接点击next
2 定义父项目的名称,修改Artifct Coordinates ---> GroupId 然后点击finsh就可以啦
3 修改pom.xml
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.ruojuangroupId>
- <artifactId>springboot-shopartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <packaging>pompackaging>
-
-
- <properties>
- <java.version>1.8java.version>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
- <spring-boot.version>2.3.2.RELEASEspring-boot.version>
- <spring-cloud.version>Hoxton.SR9spring-cloud.version>
- <spring-cloud-alibaba.version>2.2.6.RELEASEspring-cloud-alibaba.version>
- properties>
-
- <dependencyManagement>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-dependenciesartifactId>
- <version>${spring-boot.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
-
- project>
搭建一个shop-common的maven子项目
当建完这个项目时 父项目的pom.xml就会自动多出几行代码 ,这表示
关联成功
shop-common的pom.xml
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>springboot-shopartifactId>
- <groupId>com.ruojuangroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.ruojuangroupId>
- <artifactId>shop-commonartifactId>
-
-
- <dependencies>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.56version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.44version>
- dependency>
- dependencies>
-
-
-
- project>
直接选择next,然后finsh就可以了
以同样的方式创建一下三个
pom.xml
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
- <parent>
- <artifactId>springboot-shopartifactId>
- <groupId>com.ruojuangroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
-
- <modelVersion>4.0.0modelVersion>
- <artifactId>shop-orderartifactId>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>com.ruojuangroupId>
- <artifactId>shop-commonartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- dependencies>
- project>
=====================以上的基本配置完成=========================
写入实体类
Order:
- package com.ruojuan.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //import javax.persistence.GeneratedValue;
- //import javax.persistence.GenerationType;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年11月24日 15:03
- **/
- //订单
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Order {
- private Long oid;//订单id
-
- //用户
- private Integer uid;//用户id
- private String username;//用户名
-
- //商品
- private Integer pid;//商品id
- private String pname;//商品名称
- private Double pprice;//商品单价
-
- //数量
- private Integer number;//购买数量
- }
Product:
- package com.ruojuan.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //import javax.persistence.GeneratedValue;
- //import javax.persistence.GenerationType;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年11月24日 15:02
- **/
- //商品
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class Product {
- private Integer pid;//主键
-
- private String pname;//商品名称
- private Double pprice;//商品价格
- private Integer stock;//库存
- }
User:
- package com.ruojuan.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //import javax.persistence.GeneratedValue;
- //import javax.persistence.GenerationType;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年11月24日 15:02
- **/
- //用户
- @Data//不再去写set和get方法
- @NoArgsConstructor
- @AllArgsConstructor
- public class User {
- private Integer uid;//主键
- private String username;//用户名
- private String password;//密码
- private String telephone;//手机号
- }
编写aplication.yml
不要写一样的就可以啦:
shop-user:
shop-product:
shop-order:
编写controller层
shop-user:
- package com.ruojuan.shopuser.controller;
-
- import com.ruojuan.model.User;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年11月24日 15:25
- **/
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
-
- @RequestMapping("/get/{id}")
- public User get(@PathVariable("id") Integer id){
- return new User(id,"ruojuan","123456","13530303030");
- }
-
-
- }
运行:
选中右键run就可以了
这样就成功了一个
shop-product:
- package com.ruojuan.shopproduct.controller;
-
- import com.ruojuan.model.Product;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年11月24日 15:32
- **/
- @RestController
- @RequestMapping("/product")
- public class ProductController {
- @RequestMapping("/get/{pid}")
- public Product get(@PathVariable("pid") Integer pid){
- return new Product(pid,"西游记",66d,20);
- }
-
- }
shop-order:
- package com.ruojuan.shoporder.controller;
-
- import com.ruojuan.model.Order;
- import com.ruojuan.model.Product;
- import com.ruojuan.model.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年11月24日 15:23
- **/
- @RestController
- @RequestMapping("/order")
- public class OrderController {
-
- @Autowired
- private RestTemplate restTemplate;
-
- @RequestMapping("/get/{uid}/{pid}")
- public Order get(@PathVariable("uid") Integer uid,
- @PathVariable("pid") Integer pid){
- /*
- * 要在订单微服务调用 用户微服务,商品微服务,也就意味着跨项目调用
- * */
- User user = restTemplate.getForObject("http://localhost:8070/user/get/" + uid, User.class);
- Product product = restTemplate.getForObject("http://localhost:8080/product/get/" + pid, Product.class);
-
- Order order = new Order();
- order.setUsername(user.getUsername());
- order.setUid(user.getUid());
- order.setPprice(product.getPprice());
- order.setPname(product.getPname());
- order.setPid(product.getPid());
- order.setOid(System.currentTimeMillis());
- order.setNumber(product.getStock());
-
-
- return order;
-
-
- }
-
-
- }
完成~~~~~~~~~~~~~~~~~~~~~~~~~~~~