SpringCloud+SpringBoot版本选型
进入官网参考文件使用当前推荐的SpringCloud版本以及参考文献内的SpringBoot版本!
项目步骤使用官网推荐的最新的SpringCloud和推荐版本SpringBoot,Maven导入Druid-SpringBoot依赖失败,折腾一晚上后放弃了(可以本地导入Druid-SpringBoot Jar包,但是当时就想整明白为什么无法导入,刚开始使用IDEA自带的Maven3.6.3 使用阿里云的仓库无法使用,后更改为Maven官方最新版3.8. 配置阿里云私服依然无法拉取(官方仓库有Druid-SpringBoot)后配置华为Maven仓库依旧不行,最后妥协使用老版本,现在只是学习,版本只要够近,低一些不重要。)
1、pom.xml 父工程pom.xml子工程pom.xml4.0.0 org.example springcloud 1.0-SNAPSHOT cloud-provider-payment8001 pom UTF-8 1.8 1.8 4.12 1.2.17 8.0.19 1.2.8 2.2.2.RELEASE Hoxton.SR1 2.1.0.RELEASE 1.3.0 org.springframework.boot spring-boot-dependencies ${spring.boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring.cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring.cloud.alibaba.version} pom import mysql mysql-connector-java ${mysql.version} com.alibaba druid ${druid.version} org.projectlombok lombok 1.18.22 org.springframework.boot spring-boot-maven-plugin 2.5.6 true true nexus-aliyun Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public true false
2、Application.yamlspringcloud org.example 1.0-SNAPSHOT 4.0.0 cloud-provider-payment8001 8 8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 com.alibaba druid-spring-boot-starter 1.2.8 org.projectlombok lombok
server:
port: 8001
spring:
application:
name: Cloud-payment-service
datasource:
username: root
password: ********
url: jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=true&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#整合Mybatis
mybatis:
type-aliases-package: com.xjun.pojo #别名
mapper-locations: classpath:mapper/*.xml
3、SpringBoot主启动类
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
4、业务类
1、建表
CREATE TABLE `payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`serial` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '支付流水号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '支付表' ROW_FORMAT = Dynamic;
2、Pojo实体层
主实体类Payment
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private long id;
private String serial;
}
Json封装类CommonResult
@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult3、(Dao/Mapper)层{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this.code = code; this.message = message; } }
接口PaymentMapper
@Mapper
public interface PaymentMapper {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
Mybatis的映射文件PaymentMapper.xml
4、服务端Service层insert into dbspringcloud.payment (serial) values (#{serial}); select * from dbspringcloud.payment where id=#{id};
接口PaymentService
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
实现类PaymentServiceImpl
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentMapper paymentMapper;
@Override
public int create(Payment payment) {
return paymentMapper.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentMapper.getPaymentById(id);
}
}
5、Controller层
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/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(444, "插入数据库失败", null);
}
}
@GetMapping("/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(444, "无对应记录,查询ID:"+id, null);
}
}
}
测试
查询测试
插入测试



