目录
Adding devtools to your project
约定 > 配置 > 编码
构建支付微服务模块
-
建Module
-
改POM,引入依赖
-
写yml
-
主启动类
-
支付功能业务类
-
建表语句
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
-
entity实体类
package com.tudeen.learn.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Payment implements Serializable {
private Long id;
private String serial;
}
-
common消息返回类
package com.tudeen.learn.common;
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);
}
}
-
mapper接口
package com.tudeen.learn.mapper;
import com.tudeen.learn.entity.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentMapper {
public int create(Payment payment);
public Payment getPaymentByID(@Param("id") Long id);
}
-
mapper.xml(mybatis)
<?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.tudeen.learn.mapper.PaymentMapper">
<resultMap id="BaseResultMap" type="com.tudeen.learn.entity.Payment">
<id column="id" property="id" jdbcType="BIGINT"></id>
<result column="serial" property="serial" jdbcType="VARCHAR"></result>
</resultMap>
<insert id="create" parameterType="com.tudeen.learn.entity.Payment" useGeneratedKeys="true"
keyProperty="id">
insert into payment(serial) values(#{serial})
</insert>
<select id="getPaymentByID" resultMap="BaseResultMap">
select id,serial from payment where id=#{id}
</select>
</mapper>
-
service接口
package com.tudeen.learn.service;
import com.tudeen.learn.entity.Payment;
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(Long id);
}
-
service实现类
package com.tudeen.learn.service.impl;
import com.tudeen.learn.entity.Payment;
import com.tudeen.learn.mapper.PaymentMapper;
import com.tudeen.learn.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentMapper paymentMapper;
@Override
public int create(Payment payment) {
return paymentMapper.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentMapper.getPaymentById(id);
}
}
-
controller层
package com.tudeen.learn.controller;
import com.tudeen.learn.common.CommonResult;
import com.tudeen.learn.entity.Payment;
import com.tudeen.learn.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
@Slf4j
@RequestMapping
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@PostMapping(value = "/payment/create")
public CommonResult Create(@RequestBody Payment payment) {
int i = paymentService.create(payment);
log.info("插入结果{}", i);
if (i > 0) {
return new CommonResult(200, "插入成功,serverPort:" + serverPort, i);
} else {
return new CommonResult(400, "插入失败", null);
}
}
@GetMapping(value = "/payment/getPaymentByid/{id}")
public CommonResult getPaymentByid(@PathVariable Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("获取结果{}", payment);
return new CommonResult(200, "获取成功,serverPort:" + serverPort, payment);
}
}
-
整理分层架构
如下
运行启动:
http://localhost:8001/payment/getPaymentByid/31
热部署
代码如果改动了一会希望自动重启生效,让他自动热部署
Adding devtools to your project
添加热部署Pom文件dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Adding plugin to your pom.xml
在pom配置文件中添加Maven插件
<!--cloud工程pom -->
<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>
Enabling automatic build
开启自动部署的选项
Update the value of
热部署开启,点击Ctrl + Shift + Alt + / ,勾选后重启idea 如下 :
当修改代码后会自动进行部署。