二. SpringCloud基本Rest微服务工程搭建 (2)

主实体 与 Json封装体

@Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; } @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); } }

dao

接口PaymentDao与mybatis映射文件PaymentMapper

@Mapper public interface PaymentDao { int save(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.polaris.springcloud.dao.PaymentDao"> <insert parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <resultMap type="com.polaris.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id}; </select> </mapper>

service

接口与实现类

@Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; @Override public int save(Payment payment) { return paymentDao.save(payment); } @Override public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } }

controller

@RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Resource private PaymentService paymentService; @PostMapping("/save") public CommonResult save(Payment payment) { int result = paymentService.save(payment); log.info("===> result: " + result); if(result > 0) { return new CommonResult(200,"保存到数据库成功",result); } return new CommonResult(400,"保存到数据库失败",null); } @GetMapping("/get/{id}") public CommonResult<Payment> save(@PathVariable("id") Long id) { Payment paymentById = paymentService.getPaymentById(id); log.info("===> payment: " + paymentById); if(paymentById != null) { return new CommonResult(200,"查询成功",paymentById); } return new CommonResult(400,"查询失败",null); } } 3. 开启Devtools热部署 3.1 聚合父类总工程pom.xml添加配置 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build> 3.2 当前工程添加devtools依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> 3.3 Idea开启自动编译选项

image-20210124211033737

3.4 开启热注册

快捷键 ctrl + shift + alt + /,打开Registry

image-20210124230117788

勾选

image-20210124211807560

重启Idea,即可测试代码时不用手动重启

注意:该功能只能在开发阶段使用,上线前一定要关闭

4. 微服务消费者订单Module模块

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyjxf.html