springcloud的学习---支付模块提供者

    科技2024-11-23  23

    新建一个子类工程名为cloud-provider-payment8001工程目录如下: 先上pom文件和yml文件: pom.xml

    <?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>springcloud</artifactId> <groupId>com.zsp</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8001</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>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> <scope>test</scope> </dependency> </dependencies> </project>

    然后是yml文件

    server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123456 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.zsp.springcloud.entities configuration: map-underscore-to-camel-case: true

    先去建entites类: Payment.java

    package com.zsp.springcloud.entities; import lombok.Data; import java.io.Serializable; @Data public class Payment implements Serializable { private Long id; private String serial; }

    CommonResult.java

    package com.zsp.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class CommonResult<T> { private Integer code; private String message; private T date; public CommonResult(Integer code,String message){ this(code,message,null); } }

    然后是dao层: PaymentMapper.java

    package com.zsp.springcloud.mapper; import com.zsp.springcloud.entities.Payment; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Mapper @Repository public interface PaymentMapper { int create(Payment payment); Payment getPaymentById(@Param("id") Long id); }

    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.zsp.springcloud.mapper.PaymentMapper"> <insert id="create" parameterType="payment"> insert into cloud1.payment (serial) values (#{serial}) </insert> <select id="getPaymentById" resultType="payment" parameterType="long"> select * from cloud1.payment where id=#{id} </select> </mapper>

    然后是service层: PaymentService.java

    package com.zsp.springcloud.service; import com.zsp.springcloud.entities.Payment; public interface PaymentService { int create(Payment payment); Payment getPaymentById(Long id); }

    service层的实现类:

    package com.zsp.springcloud.service; import com.zsp.springcloud.entities.Payment; import com.zsp.springcloud.mapper.PaymentMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PaymentServiceImpl implements PaymentService { @Autowired PaymentMapper paymentMapper; @Override public int create(Payment payment) { return paymentMapper.create(payment); } @Override public Payment getPaymentById(Long id) { return paymentMapper.getPaymentById(id); } }

    最后是PaymentController.java,就是控制层:

    package com.zsp.springcloud.controller; import com.zsp.springcloud.entities.CommonResult; import com.zsp.springcloud.entities.Payment; import com.zsp.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Autowired PaymentService paymentService; @PostMapping(value = "/creat") public CommonResult creat(Payment payment){ int result=paymentService.create(payment); log.info("插入的数据是: "+payment); if (result>0) { return new CommonResult(200,"插入数据成功",result); } else { return new CommonResult(444,"插入数据失败",null); } } @GetMapping(value = "/get/{id}") public CommonResult 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,"查询失败",null); } } }
    Processed: 0.011, SQL: 8