前言
本篇博客主要是向大家介绍支付模块的构建及demo的练习,希望看到本篇博客的你,能有所提升,O(∩_∩)O哈哈~
一、创建支付模块
1.创建module
2.修改pom文件
<?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>com.zcw.springcloud2020508</artifactId>
<groupId>org.zcw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment-zcw</artifactId>
<dependencies>
<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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</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>
</dependency>
</dependencies>
</project>
3.修改YML文件
server:
port: 8081
spring:
application:
name: cloud-provider-payment-zcw-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/zcw20200509?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zcw.springcloud.entities # 所有entity别名类所在包
4.创建启动类:
package com.zcw.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName : PaymentMain0509
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-09 11:44
*/
@SpringBootApplication
public class PaymentMain0509 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain0509.class,args);
}
}
5.业务类创建:
- 创建sql语句:
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
- entities创建
package com.zcw.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @ClassName : Payment
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-09 14:06
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
package com.zcw.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName : CommonResult
* @Description :JSON封装体CommonResult
* @Author : Zhaocunwei
* @Date: 2020-05-09 14:09
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code,String message){
this(code,message,null);
}
}
- dao层创建
package com.zcw.springcloud.dao;
import com.zcw.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @ClassName : PaymentDao
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-09 14:16
*/
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id") Long id);
}
<?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.zcw.springcloud.dao.PaymentDao">
<resultMap id="BaseResultMap" type="com.zcw.springcloud.entities.Payment">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="serial" jdbcType="VARCHAR" property="serial" />
</resultMap>
<sql id="Base_Column_List">
id, serial
</sql>
<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>
上面这些类xml文件可以直接通过下面的工具直接逆向出来:
- service创建
package com.zcw.springcloud.service;
import com.zcw.springcloud.entities.Payment;
/**
* @ClassName : PaymentService
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-09 14:51
*/
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(Long id);
}
package com.zcw.springcloud.service.impl;
import com.zcw.springcloud.dao.PaymentDao;
import com.zcw.springcloud.entities.Payment;
import com.zcw.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @ClassName : PaymentServiceImpl
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-09 14:52
*/
@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);
}
}
- controller层创建
package com.zcw.springcloud.controller;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import com.zcw.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;
/**
* @ClassName : PaymentController
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-09 14:57
*/
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/create")
public CommonResult create(Payment payment){
int result= paymentService.create(payment);
if(result >0){
return new CommonResult(200,"插入数据库成功",result);
}
return new CommonResult(500,"失败",null);
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id")Long id ){
Payment paymentById = paymentService.getPaymentById(id);
if(paymentById !=null){
return new CommonResult(200,"成功",paymentById);
}
return new CommonResult(444,"查询失败",null);
}
}
6.测试:
7.通过小小的练习竟然出现了问题
解决bug,断点调试
- 修改了参数的请求形式
- 查询数据库