- 问题背景
- 项目搭建
- Lyric: 一只灰狼问候我谁是神枪手
从零开始学springcloud微服务项目
注意事项:
- 约定 > 配置 > 编码
- IDEA版本2021.1
- 这个项目,我分了很多篇章,每篇文章一个操作步骤,目的是显得更简单明了
- controller调service,service调dao
1 创建提供者支付微服务相关mysql表
CREATE TABLE `payment` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `serial` varchar(200) DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
2 IDEA新建entities,dao,mapper文件夹
3 在entities新建Payment,实现Serializable接口,后续做分布式架构
package com.yg.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
4 在entities中新建封装类CommonResult
package com.yg.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class CommonResult{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this(code, message, null); } }
5 在dao中新建PaymentDao接口
package com.yg.springcloud.dao;
import com.yg.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
6 在mapper中新建PaymentMapper.xml数据库映射文件
INSERT INTO payment(SERIAL) VALUES (#{serial}); SELECT * FROM payment WHERe id = #{id};
7 mysql插入一条数据
INSERT INTO `mysqltest`.`payment` (`id`, `serial`) VALUES (1, 'abc');
8 启动提供者支付module引用
9 打开浏览器,输入查询的数据库 id
http://localhost:8001/payment/get/1
10 使用postman,输入
http://localhost:8001/payment/create?serial=qaz
11 查看数据库
作为程序员第 172 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …



