Springcloud_H版-alibaba学习笔记(二) 支付模块

顺序:
1.建module
2.改pom
3.写yml
4.编写主启动类
5.业务类

1.工程下创建支付模块

在这里插入图片描述

在这里插入图片描述

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>dt-SpringCloud-2020</artifactId>  
        <groupId>com.dt.springcloud</groupId>  
        <version>1.0.0-SNAPSHOT</version>  
    </parent>  
    <modelVersion>4.0.0</modelVersion>  
    <!-- 模块ID-->  
    <artifactId>cloud-provider-payment8001</artifactId>  
  
    <properties>  
        <maven.compiler.source>8</maven.compiler.source>  
        <maven.compiler.target>8</maven.compiler.target>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
  
    <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>com.alibaba</groupId>  
            <artifactId>druid-spring-boot-starter</artifactId>  
            <version>1.1.10</version>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-jdbc</artifactId>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-devtools</artifactId>  
            <scope>runtime</scope>  
            <optional>true</optional>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->  
        <dependency>  
            <groupId>org.projectlombok</groupId>  
            <artifactId>lombok</artifactId>  
            <optional>true</optional>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-test</artifactId>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>  
</project>

3.resource目录下新application.yml

driver-class-name说明:
版本5.1.47以前可以使用 : org.gjt.mm.mysql.Driver
版本6-以下可以使用 : com.mysql.jdbc.Driver
版本6+以上可以使用 : com.mysql.cj.jdbc.Driver

server:  
  port: 8001  #服务端口  
  
spring:  
  application:  
    name: cloud-payment-service  #服务名  
  
  ## org.gjt.mm.mysql.Driver 版本5.1.47以前可以使用  
  ## com.mysql.jdbc.Driver 版本6-以下可以使用  
  ## com.mysql.cj.jdbc.Driver 版本6+以上可以使用  
  datasource:  
    type: com.alibaba.druid.pool.DruidDataSource    #当前数据源类型  
    driver-class-name: com.mysql.cj.jdbc.Driver  #数据库驱动包  
    url: jdbc:mysql://192.168.28.xx:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false  
    username: root  
    password: xxxxx
  
  devtools:  
    restart:  
      enabled: true #是否支持热部署  
  
mybatis:  
  # 用于将配置路径下的*.xml文件加载到mybatis中  
  mapper-locations: classpath:mapper/*.xml  
  #所有Entity别名类所在包
  type-aliases-package: com.dt.springcloud.entities  
  

4.新建启动类 PaymentMain8001

package com.dt.springcloud;  
  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.context.annotation.Configuration;  
  
  
/**  
 * @ClassName: PaymentMain8001 * @description: PaymentMain8001主启动类  
 * @author: wenbo  
 **/@SpringBootApplication  
public class PaymentMain8001 {  
    public static void main(String[] args) {  
        SpringApplication.run(PaymentMain8001.class, args);  
    }  
}

5.编写业务

顺序:
建表->entities->dao->service->controller

1.创建数据库

CREATE DATABASE /*!32312 IF NOT EXISTS*/`springcloud` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */;USE `springcloud`;/*Table structure for table `payment` */DROP TABLE IF EXISTS `payment`;CREATE TABLE `payment` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `serial` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;/*Data for the table `payment` */insert  into `payment`(`id`,`serial`) values (1,'尚硅谷'),(2,'alibaba'),(3,'京东'),(4,'头条');

2.创建实体类Entities

Payment实体类

@Data 由于引入了loombook包, 省去了get/set方法;
@AllArgsConstructor 就是用这个注解修饰的类会把里面的变量都生成全参数的构造器
@NoArgsConstructor 生成无参构造器

package com.dt.springcloud.entities;  
  
import lombok.AllArgsConstructor;  
import lombok.Data;  
import lombok.NoArgsConstructor;  
  
import java.io.Serializable;  
  
/**  
 * Payment实体类  
 *  Data 由于引入了loombook包, 省去了get/set方法;  
 *  AllArgsConstructor 就是用这个注解修饰的类会把里面的变量都生成全参数的构造器  
 *  NoArgsConstructor 生成无参构造器  
 */  
@Data  
@AllArgsConstructor  
@NoArgsConstructor  
public class Payment implements Serializable {  
    private Long id ;  
    private String serial;  
}
通用返回前端的实体
package com.dt.springcloud.entities;  
  
