@TOC
問題背景
從零開始學(xué)springcloud微服務(wù)項(xiàng)目
注意事項(xiàng):
- 約定 > 配置 > 編碼
- IDEA版本2021.1
- 這個(gè)項(xiàng)目,我分了很多篇章,每篇文章一個(gè)操作步驟,目的是顯得更簡(jiǎn)單明了
- controller調(diào)service邓深,service調(diào)dao
- 項(xiàng)目源碼以及sentinel安裝包
項(xiàng)目搭建
1 創(chuàng)建提供者支付微服務(wù)相關(guān)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芥备,實(shí)現(xiàn)Serializable接口,后續(xù)做分布式架構(gòu)
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數(shù)據(jù)庫映射文件
<?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.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插入一條數(shù)據(jù)
INSERT INTO `mysqltest`.`payment` (`id`, `serial`) VALUES (1, 'abc');
8 啟動(dòng)提供者支付module引用
9 導(dǎo)入實(shí)現(xiàn)類和服務(wù)接口
package com.yg.springcloud.service;
import com.yg.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
/**
* @Author suolong
* @Date 2022/6/14 21:32
* @Version 2.0
*/
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
package com.yg.springcloud.service.impl;
import com.yg.springcloud.dao.PaymentDao;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @Author suolong
* @Date 2022/6/14 21:30
* @Version 2.0
*/
@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);
}
}
10 導(dǎo)入controller
package com.yg.springcloud.controller;
import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @Author suolong
* @Date 2022/6/14 21:36
* @Version 2.0
*/
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("result哈哈哈: {}", result);
if (result > 0) {
return new CommonResult(200, "插入成功", result);
}
return new CommonResult(500, "插入失敗", null);
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("查詢結(jié)果哈哈哈hahah:{}", payment);
if (payment != null) {
return new CommonResult(200, "查詢成功哈", payment);
} else {
return new CommonResult(500, "沒有對(duì)應(yīng)記錄,查詢ID: " + id, null);
}
}
}
11 打開瀏覽器舌菜,輸入查詢的數(shù)據(jù)庫 id
http://localhost:8001/payment/get/1
12 使用postman萌壳,輸入
http://localhost:8001/payment/create?serial=qaz
13 查看數(shù)據(jù)庫
14 整體目錄結(jié)構(gòu)
SpringCloud無介紹快使用,Seata處理分布式事務(wù)(二十五)
SpringCloud無介紹快使用日月,sentinel服務(wù)熔斷功能(二十四)
SpringCloud無介紹快使用袱瓮,sentinel注解@SentinelResource的基本使用(二十三)
SpringCloud無介紹快使用,sentinel熱點(diǎn)key限流與系統(tǒng)規(guī)則的基本使用(二十二)
SpringCloud無介紹快使用爱咬,sentinel熔斷降級(jí)和限流的基本使用(二十一)
SpringCloud無介紹快使用尺借,Nacos集群和Nginx代理(二十)
SpringCloud無介紹快使用,nacos配置中心的基本使用(十九)
SpringCloud無介紹快使用精拟,nacos注冊(cè)中心的基本使用(十八)
SpringCloud無介紹快使用燎斩,gateway通過微服務(wù)名實(shí)現(xiàn)動(dòng)態(tài)路由(十七)
SpringCloud無介紹快使用虱歪,gateway的基本使用(十六)
SpringCloud無介紹快使用,Ribbon負(fù)載均衡工具與OpenFeign的使用(十五)
SpringCloud無介紹快使用栅表,使用Zookeeper替換Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)(十四)
SpringCloud無介紹快使用笋鄙,服務(wù)發(fā)現(xiàn)Discovery和Eureka自我保護(hù)(十三)
SpringCloud無介紹快使用,集群cloud-provider-payment8002搭建(十二)
SpringCloud無介紹快使用怪瓶,集群Eureka服務(wù)注冊(cè)中心cloud-eureka-server7002搭建(十一)
SpringCloud無介紹快使用局装,單機(jī)Eureka服務(wù)注冊(cè)中心cloud-eureka-server7001搭建(十)
SpringCloud無介紹快使用,新建cloud-api-commons公共模塊module(九)
SpringCloud無介紹快使用劳殖,新建子module消費(fèi)者訂單模塊(八)
SpringCloud無介紹快使用,熱部署devtools配置(七)
SpringCloud無介紹快使用拨脉,子module提供者支付微服務(wù)業(yè)務(wù)開發(fā)(六)
SpringCloud無介紹快使用哆姻,新建子module提供者支付微服務(wù)yml整合和新建啟動(dòng)類(五)
SpringCloud無介紹快使用,新建子module提供者支付微服務(wù)pom整合(四)
SpringCloud無介紹快使用玫膀,springcloud父工程pom文件整理(三)
SpringCloud無介紹快使用矛缨,IDEA新建springcloud父工程(二)
SpringCloud無介紹快使用,與Spingboot之間的兼容版本選擇(一)
作為程序員第 172 篇文章帖旨,每次寫一句歌詞記錄一下箕昭,看看人生有幾首歌的時(shí)間,wahahaha ...