org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
com.alibaba
druid-spring-boot-starter
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包
@SpringBootApplication
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
2.6建库建表
-- create database springcloud_db
-- use springcloud_db;
CREATE TABLE `t_payment`(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`flow_number` varchar(32),
PRIMARY KEY(`id`)
) ENGINE=INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = UTF8;
package cn.bdqn.domain;
import java.io.Serializable;
public class Payment implements Serializable {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFlowNumber() {
return flowNumber;
}
public void setFlowNumber(String flowNumber) {
this.flowNumber = flowNumber;
}
private String flowNumber;
@Override
public String toString() {
return "Payment{" +
"id=" + id +
", flowNumber='" + flowNumber + '\'' +
'}';
}
}
@Mapper
public interface PaymentMapper {
//保存一个支付流水
public void insert(Payment payment);
//根据id获取具体的支付信息
public Payment selectById(@Param("id") Integer id);
}
insert into t_payment (flow_number) values(#{flowNumber});
public interface PaymentService {
//保存一个支付流水
public void save(Payment payment);
//根据id获取具体的支付信息
public Payment queryById(Integer id);
}
@Service
public class PaymentServiceImpl implements PaymentService{
@Autowired
private PaymentMapper paymentMapper;
@Transactional(propagation = Propagation.REQUIRED)
public void save(Payment payment) {
paymentMapper.insert(payment);
}
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public Payment queryById(Integer id) {
return paymentMapper.selectById(id);
}
}
public class ResponseResult {
//响应的编码
private Integer code;
//响应给前段用户的消息提示
private String message;
//响应的数据
private T data;
public ResponseResult() {
}
public ResponseResult(Integer code, String message) {
this.code = code;
this.message = message;
}
public void setCode(Integer code) {
this.code = code;
}
public void setMessage(String message) {
this.message = message;
}
public void setData(T data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
@Autowired
private PaymentService paymentService;
@GetMapping("/payment/get/{id}")
public ResponseResult queryById(@PathVariable(name="id") Integer id){
Payment payment=paymentService.queryById(id);
if(payment!=null){
return new ResponseResult(200, "成功",payment);
}else{
return new ResponseResult(404,"没有对应记录,查询ID:"+id, null);
}
}
@GetMapping("/payment/save")
public ResponseResult save(Payment payment){
try {
paymentService.save(payment);
return new ResponseResult(200,"成功",null);
}catch (Exception e){
e.printStackTrace();
return new ResponseResult(500,"失败",null);
}
}