创建order服务-创建项目
1.1创建order-provider服务。
1.2修改Order-Provider的pom文件。
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-order-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3修改Order-Provider的配置文件。
spring.application.name=book-order-provider
server.port=8002
#设置服务注册中心地址,指向另一个服务
eureka.client.service-url.defaultZone=http://admin:1234@192.168.41.242:5050/eureka/,http://admin:1234@192.168.41.242:5051/eureka/
#--------------db----------------
mybatis.type-aliases-package=com.book.order.pojo
mybatis.mapper-locations=classpath:com/book/order/mapper/*.xml
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book-order?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
创建order服务-添加mybaits组件
2.1创建Order-Service服务。
2.2将工具生成Pojo、Mpper、映射配置文件,添加到相关项目当中。
使用逆向工程生成Pojo、Mpper、映射配置文件,pojo放在Order-Service服务。Mpper、映射配置文件放在Order-Provider服务
创建order服务-查询订单业务
3.1在Order-Service项目中添加一个OrderService接口。
package com.book.order.service;
import com.book.order.pojo.Orders;
import javafx.scene.transform.MatrixType;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Order服务接口
*/
@RequestMapping("/order")
public interface OrderService {
//查询所有订单服务
@RequestMapping("/findAll")
public List<Orders> findAll();
//添加订单
@RequestMapping(value = "addOrder",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public Integer addOrder(@RequestBody Orders orders);
//根据订单ID查询订单
@RequestMapping(value = "/findOrderById",method = RequestMethod.GET)
public Orders findOrderById(@RequestParam("orderid")Integer orderid);
//更新订单
@RequestMapping(value = "/updateOrder",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateOrder(@RequestBody Orders orders);
}
3.2在Order-Provider服务中添加OrderServiceImpl类。
package com.book.order.service;
import com.book.order.mapper.OrdersMapper;
import com.book.order.pojo.Orders;
import com.book.order.pojo.OrdersExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
@Service
public class OrderServiceImpl {
@Autowired
private OrdersMapper ordersMapper;
//查询所有订单
public List findAll(){
OrdersExample example=new OrdersExample();
List list = this.ordersMapper.selectByExample(example);
return list;
}
//添加订单
public Integer addOrder( Orders orders){
return ordersMapper.insert(orders);
}
//根据id查询订单
public Orders findOrderById(Integer orderid){
Orders orders = ordersMapper.selectByPrimaryKey(orderid);
return orders;
}
//更新订单
public void updateOrder(Orders orders){
ordersMapper.updateByPrimaryKey(orders);
}
}
3.3在Order-Provider服务中添加一个Controller。
import java.util.List;
@RestController
public class OrderController implements OrderService {
@Autowired
private OrderServiceImpl orderServiceImpl;
@Override
public List<Orders> findAll() {
return this.orderServiceImpl.findAll();
}
@Override
public Integer addOrder(@RequestBody Orders orders) {
orderServiceImpl.addOrder(orders);
return orders.getId();
}
@Override
public Orders findOrderById(Integer orderid) {
return orderServiceImpl.findOrderById(orderid);
}
@Override
public void updateOrder(@RequestBody Orders orders) {
orderServiceImpl.updateOrder(orders);
}
}
3.4修改Order-Provider启动类。
@MapperScan("com.book.order.mapper")
@EnableEurekaClient
@SpringBootApplication
public class OrderProviderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderProviderApplication.class, args);
}
}
3.5访问服务,查看结果。
创建Consumer服务-创建项目
4.1修改pom文件添加相关坐标。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>e-book-consumer</name>
<description>e-book-consumer</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-product-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>e-book-trade-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 修改配置文件,添加服务相关配置。
spring.application.name=book-consumer-provider
server.port=9001
#设置服务注册中心地址,指向另一个服务
eureka.client.service-url.defaultZone=http://admin:1234@192.168.41.242:5050/eureka/,http://admin:1234@192.168.41.242:5051/eureka/
4.3 创建启动类。
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EBookConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EBookConsumerApplication.class, args);
}
}
创建consumer服务-搭建业务结构
5.1创建Controller,注入相关对象,定义业务方法。
package com.book.consumer.controller;
import com.book.consumer.service.ConsumerOrderService;
import com.book.consumer.service.ConsumerProductService;
import com.book.consumer.service.ConsumerTradeService;
import com.book.consumer.service.ConsumerUserService;
import com.book.order.pojo.Orders;
import com.book.product.pojo.Product;
import com.book.trade.pojo.Trade;
import com.book.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
@RestController
public class ConsumerController {
@Autowired
ConsumerOrderService orderService;
@Autowired
ConsumerProductService productService;
@Autowired
ConsumerUserService userService;
@Autowired
ConsumerTradeService tradeService;
/**
* 模拟内容: 登录 查看产品 下订单
* 1.测试登录 账号 admin admin
* 2.查看所有产品列表
* 3.选第一款产品,下订单
* 4.实现订单交易支付
* 5.查看所有的订单信息
*/
//创建订单
@RequestMapping(value = "/createOrder",method = RequestMethod.GET)
public List<Orders> createOrder(){
//1.调用登录
Integer uid = this.login();
System.out.println(uid);
//2.查询所有商品
List<Product> list = productService.findAll();
for (Product product : list) {
System.out.println(product.getName());
}
//3.添加订单
Product product = list.get(0);
Orders orders=new Orders();
Integer orderid=1011;
orders.setId(orderid);
orders.setUserId(uid);
orders.setProductId(product.getId());
orders.setPrice(product.getPrice());
orders.setDeleted((byte)0);
Integer orderId = orderService.addOrder(orders);
System.out.println(orderId);
//4.完成支付
Trade trade=new Trade();
trade.setUserId(uid);
trade.setOrderId(orderId);
trade.setPrice(orders.getPrice());
trade.setPayStatus((byte)4);
trade.setPayType((byte)4);
trade.setGatewayPayNum(new Date().getTime()+"");
trade.setGatewayPayPrice(orders.getPrice());
trade.setGatewayPayTime(new Date());
trade.setDeleted((byte)0);
tradeService.addTrade(trade);
List<Orders> all = orderService.findAll();
return all;
}
//模拟登录
public Integer login(){
String username="admin";
String password="admin";
User login = userService.login(username, password);
if(login!=null && login.getUserName().length()>0){
System.out.println("登录成功");
return login.getId();
}else {
System.out.println("登录失败");
return null;
}
}
}
5.2 创建ConsumerOrderService。
@FeignClient("book-order-provider")
public interface ConsumerOrderService extends OrderService {
}
5.3创建ConsumerUserService。
@FeignClient("book-user-provider")
public interface ConsumerUserService extends UserService {
}
5.4创建ConsumerProductService。
@FeignClient("book-product-provider")
public interface ConsumerProductService extends ProductService {
}
5.5创建ConsumerTradeService。
@FeignClient("book-trade-provider")
public interface ConsumerTradeService extends TradeService {
}
完成用户登录、查询商品业务
6.1在User-Serivce中添加用户登录接口
@RequestMapping("/user")
public interface UserService {
//用户登录
@RequestMapping(value = "/login",method = RequestMethod.GET)
public User login(@RequestParam("userName") String userName ,@RequestParam("password") String password);
}
6.2在User-Provider中添加接口实现。
@RestController
public class UserController implements UserService {
@Autowired
private UserServiceImpl userServiceImpl;
@Override
public User login(String userName, String password) {
User login = this.userServiceImpl.login(userName, password);
return login;
}
}
6.3在UserServiceImpl类中完成对数据库的操作。
@Service
public class UserServiceImpl {
@Autowired
private UserMapper userMapper;
//登录
public User login(String userName,String password){
UserExample example=new UserExample();
UserExample.Criteria criteria = example.createCriteria();
criteria.andUserNameEqualTo(userName);
criteria.andPasswordEqualTo(password);
List<User> list = this.userMapper.selectByExample(example);
if(list!=null&&list.size()>0){
return list.get(0);
}
return null;
}
}
6.4在Product-Service中添加查询所有商品接口。
@RequestMapping("/product")
public interface ProductService {
//查询所有商品
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
List<Product> findAll();
}
6.5在Product-Provider中添加接口实现。
@RestController
public class ProductController implements ProductService {
@Autowired
private ProductServiceImpl productServiceImpl;
@Override
public List<Product> findAll() {
List<Product> list = this.productServiceImpl.findProductAll();
return list;
}
}
6.6在ProductServiceImpl类中完成对数据库的操作。
@Service
public class ProductServiceImpl {
@Autowired
private ProductMapper productMapper;
//查询所有商品
public List<Product> findProductAll(){
ProductExample example = new ProductExample();
List<Product> list = this.productMapper.selectByExampleWithBLOBs(example);
return list;
}
}
完成创建订单业务
7.1在Order-Service中添加创建订单接口。
package com.book.order.service;
import com.book.order.pojo.Orders;
import javafx.scene.transform.MatrixType;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Order服务接口
*/
@RequestMapping("/order")
public interface OrderService {
//查询所有订单服务
@RequestMapping("/findAll")
public List<Orders> findAll();
//添加订单
@RequestMapping(value = "addOrder",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public Integer addOrder(@RequestBody Orders orders);
//根据订单ID查询订单
@RequestMapping(value = "/findOrderById",method = RequestMethod.GET)
public Orders findOrderById(@RequestParam("orderid")Integer orderid);
//更新订单
@RequestMapping(value = "/updateOrder",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateOrder(@RequestBody Orders orders);
}
7.2在Order-Provider中添加接口实现。
package com.book.order.controller;
import com.book.order.pojo.Orders;
import com.book.order.service.OrderService;
import com.book.order.service.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class OrderController implements OrderService {
@Autowired
private OrderServiceImpl orderServiceImpl;
@Override
public List<Orders> findAll() {
return this.orderServiceImpl.findAll();
}
@Override
public Integer addOrder(@RequestBody Orders orders) {
orderServiceImpl.addOrder(orders);
return orders.getId();
}
@Override
public Orders findOrderById(Integer orderid) {
return orderServiceImpl.findOrderById(orderid);
}
@Override
public void updateOrder(@RequestBody Orders orders) {
orderServiceImpl.updateOrder(orders);
}
}
7.3在OrderServiceImpl类中完成数据库操作。
package com.book.order.service;
import com.book.order.mapper.OrdersMapper;
import com.book.order.pojo.Orders;
import com.book.order.pojo.OrdersExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
@Service
public class OrderServiceImpl {
@Autowired
private OrdersMapper ordersMapper;
//查询所有订单
public List<Orders> findAll(){
OrdersExample example=new OrdersExample();
List<Orders> list = this.ordersMapper.selectByExample(example);
return list;
}
//添加订单
public Integer addOrder( Orders orders){
return ordersMapper.insert(orders);
}
//根据id查询订单
public Orders findOrderById(Integer orderid){
Orders orders = ordersMapper.selectByPrimaryKey(orderid);
return orders;
}
//更新订单
public void updateOrder(Orders orders){
ordersMapper.updateByPrimaryKey(orders);
}
}
完成交易信息持久化
8.1在Trade-Service中添加创建交易接口。
@RequestMapping("/trade")
public interface TradeService {
//查询支付信息
@RequestMapping("/findAll")
public List<Trade> findAll();
//添加支付信息
@RequestMapping(value = "addTrade",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public void addTrade(@RequestBody Trade trade);
}
8.2在Trade-Provider中添加接口实现。
@RestController
public class TradeController implements TradeService {
@Autowired
private TradeServiceImpl tradeServiceImpl;
@Autowired
TradeOrderService tradeOrderService;
@Override
public List<Trade> findAll() {
return this.tradeServiceImpl.findAll();
}
@Override
public void addTrade(@RequestBody Trade trade) {
tradeServiceImpl.addTrade(trade);
//根据id查询订单
Orders order = tradeOrderService.findOrderById(trade.getOrderId());
order.setTradeId(trade.getId());
//跟新订单
tradeOrderService.updateOrder(order);
}
}
8.3在TradeServiceImpl类中完成对数据库的操作。
@Service
public class TradeServiceImpl {
@Autowired
private TradeMapper tradeMapper;
public List<Trade> findAll(){
TradeExample example=new TradeExample();
return this.tradeMapper.selectByExample(example);
}
public void addTrade(Trade trade){
this.tradeMapper.insert(trade);
}
}
8.4在交易中更新订单信息。
//根据id查询订单
Orders order = tradeOrderService.findOrderById(trade.getOrderId());
order.setTradeId(trade.getId());
//跟新订单
tradeOrderService.updateOrder(order);
在交易中更新订单信息
9.1修改启动类
@EnableFeignClients
@MapperScan("com.book.trade.mapper")
@EnableEurekaClient
@SpringBootApplication
public class TradeProviderApplication {
public static void main(String[] args) {
SpringApplication.run(TradeProviderApplication.class, args);
}
}
9.2在Trade-Provider服务中添加接口
@FeignClient("book-order-provider")
public interface TradeOrderService extends OrderService {
}
9.3修改Order-Service添加方法
package com.book.order.service;
import com.book.order.pojo.Orders;
import javafx.scene.transform.MatrixType;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Order服务接口
*/
@RequestMapping("/order")
public interface OrderService {
//查询所有订单服务
@RequestMapping("/findAll")
public List<Orders> findAll();
//添加订单
@RequestMapping(value = "addOrder",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public Integer addOrder(@RequestBody Orders orders);
//根据订单ID查询订单
@RequestMapping(value = "/findOrderById",method = RequestMethod.GET)
public Orders findOrderById(@RequestParam("orderid")Integer orderid);
//更新订单
@RequestMapping(value = "/updateOrder",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateOrder(@RequestBody Orders orders);
}
9.4修改Order-Provider的OrderServiceImpl完成对数据库的操作。
package com.book.order.service;
import com.book.order.mapper.OrdersMapper;
import com.book.order.pojo.Orders;
import com.book.order.pojo.OrdersExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
@Service
public class OrderServiceImpl {
@Autowired
private OrdersMapper ordersMapper;
//查询所有订单
public List<Orders> findAll(){
OrdersExample example=new OrdersExample();
List<Orders> list = this.ordersMapper.selectByExample(example);
return list;
}
//添加订单
public Integer addOrder( Orders orders){
return ordersMapper.insert(orders);
}
//根据id查询订单
public Orders findOrderById(Integer orderid){
Orders orders = ordersMapper.selectByPrimaryKey(orderid);
return orders;
}
//更新订单
public void updateOrder(Orders orders){
ordersMapper.updateByPrimaryKey(orders);
}
}
9.5修改Order-provider的Controller。
package com.book.order.controller;
import com.book.order.pojo.Orders;
import com.book.order.service.OrderService;
import com.book.order.service.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class OrderController implements OrderService {
@Autowired
private OrderServiceImpl orderServiceImpl;
@Override
public List<Orders> findAll() {
return this.orderServiceImpl.findAll();
}
@Override
public Integer addOrder(@RequestBody Orders orders) {
orderServiceImpl.addOrder(orders);
return orders.getId();
}
@Override
public Orders findOrderById(Integer orderid) {
return orderServiceImpl.findOrderById(orderid);
}
@Override
public void updateOrder(@RequestBody Orders orders) {
orderServiceImpl.updateOrder(orders);
}
}
9.6在Trade-provider的controller中完成业务实现
package com.book.trade.controller;
import com.book.order.pojo.Orders;
import com.book.trade.pojo.Trade;
import com.book.trade.service.TradeOrderService;
import com.book.trade.service.TradeService;
import com.book.trade.service.TradeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TradeController implements TradeService {
@Autowired
private TradeServiceImpl tradeServiceImpl;
@Autowired
TradeOrderService tradeOrderService;
@Override
public List<Trade> findAll() {
return this.tradeServiceImpl.findAll();
}
@Override
public void addTrade(@RequestBody Trade trade) {
tradeServiceImpl.addTrade(trade);
//根据id查询订单
Orders order = tradeOrderService.findOrderById(trade.getOrderId());
order.setTradeId(trade.getId());
//跟新订单
tradeOrderService.updateOrder(order);
}
}