import lombok.AllArgsConstructor;  
import lombok.Data;  
import lombok.NoArgsConstructor;  
  
/**  
 * 通用返回前端的实体  
 *  
 * @param <T>  
 */  
@Data  
@AllArgsConstructor  
@NoArgsConstructor  
public class CommonResult<T> {  
    private Integer code;  
    private String message;  
    private T data;  
  
    public CommonResult(Integer code , String message)  
    {  
        this(code, message,null);  
    }  
}

3.Dao层开发

1.编写Dao
package com.dt.springcloud.dao;  
  
import com.dt.springcloud.entities.Payment;  
import org.apache.ibatis.annotations.Mapper;  
import org.apache.ibatis.annotations.Param;  
  
/**  
 * PaymentDao * */@Mapper  
public interface PaymentDao {  
    public int create(Payment payment);  
    public Payment getPaymentById(@Param("id") Long id);  
  
}
2.编写PaymentMapper
<?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对应哪个接口 -->  
<mapper namespace="com.dt.springcloud.dao.PaymentDao">  
  
    <!--resultMap 实体类映射 -->  
    <resultMap id="BaseResultMap" type="com.dt.springcloud.entities.Payment">  
        <id column="id" property="id" jdbcType="BIGINT"></id>  
        <id column="serial" property="serial" jdbcType="VARCHAR"></id>  
    </resultMap>  
  
  
    <insert id="create" parameterType="com.dt.springcloud.entities.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>

4.Service层开发

1.Service接口
package com.dt.springcloud.service;  
  
import com.dt.springcloud.entities.Payment;  
import org.apache.ibatis.annotations.Param;  
import org.springframework.stereotype.Service;  
  
/**  
 * 支付PaymentService接口  
 */  
  
public interface PaymentService  {  
    public int create(Payment payment);  
    public Payment getPaymentById(Long id);  
}
2.Service实现类
package com.dt.springcloud.service.impl;  
  
import com.dt.springcloud.dao.PaymentDao;  
import com.dt.springcloud.entities.Payment;  
import com.dt.springcloud.service.PaymentService;  
import org.apache.ibatis.annotations.Param;  
import org.springframework.stereotype.Service;  
  
import javax.annotation.Resource;  
  
/***  
 * 支付PaymentService 实现类  
 */  
@Service  
public class PaymentServiceImpl implements PaymentService {  
    @Resource  
    private PaymentDao paymentDao;  
  
    public int create(Payment payment) {  
        return this.paymentDao.create(payment);  
    }  
  
    public Payment getPaymentById(Long id) {  
  
        return this.paymentDao.getPaymentById(id);  
    }  
  
}

5.Controller开发

package com.dt.springcloud.controller;  
  
import com.dt.springcloud.entities.CommonResult;  
import com.dt.springcloud.entities.Payment;  
import com.dt.springcloud.service.PaymentService;  
import lombok.extern.slf4j.Slf4j;  
import org.apache.ibatis.annotations.Param;  
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;  
  
/**  
 * PaymentController * slf4j:日志注解  
 */  
@RestController  
@Slf4j  
public class PaymentController {  
  
    @Resource  
    private PaymentService paymentService;  
  
    /**  
     * 保存  
     *  
     * @param payment  
     * @return CommonResult 统一返回格式  
     */  
    @PostMapping(value = "/payment/create")  
    public CommonResult create(Payment payment) {  
        int result = this.paymentService.create(payment);  
        log.info("插入结果为:" + result);  
        if (result > 0) {  
            return new CommonResult(200, "插入数据库成功", result);  
        } else {  
            return new CommonResult(444, "插入数据库成功", null);  
        }  
    }  
  
    /**  
     * 查询  
     *  
     * @param id  
     * @return CommonResult 统一返回格式  
     */  
    @GetMapping(value = "/payment/get/{id}")  
    public CommonResult getPaymentById(@PathVariable("id") Long id) {  
        Payment payment = this.paymentService.getPaymentById(id);  
        log.info("插入结果为:" + payment);  
        if (payment != null) {  
            return new CommonResult(200, "查询数据库成功", payment);  
        } else {  
            return new CommonResult(444, "查询数据库成功", null);  
        }  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值