发布时间:2023-11-26 08:00
从零开始学springcloud微服务项目
注意事项:
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;
/**
* @Author suolong
* @Date 2022/6/14 21:13
* @Version 2.0
*/
@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;
/**
* @Author suolong
* @Date 2022/6/14 21:15
* @Version 2.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
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;
/**
* @Author suolong
* @Date 2022/6/14 21:19
* @Version 2.0
*/
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param(\"id\") Long id);
}
6 在mapper中新建PaymentMapper.xml数据库映射文件
DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >
<mapper namespace=\"com.yg.springcloud.dao.PaymentDao\">
<resultMap id=\"BaseResultMap\" type=\"com.yg.springcloud.entities.Payment\">
<id column=\"id\" property=\"id\" jdbcType=\"BIGINT\"/>
<result column=\"serial\" property=\"serial\" jdbcType=\"VARCHAR\"/>
resultMap>
<insert id=\"create\" parameterType=\"Payment\" useGeneratedKeys=\"true\" keyProperty=\"id\">
INSERT INTO payment(SERIAL)
VALUES (#{serial});
insert>
<select id=\"getPaymentById\" parameterType=\"Long\" resultMap=\"BaseResultMap\">
SELECT *
FROM payment
WHERE id = #{id};
select>
mapper>
7 mysql插入一条数据
INSERT INTO `mysqltest`.`payment` (`id`, `serial`) VALUES (1, \'abc\');
9 打开浏览器,输入查询的数据库 id
http://localhost:8001/payment/get/1
10 使用postman,输入
http://localhost:8001/payment/create?serial=qaz
作为程序员第 172 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …