微服务提供者-支付Module模块构建
2021/10/9 23:39:30
本文主要是介绍微服务提供者-支付Module模块构建,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
微服务模块流程:
1.建module
右键父工程,选择New中的Module。
输入模块名称。点击【Finish】。
建好的结构如下:
2.改POM
查看父工程的POM文件,看看变化。在父POM中多了modules节点。
选择modeul中的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>ckfuture-springcloud</artifactId> <groupId>com.ckfuture.springcloud</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>
3.写YML
新建yml文件。在modul中找到main下的resources右键新建file。输入“application.yml”
application.yml配置信息为:
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/standarddb?userUnicode=true&characterEncoding=utf-8&useSSL=false username: sa password: HRBJYKJ-Battery mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities
注:如果type:com.alibab.druid.pool.DruidDataSource爆红,则在mave中重新下载一下即可。
4.主启动
在main的java右键新建Java Class。
输入名称:"com.ckfuture.springcloud.PaymentMain8001"。
修改类代码:
package com.ckfuture.springcloud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @descrption: 主启动类 * @author: CKFuture * @since: 2021-10-09 21:18 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @SpringBootApplication public class PaymentMain8001 { private static final Logger logger = LoggerFactory.getLogger(PaymentMain8001.class); public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); logger.info("------------ API Service8001 Start Running--------------"); } }
5.业务类
1)创建数据表
创建数据表payment。
2)entities
创建“entities”文件夹。分别创建Payment和CommonResult类。Payment为支付实体类。CommonResult为统一的返回类。
package com.ckfuture.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; /** * @descrption: 支付实体类 * @author: CKFuture * @since: 2021-10-09 21:35 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
package com.ckfuture.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 data; public CommonResult(Integer code,String message){ this(code,message,null); } }
3)dao
创建“dao”文件夹,并创建“PaymentDao”接口。
package com.ckfuture.springcloud.dao; import com.ckfuture.springcloud.entities.Payment; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface PaymentDao { /** * 添加 * @param payment * @return */ public int create(Payment payment); /** * 通过id查询数据 * @param id * @return */ public Payment getPaymentById(@Param("id") Long id); }
在resources中创建mapper文件夹,并创建“PaymentMapper.xml”文件,用于实现PaymentDao接口。
<?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.ckfuture.springcloud.dao.PaymentDao"> <resultMap id="BaseResultMap" type="com.ckfuture.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <insert id="create" parameterType="com.ckfuture.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from where id= #{id}; </select> </mapper>
4)service
创建“service”文件夹,并创建“PaymentService”接口。
package com.ckfuture.springcloud.service; import com.ckfuture.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; /** * @descrption: 支付服务接口 * @author: CKFuture * @since: 2021-10-09 22:26 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ public interface PaymentService { /** * 添加 * @param payment * @return */ public int create(Payment payment); /** * 通过id查询数据 * @param id * @return */ public Payment getPaymentById(@Param("id") Long id); }
创建“impl”文件夹,并创建“PaymentServiceImpl”实现类。
package com.ckfuture.springcloud.service.impl; import com.ckfuture.springcloud.dao.PaymentDao; import com.ckfuture.springcloud.entities.Payment; import com.ckfuture.springcloud.service.PaymentService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @descrption: 支付服务接口实现类 * @author: CKFuture * @since: 2021-10-09 22:26 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; public int create(Payment payment){ return paymentDao.create(payment); } public Payment getPaymentById(Long id){ return paymentDao.getPaymentById(id); } }
5)controller
新建“controller”文件夹,并创建“PaymentController”类。
package com.ckfuture.springcloud.controller; import com.ckfuture.springcloud.entities.CommonResult; import com.ckfuture.springcloud.entities.Payment; import com.ckfuture.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; @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(200,"插入数据库成功",result); }else { return new CommonResult(301,"插入数据库失败",null); } } @GetMapping(value = "/payment/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(301,"没有对应记录,查询Id:"+id,null); } } }
6.测试
启动8001服务
浏览器数据测试接口,得到查询数据。
利用postman测试插入
这篇关于微服务提供者-支付Module模块构建的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14动态路由项目实战:从入门到上手
- 2024-11-14函数组件项目实战:从入门到简单应用
- 2024-11-14获取参数项目实战:新手教程与案例分析
- 2024-11-14可视化开发项目实战:新手入门教程
- 2024-11-14可视化图表项目实战:从入门到实践
- 2024-11-14路由懒加载项目实战:新手入门教程
- 2024-11-14路由嵌套项目实战:新手入门教程
- 2024-11-14全栈低代码开发项目实战:新手入门指南
- 2024-11-14全栈项目实战:新手入门教程
- 2024-11-14useRequest教程:新手快速入门指南