上一节已经建完。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.ezerbel.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<!-- 包含了sleuth zipkin 数据链路追踪-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--<!– 引用自己定义的api通用包,可以使用Payment支付Entity –>-->
<!--<dependency>-->
<!--<groupId>com.eiletxie.springcloud</groupId>-->
<!--<artifactId>cloud-api-commons</artifactId>-->
<!--<version>${project.version}</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<!--如果没写版本,从父层面找,找到了就直接用,全局统一-->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
server:
port: 8080
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
# 防止报SSL安全加固未禁用也未指定的错误
url: jdbc:mysql://192.168.101.7:3306/cloudedu?useunicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123
mybatis:
mapperLocations: classpath:mapper/*.xml
#所有Entity别名类所在的包
type-aliases-package: com.ezerbel.springcloud.entities
package com.ezerbel.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 11:57
*/
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 12:51
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
package com.ezerbel.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 12:54
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
/**
* Json 封装体,用于规范前端交互过程
*/
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code,String message){
this(code,message,null);
}
}
package com.ezerbel.springcloud.dao;
import com.ezerbel.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 13:01
*/
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id")Long id);
}
package com.ezerbel.springcloud.service;
import com.ezerbel.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 13:28
*/
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id")Long id);
}
package com.ezerbel.springcloud.service.impl;
import com.ezerbel.springcloud.dao.PaymentDao;
import com.ezerbel.springcloud.entities.Payment;
import com.ezerbel.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 13:29
*/
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
package com.ezerbel.springcloud.controller;
import com.ezerbel.springcloud.entities.CommonResult;
import com.ezerbel.springcloud.entities.Payment;
import com.ezerbel.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @Author: EzerbelCN
* @Date: 2020/7/19 13:32
*/
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
/**
* post 表示写操作
* @param payment
* @return
*/
@PostMapping(value="/payment/create")
public CommonResult create(/*@RequestBody*/ Payment payment){
int result = paymentService.create(payment);
log.info("****插入结果:" + result);
if(result>0){
return new CommonResult(200,"插入数据成功",result);
}
else{
return new CommonResult(444,"插入数据库失败",null);
}
}
@GetMapping(value="/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
if (payment == null){
return new CommonResult(444,"查询数据失败,ID:"+id,null);
}else{
return new CommonResult(200,"得到对应数据",payment);
}
}
}
PaymentMapper.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.ezerbel.springcloud.dao.PaymentDao">
<insert id="create" parameterType="com.ezerbel.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial)values (#{serial});
</insert>
<resultMap id="BaseReultMap" type="com.ezerbel.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" resultMap="BaseReultMap" parameterType="long">
select * from payment where id = #{id}
</select>
</mapper>