Version: Next

支付模块构建

构建两个微服务模块:消费者订单模块支付模块

  • 消费者订单模块 调用 支付模块
  • 微服务提供者支付Module模块—— cloud-provider-payment8001
  • 微服务消费者订单Module模块—— cloud-consumer-order80

热部署 Devtools

构建微服务模块

  1. 创建 module
  2. 改 pom
  3. 写 yaml 配置
  4. 主启动
  5. 业务类

新建 module ,命名为 cloud-provider-payment8001

  • 创建完毕后,查看父工程的 pom ,会发现多了 modules 标签,其中有一个 module 标签,内容为刚刚创建的子模块

    <modules>
    <module>cloud-provider-payment8001</module>
    </modules>

pom

子模块 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">
<parent>
<artifactId>cloud2021</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- g、v 都继承-->
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
<!--如果没写版本,从父层面找,找到了就直接用,全局统一-->
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>compile</scope>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

application.yaml

  • 服务端口号
  • 服务名称:cloud-payment-service
  • 数据源
  • Mybatis:mapper 文件路径
注意

这里端口我自己写错了,把 8001 写成了 8081

  • 后面全都写成 8081 了,所以干脆将错就错
server:
port: 8081
spring:
application:
name: cloud-payment-service
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true #驼峰映射
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #调试打印SQL

主启动

新建类:com.bsx.springcloud.PaymentMain8001

  • 添加 @SpringBootApplication 注解
  • main 方法,SpringApplication.run(类.class, args)
主启动类
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}

业务类

  1. 建数据库表
  2. entities
  3. dao
  4. service
  5. controller

建表 payment

CREATE TABLE `payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`serial` varchar(200) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

实体类 entities.Payment

实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
private long id;
private String serial;
}

实体类 entities.CommonResult —— 提供给前端的统一响应

  • 状态码
  • 信息
CommonResult
/**
* 统一响应
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
private Integer code; // 状态码
private String message; // 信息
private T data; // 数据实体
public CommonResult(Integer code, String message) {
// 空数据的构造方法
this(code, message, null);
}
}

dao.PaymentDao 接口

持久层接口
@Mapper
public interface PaymentDao {
int create(Payment payment); // 添加一条payment记录
Payment getPaymentById(@Param("id") Long id); // 根据id查询payment
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bsx.springcloud.dao.PaymentDao">
<insert id="create" parameterType="com.bsx.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id">
INSERT INTO payment (id, serial) VALUES (NULL, #{serial});
</insert>
<select id="getPaymentById" resultMap="BaseResultMap" parameterType="Long">
SELECT * FROM payment WHERE id = #{id}
</select>
<resultMap id="BaseResultMap" type="com.bsx.springcloud.entities.Payment">
<id column="id" property="id"/>
<result column="serial" property="serial"/>
</resultMap>
</mapper>

service.PaymentService 接口

service 接口
public interface PaymentService {
int create(Payment payment); // 添加一条payment记录
Payment getPaymentById(@Param("id") Long id); // 根据id查询payment
}

实现类

service 实现类
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}

Controller

PaymentController
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/payment/create")
public CommonResult<Integer> create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("=====插入结果:" + result);
return result > 0 ? new CommonResult<>(200, "插入成功", result)
: new CommonResult<>(444, "插入失败", null);
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
return payment != null ? new CommonResult<>(200, "查询成功", payment) :
new CommonResult<>(444, "查询失败", null);
}
}

自己用 PostMan 测一下