- 服务器端
YML
server:
port: 8080
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: yuanlai8
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.ximin.springcloud.entities # 所有Entity别名类所在包
主启动:PaymentMain8080
package com.ximin.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author ximin
* @date 2021-07-04 9:48
*/
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
applicaiton.xml
<?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.ximin.springcloud.dao.PaymentDao">
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values(#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.ximin.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id=#{id};
</select>
</mapper>
Entities
-Payment
package com.ximin.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author ximin
* @date 2021-07-04 10:24
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;//bigint
private String serial;//流水号
}
-CommonResult
package com.ximin.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 返回给前端的json数据串
*
* @author ximin
* @date 2021-07-04 10:31
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
//如果是一个T=(payment)返回一个payment,
// 如果是一个student返回一个student
//.....
public class CommonResult<T> {
//404 not found
private Integer code;//异常数字码
private String message;//异常详细信息
private T data;//传递用户的参数,或者说点击的请求参数
//拿来只传递异常状态信息
public CommonResult(Integer code, String message) {
this(code, message, null);
}
}
DAO
package com.ximin.springcloud.dao;
import com.ximin.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @author ximin
* @date 2021-07-04 10:37
*/
@Mapper
public interface PaymentDao {
//数据的写入和读
//写
//要么是add,create,save
int create(Payment payment);
//读
Payment getPaymentById(@Param("id") Long id);
}
Service
--接口
package com.ximin.springcloud.service;
import com.ximin.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
/**
* @author ximin
* @date 2021-07-04 11:02
*/
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") Long id);
}
--实现类
package com.ximin.springcloud.service.impl;
import com.ximin.springcloud.dao.PaymentDao;
import com.ximin.springcloud.entities.Payment;
import com.ximin.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author ximin
* @date 2021-07-04 11:05
*/
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
public int create(Payment payment){
//返回Dao层调用写入数据的结果
return paymentDao.create(payment);
}
public Payment getPaymentById(Long id){
//返回向Dao层要得的数据
return paymentDao.getPaymentById(id);
}
}
Controller
/**
* @author ximin
* @date 2021-07-04 11:18
*/
//以json方式传回数据
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
//返回数据结果传给前端
@PostMapping(value = "/payment/create")
public CommonResult create(Payment payment) {
int result = paymentService.create(payment);
log.info("*******插入结果: " + result);
if (result > 0) {
return new CommonResult(100, "插入数据库成功",result);
} else {
return new CommonResult(444, "插入数据库失败",null);
}
}
@GetMapping(value = "payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("******查询结果:"+payment);
if(payment != null){
return new CommonResult(200,"查询成功,返回数据给你 "+payment);
}else{
return new CommonResult(444,"查询失败,没有对应记录。数据不给了,你查询的id是 :"+id,null);
}
}
}
2.用户
yml
server:
port: 9887
config
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
import javax.annotation.Resource;
@Slf4j
@RestController
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(@RequestBody Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
}
}