在这里,我们约定:
订单模块(消费者)80端口
支付模块(提供者)8001端口
建立module快速口诀:
- 建module
- 改pom
- 写yml
- 主启动
- 业务类
- 测试
1.这里我们先建立提供者支付module
cloud-provider-payment8001
2.改pom
cloud2021 com.jijie 1.0-SNAPSHOT 4.0.0 cloud-provider-payment8001org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.mybatis.spring.boot mybatis-spring-boot-startercom.alibaba druid-spring-boot-starter1.1.10 mysql mysql-connector-javaorg.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
3.改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 # mysql驱动包
url: jdbc:mysql://localhost:3306/cloud2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: jj789632145
mybatis:
mapperLocations: classpath:mapper
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
5.业务类
业务类编写
- 建表sql
- 实体类entities
- dao
- service
- controller
1)建表
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)实体类
package com.jijie.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;
}
3)dao层接口
package com.jijie.springcloud.dao;
import com.jijie.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id")Long id);
}
mapper文件
insert into payment(serial) values(#{serial}); select * from payment where id=#{id};
4)service层开发
package com.jijie.springcloud.service;
import com.jijie.springcloud.entities.Payment;
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(Long id);
}
service实现
package com.jijie.springcloud.service.impl;
import com.jijie.springcloud.dao.PaymentDao;
import com.jijie.springcloud.entities.Payment;
import com.jijie.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@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);
}
}
5)controller层开发
package com.jijie.springcloud.controller;
import com.jijie.springcloud.entities.CommentResult;
import com.jijie.springcloud.entities.Payment;
import com.jijie.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping(value = "/create")
public CommentResult create(Payment payment){
int result = paymentService.create(payment);
log.info("插入结果:"+result);
if (result > 0){
return new CommentResult(200,"插入数据库成功",result);
}
return new CommentResult(444,"插入数据库失败");
}
@GetMapping(value = "/get/{id}")
public CommentResult getPaymentById(@PathVariable("id") Long id){
Payment result = paymentService.getPaymentById(id);
log.info("查询结果:"+result);
if (result != null) {
return new CommentResult(200,"查询成功",result);
}
return new CommentResult(444,"查询失败,查询id"+id);
}
}
6)测试
打开浏览器,或者使用postman测试即可。



