上一节父工程已经建完,本文开始新建第一个Moule——支付模块。 意在模拟用户下单跳转到支付模块(如支付宝)这样一个过程。
新建Moudle(五大步)
1.建moudle
注意:命名要规范,后面moudle变多,随意命名会分不清。我用的名字是:cloud-provider-payment8001
新建完成之后观察父工程pom文件的变化。
2.改pom
修改Moudle的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>cloud2020</artifactId> <groupId>com.li.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <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.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </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>
补充知识:spring-boot-starter-actuator——监控系统健康情况的工具
在生产环境中,需要实时或定期监控服务的可用性。Spring Boot的actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。在启动时会有显示。
3.写yml
在子Moudle的resources目录下新建application.yml
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.li.springcloud.entities
修改成自己数据库配置。
新建数据库db2019,建表payment。
CREATE TABLE payment ( id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', SERIAL VARCHAR(200) DEFAULT '00000', PRIMARY KEY(id) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET='utf8' INSERT INTO payment VALUES(01,'202011061045'); INSERT INTO payment(SERIAL) VALUES('202011061046');
4.主启动
新建主启动类,和springboot一样。注意目录关系,主启动类要建在最外面,不然访问时报:Required request body is missing
package com.li.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
5.业务类
mysql->dao->service->controller->VUE
5.1实体类entities
package com.li.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
package com.li.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** *@Author: 一条IT *@Date: 2020/11/5 11:17 **/ @Data @AllArgsConstructor @NoArgsConstructor public class CommonResult<T> { // 404 not found private Integer code; private String message; private T data; public CommonResult(Integer code, String message){ this(code,message,null); } }
补充知识1:CommenResult
作为返回结果集,无论什么业务,传给前端一个结果,类似“404 not found”。
补充知识2:Lombok:可用来帮助开发人员消除 Java 的冗长代码,尤其是对于简单的 Java 对象(POJO)。它通过注解实现这一目的。
@NoArgsConstructor:生成无参构造器;
@AllArgsConstructor:生成全参构造器
@Data:作用于类上,是以下注解的集合:@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
lombok的使用除了要引入依赖,还要安装插件,依赖已经在pom文件写好。
idea安装
eclipse安装:https://blog.csdn.net/qq_35868412/article/details/93137304
了解更多Lombok:https://www.jianshu.com/p/2543c71a8e45
5.2dao
package com.li.springcloud.dao; import org.apache.ibatis.annotations.Mapper; import com.li.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; /** * @Author: 一条IT * @Date: 2020/11/5 14:00 */ @Mapper public interface PaymentDao { // 新增订单 public int create(Payment Payment); //按照id查找订单 public Payment getPaymentById(@Param("id") Long id); }
写Mapper,mabits操作
<?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.li.springcloud.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <resultMap id="BaseResultMap" type="com.li.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"></id> <id column="serial" property="serial" jdbcType="VARCHAR"></id> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id}; </select> </mapper>
注意:parameterType不要写成parameterMap;id对应方法名,不要手敲,容易错,统统复制粘贴。
5.3service
先写接口,再写实现类。方法同样复制dao的,不要手敲。
package com.li.springcloud.service; import com.li.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; /** * @Author: 一条IT * @Date: 2020/11/5 14:20 */ public interface PaymentService { public int create(Payment Payment); public Payment getPaymentById(@Param("id") Long id); }
实现类
package com.li.springcloud.service; import com.li.springcloud.dao.PaymentDao; import com.li.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @Author: 一条IT * @Date: 2020/11/5 14:21 */ @Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; public int create(Payment Payment) { return paymentDao.create(Payment); } public Payment getPaymentById(@Param("id") Long id) { return paymentDao.getPaymentById(id); } }
5.4controller
package com.li.springcloud.controller; import com.li.springcloud.entities.CommonResult; import com.li.springcloud.entities.Payment; import com.li.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * @Author: 一条IT * @Date: 2020/11/5 14:33 */ @RestController //日志 @Slf4j public class PaymentController { // Java自带的依赖注入:import javax.annotation.Resource; @Resource private PaymentService paymentService; //尽量用新的注解 @PostMapping(value = "/payment/create") public CommonResult create(@RequestBody Payment payment) { int result = paymentService.create(payment); log.info("*****插入结果:"+result); if(result > 0) { return new CommonResult(200,"插入数据库成功: ",result); }else{ return new CommonResult(444,"插入数据库失败",null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment payment=paymentService.getPaymentById(id); log.info("********查询结果:"+payment); if (payment!=null){ return new CommonResult(200,"查询成功",payment); }else { return new CommonResult(444,"查询失败,id="+id,null); } } }
测试模块功能
启动模块
查询订单方法,因为是get请求,浏览器可测。地址栏输入:http://localhost:8001/payment/get/1
新增订单方法,因为是post请求,大部分浏览器不支持,使用postman测试。
postman需要准备的:
1. 选post请求
2. Headers中添加 key: Content-Type value:application/json
3. Body中选raw, JSON
4. 文本框中写入json参数
//
总结
构建微服务模块五大步
1.建moudle
2.改pom
3.写yml
4.主启动
5.业务类
下一节将会新建另一个模块Order模块,模拟用户下单。