在这里,我们约定:
订单模块(消费者)80端口
支付模块(提供者)8001端口
建立module快速口诀:
- 建module
- 改pom
- 写yml
- 主启动
- 业务类
- 测试
1.这里我们先建立提供者支付module
cloud-provider-payment8001
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>cloud2021</artifactId> <groupId>com.jijie</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
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloud2021?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: jj789632145 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.jijie.springcloud.entities # 所有Entity别名类所在包
4.主启动
手动写springboot启动类
package com.jijie.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * <p>Description: </p> * * @author jijie * @Date 2021/5/17 11:11 */ @SpringBootApplication public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
5.业务类
业务类编写
- 建表sql
- 实体类entities
- dao
- service
- controller
1)建表
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
2)实体类
package com.jijie.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; /** * <p>Description: </p> * * @author jijie * @Date 2021/5/17 11:11 */ @Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
3)dao层接口
package com.jijie.springcloud.dao; import com.jijie.springcloud.entities.Payment; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; /** * <p>Description: </p> * * @author jijie * @Date 2021/5/17 11:11 */ @Mapper public interface PaymentDao { int create(Payment payment); Payment getPaymentById(@Param("id")Long id); }
mapper文件
<?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.jijie.springcloud.dao.PaymentDao"> <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <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>
4)service层开发
package com.jijie.springcloud.service; import com.jijie.springcloud.entities.Payment; /** * <p>Description: </p> * * @author jijie * @Date 2021/5/17 11:11 */ public interface PaymentService { int create(Payment payment); Payment getPaymentById(Long id); }
service实现
package com.jijie.springcloud.service.impl; import com.jijie.springcloud.dao.PaymentDao; import com.jijie.springcloud.entities.Payment; import com.jijie.springcloud.service.PaymentService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * <p>Description: </p> * * @author jijie * @Date 2021/5/17 11:11 */ @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); } }
5)controller层开发
package com.jijie.springcloud.controller; import com.jijie.springcloud.entities.CommentResult; import com.jijie.springcloud.entities.Payment; import com.jijie.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * <p>Description: </p> * * @author jijie * @Date 2021/5/17 11:11 */ @RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/create") public CommentResult create(Payment payment){ int result = paymentService.create(payment); log.info("插入结果:"+result); if (result > 0){ return new CommentResult(200,"插入数据库成功",result); } return new CommentResult(444,"插入数据库失败"); } @GetMapping(value = "/get/{id}") public CommentResult getPaymentById(@PathVariable("id") Long id){ Payment result = paymentService.getPaymentById(id); log.info("查询结果:"+result); if (result != null) { return new CommentResult(200,"查询成功",result); } return new CommentResult(444,"查询失败,查询id"+id); } }
6)测试
打开浏览器,或者使用postman测试即可。