微服务项目-订单模块
准备工作
参照之前的文章搭建好项目架构,接下来开始写订单模块的后端接口。接口文档在开源仓库上有,仓库地址在第一篇博客上有。
新建订单服务模块
1.创建新模块
模块继承主工程依赖依赖
2.导入相关依赖
<?xml version="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>common-service</artifactId><groupId>cuit.jiefang</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>order-service</artifactId><dependencies><!-- feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.2.RELEASE</version></dependency><!-- RocketMQ --><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.1.0</version></dependency><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>4.7.0</version></dependency><!-- Common Service --><dependency><groupId>cuit.jiefang</groupId><artifactId>common-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- Repository Service --><dependency><groupId>cuit.jiefang</groupId><artifactId>repository-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><build><resources><resource><!--指定根目录 到java文件夹 一般如下--><directory>src/main/java</directory><includes><include>**/*.yml</include><include>**/*.yaml</include><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>false</filtering></resource></resources></build>
</project>
3.基于MP 自动生成基本架构
新建一个代码生成类,自动生成代码,自动生成代码只需要修改自己的数据库用户名、用户密码、数据库表名(这个模块用 order、order-detail 表)
代码如下
package cuit.jiefang;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import java.util.Arrays;
import java.util.List;/*** @author :jiefang* @description:代码生成模块* @date :2022/4/10 11:44*/
public class Code {public static void main(String[] args) {AutoGenerator autoGenerator = new AutoGenerator();//数据源DataSourceConfig dataSourceConfig = new DataSourceConfig();dataSourceConfig.setDbType(DbType.MYSQL);dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/uushop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");dataSourceConfig.setUsername("root");dataSourceConfig.setPassword("123098");autoGenerator.setDataSource(dataSourceConfig);//全局配置GlobalConfig globalConfig = new GlobalConfig();globalConfig.setOutputDir(System.getProperty("user.dir")+"/order-service/src/main/java");globalConfig.setAuthor("jiefang");globalConfig.setOpen(false);//去掉Service的IglobalConfig.setServiceName("%sService");autoGenerator.setGlobalConfig(globalConfig);//包配置PackageConfig packageConfig = new PackageConfig();packageConfig.setParent("cuit.jiefang");packageConfig.setEntity("entity");packageConfig.setMapper("mapper");packageConfig.setService("service");packageConfig.setServiceImpl("service.impl");packageConfig.setController("controller");autoGenerator.setPackageInfo(packageConfig);//策略配置StrategyConfig strategyConfig = new StrategyConfig();strategyConfig.setInclude("order_detail","order_master");strategyConfig.setNaming(NamingStrategy.underline_to_camel);strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);strategyConfig.setEntityLombokModel(true);TableFill tableFill = new TableFill("create_time", FieldFill.INSERT);TableFill tableFill2 = new TableFill("update_time", FieldFill.INSERT_UPDATE);List<TableFill> list = Arrays.asList(tableFill,tableFill2);strategyConfig.setTableFillList(list);autoGenerator.setStrategy(strategyConfig);//启动autoGenerator.execute();}
}
4创建启动类和配置文件
启动类:
注意启动类要放在项目的外层包里面。SpringBoot 启动时会去扫描当前包及其子包内的注解。当前包和子包外的注解是扫描不到的。
package cuit.jiefang;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @author :jiefang* @description:* @date :2022/4/10 11:52*/
@MapperScan("cuit.jiefang.mapper")
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class,args);}
}
配置文件
server:port: 8083
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/uushop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghaiusername: rootpassword: 123098# 注册到微服务里面的服务明# 使用nacos 注册服务很简单 引入依赖 启动nacos 然后配置一下就行application:name: order-service
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath:cuit/jiefang/mapper/xml/*.xml
#rocketmq:
# name-server: 192.168.108.140:9876
# producer:
# group: order
5 开始写后端代码
后端代码的实现主要参照接口文档,按照前端需求,将数据从数据库里面查出来,封装成前端需要的格式进行返回。这里主要介绍一下其中较为复杂的接口,分页查询,其他接口可以参照我gitee 上开源的项目。
分页查询配置类
配置类主要就是创建一个分页对象交给Spring 容器管理。
package cuit.jiefang.configuration;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;/*** @author :jiefang* @description:* @date :2022/4/13 16:09*/
@Component
public class PaginationConfiguration {@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();}
}
配置数据库新增、修改数据的日期自动填充
package cuit.jiefang.handle;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;/*** @author :jiefang* @description:* @date :2022/4/13 15:10*/
@Component
public class FillHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);}@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);}
}
上面两个类可以放在reporsitory 模块里面,因为其他所有与数据库交互的模块都需要用到
分页查询接口实现
实现
@GetMapping("/list/{buyId}/{page}/{size}")public ResultVo list(@PathVariable("buyId")Integer id, @PathVariable("page") Integer page, @PathVariable("size")Integer size){QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("buyer_openid",id);Page<OrderMaster> page1 = new Page<>(page,size);Page<OrderMaster> resultPage = this.orderMasterService.page(page1,queryWrapper);List<OrderMaster> records = resultPage.getRecords();return ResultUtil.success(records);}
6 基于Feign 进行接口调用
Feign 的调用过程是,根据服务名在nacos 中发现相关服务。然后像调用接口一样,调用相关服务。
1.导入依赖
<!-- feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.2.RELEASE</version></dependency>
2.定义接口
package cuit.jiefang.feign;import cuit.jiefang.vo.ProductInfoVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;import java.math.BigDecimal;
//声明调用的服务
@FeignClient("product-service")
public interface ProductServiceFeign {String baseurl="/buyer/product";//请求服务的地址@GetMapping(baseurl+"/findPriceById/{id}")public BigDecimal findPriceById(@PathVariable("id") Integer id);@GetMapping(baseurl+"/findById/{id}")public ProductInfoVO findById(@PathVariable("id") Integer id);@PutMapping(baseurl+"/subStockById/{id}/{quantity}")public boolean subStockById(@PathVariable("id") Integer id, @PathVariable("quantity") Integer quantity);@PutMapping(baseurl+"/addStockById/{id}/{quantity}")public boolean addStockById(@PathVariable("id") Integer id, @PathVariable("quantity") Integer quantity);
}
服务调用
1.像调用普通服务一样 注入
@Autowiredprivate ProductServiceFeign productServiceFeign;
2.调用
ProductInfoVO productInfoVO = productServiceFeign.findById(item.getProductId());
其他模块写
其他模块也是类似与这样 模块之前的调用采用Feign。搭建模块的时候基于MP 生成项目的基本框架。然后按照接口文档一个个的完成接口。前端部分的代码也在仓库里了
这里在放一下仓库地址:需要的小伙伴自取代码
https://gitee.com/jiefang666/uushop
微服务项目-订单模块相关推荐
- 商城项目02_环境搭建、安装VAGRANT、DOCKER、MYSQL、REDIS、从0搭建各个微服务项目、数据库初始化、安装NGINX
文章目录 ①. virtualBox - 管理虚拟机 ②. vagrant - 安装虚拟机 ③. 虚拟机安装 - docker ④. docker上安装mysql ⑤. docker上安装redis ...
- SpringCloud入门总结 + 使用SpringCloud搭建微服务项目
SpringCloud 1.认识微服务 2.认识spring Cloud 3.Spring Cloud Eureka 服务发现框架 3.1认识Eureka 3.2 实战--开发并部署Eureka Se ...
- Spring Cloud 微服务项目实战 -
文章目录 微服务"三大功能,两大特性" Spring Boot & Spring Cloud Spring Cloud 组件库一览 Spring Cloud 版本 毕业版本 ...
- 使用Spring Cloud搭建微服务项目
什么是Spring Clould Spring Cloud是由Spring提供的一套能够快速搭建微服务架构程序的框架集 框架集表示Spring Cloud不是一个框架,而是很多框架的统称 Spring ...
- java微服务项目简历_Spring Cloud及微服务简介
最近在看微服务编排的东西,看到一篇入门博客,私以为不错,再次分享下:https://blog.csdn.net/w05980598/article/details/79007194 什么是微服务 微服 ...
- 2023年最新黑马程序员Java微服务项目--学成在线
正式上线Java微服务项目<学成在线> 项目对程序员的重要性 不用播妞多说了吧 更重要的是 这次是完整!实战!企业级!项目! 划重点:全新发布!正式上线! <学成在线>项目以在 ...
- Spring Cloud Alibaba微服务项目中集成Redis实现分布式事务锁实践
引言 我们知道同一个进程里面为了解决资源共享而不出现高并发的问题可以通过高并发编程解决,通过给变量添加volatile关键字实现线程间变量可见:通过synchronized关键字修饰代码块.对象或者方 ...
- IDEA集成Docker插件实现一键自动打包部署微服务项目
一. 前言 大家在自己玩微服务项目的时候,动辄十几个服务,每次修改逐一部署繁琐不说也会浪费越来越多时间,所以本篇整理通过一次性配置实现一键部署微服务,实现真正所谓的一劳永逸. 二. 配置服务器 1. ...
- 微服务项目的整合与测试
实验目的 掌握微服务项目的整合使用 掌握Swagger-UI的简单使用 练习内容 1.微服务项目整合 1.1.项目预览 1.1.1.在 https://github.com/shi469391tou/ ...
最新文章
- SWFTools PDF转换为SWF
- mysql 字符串的处理
- android--------WebView 实现缓存网页数据
- SolrCloud7.4(Jetty容器)+mysql oracle 部署与应用
- Programming C#.Classes and Objects.只读字段
- php两个数组融合,php合并两个数组的方式有哪些
- Java web文件下载断点续传
- Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处。用来指定播放器	1 2. object 标签用于包含对象,比如图像、音
- 俄罗斯方块c语言代码 vc 6.0,VC++6.0俄罗斯方块代码
- 第十三届蓝桥杯A组Python组心得分享
- php开发环境浏览器有哪些,ie内核浏览器有哪些
- 思科模拟器路由器配置
- 海外抖音推荐算法,玩转tiktok短视频内容运营
- CSDN日报20170224——《程序员该用哪种姿势来理财》
- python职业发展规划-马哥教育官网-专业Linux培训班,Python培训机构
- php汉字转换拼音的类 做了修改用mb_convert_encoding代替iconv实现编码转换
- 关于抢红包的_关于抢红包的作文600
- Uncaught Error: Syntax error, unrecognized expression: |117的js错误
- len是python的内置函数吗_len(x) 击败 x.len(),从内置函数看 Python 的设计思想(内有公号宣传,不喜勿进)...
- 网站倒计时使用服务器时间,根据服务器时间校准倒计时时间
热门文章
- win7开机黑屏怎么解决?win7开机黑屏出现reboot and select的解决方法
- 双飞燕鼠标滚轮无法在Edge里垂直滚动,只能水平滚动
- 24速算c语言实训报告ppt,C语言速算24数据结构课程设计.doc
- 目标检测YOLO实战应用案例100讲-基于深度学习的自动驾驶目标检测算法研究
- 八大(变量)基本类型
- 微信分享 传递数据 接收收据
- rpf逆向路径转发 linux,组播RPF 逆向路径转发 实验原理
- jvm 其余工作原理
- kali rtl8812 使用airodump破解wifi密码
- 用计算机提升银行业务,银行使用计算机完成客户存款的通存通兑业务在计算机应用上属于__ __...