开始之前创建微服务Cloud整体聚合工程Project
步骤:
<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">
<modelVersion>4.0.0modelVersion>
<groupId>cn.kitey.springc;oudgroupId>
<artifactId>demo01_cloudartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>cloud-provider-payment8001module>
<module>cloud-provider-payment-8001module>
modules>
<name>Mavenname>
<url>http://maven.apache.org/url>
<inceptionYear>2001inceptionYear>
<distributionManagement>
<site>
<id>websiteid>
<url>scp://webhost.company.com/www/websiteurl>
site>
distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>12maven.compiler.source>
<maven.compiler.target>12maven.compiler.target>
<junit.version>4.12junit.version>
<lombok.version>1.18.10lombok.version>
<log4j.version>1.2.17log4j.version>
<mysql.version>8.0.18mysql.version>
<druid.version>1.1.16druid.version>
<mybatis.spring.boot.version>2.1.1mybatis.spring.boot.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-project-info-reports-pluginartifactId>
<version>3.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.spring.boot.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-site-pluginartifactId>
<version>3.7.1version>
plugin>
<plugin>
<artifactId>maven-project-info-reports-pluginartifactId>
<version>3.0.0version>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-site-pluginartifactId>
<configuration>
<locales>en,frlocales>
configuration>
plugin>
plugins>
build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-pluginartifactId>
plugin>
plugins>
reporting>
project>
因为父类工程使用了dependencyManagement 所以有些可以进行省略
<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>demo01_cloudartifactId>
<groupId>cn.kitey.springc;oudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-provider-payment-8001artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zipkinartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.20version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
1.设置数据源:datasource
2.mybatis扫面的mapper文件和扫面的实体类包名
3.配置了eureka的注册
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源的操作类型
driver-class-name: com.mysql.jdbc.Driver #mysql驱动包
url: jdbc:mysql:///db2020?characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 25002500
jpa:
show-sql: true
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.kitey.springcloud.entities #所有的Entity所在的包
eureka:
client:
register-with-eureka: false # 是否注册自己的信息到EurekaServer,默认是true
fetch-registry: false # 是否拉取其它服务的信息,默认是true
service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。
defaultZone: http://172.30.242.7:8001/eureka
程序启动的入口
package cn.kitey.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
CREATE TABLE `payment`(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`serial` VARCHAR(200) NOT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
这里创建实体类使用了lombok
@Data //它包含getter、setter、NoArgsConstructor注解,即当使用当前注解时,会自动生成包含的所有方法;
@AllArgsConstructor //全参构造
@NoArgsConstructor //无参构造
数据库对应的实体类
package cn.kitey.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 实体类
*/
@Data //它包含getter、setter、NoArgsConstructor注解,即当使用当前注解时,会自动生成包含的所有方法;
@AllArgsConstructor //全参构造
@NoArgsConstructor //无参构造
public class Payment implements Serializable {
private Long id;
private String serial;
}
返回给前端的json的实体类
package cn.kitey.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 信息返回给前端人员
* @param
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
//返回的404 500 等
private Integer code;
//返回的信息 例如错误信息
private String message;
private T data;
public CommonResult(Integer code, String message){
this.code =code;
this.message =message;
}
}
这里最好使用mapper注解
我们这里使用的配置文件方式进行sql语句的书写
package cn.kitey.springcloud.dao;
import cn.kitey.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* dao持久层
*/
@Mapper
public interface PaymentDao {
/**
* 写操作
* @param payment
* @return
*/
public int create(Payment payment);
/**
* 读操作
* @param id
* @return
*/
public Payment getPaymentById(@Param("id") Long id);
}
创建后,在mapper包创建PaymentMapper.xml
创建执行的sql语句
<mapper namespace="cn.kitey.springcloud.dao.PaymentDao">
<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>
<resultMap id="BaseResultMap" type="cn.kitey.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT">id>
<id column="serial" property="serial" jdbcType="VARCHAR">id>
resultMap>
mapper>
首先创建service接口
注意添加注解mapper
package cn.kitey.springcloud.service;
import cn.kitey.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentSrevice{
/**
* 写操作
* @param payment
* @return
*/
public int create(Payment payment);
/**
* 读操作
* @param id
* @return
*/
public Payment getPaymentById(@Param("id") Long id);
}
创建service实现类
注意添加注解service
package cn.kitey.springcloud.service.impl;
import cn.kitey.springcloud.dao.PaymentDao;
import cn.kitey.springcloud.entities.Payment;
import cn.kitey.springcloud.service.PaymentSrevice;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PaymentServiceImpl implements PaymentSrevice {
@Resource
PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
我们这里使用的
@Slf4j //打印日志
@Resource :数据的自动注入
package cn.kitey.springcloud.controller;
import cn.kitey.springcloud.entities.CommonResult;
import cn.kitey.springcloud.entities.Payment;
import cn.kitey.springcloud.service.impl.PaymentServiceImpl;
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 PaymentServiceImpl paymentService;
/**
* 数据的插入
* @param payment
* @return
*/
@PostMapping("/payment/create")
public CommonResult create(Payment payment){
int result = paymentService.create(payment);
log.info("******插入结果" + result);
if(result > 0){
return new CommonResult(result, "200", "插入数据成功!");
}else{
return new CommonResult(444,"插入数据失败!", null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") long id){
Payment paymentById = paymentService.getPaymentById( id);
log.info("*****查询数据的结果:" +paymentById);
if(paymentById !=null){
return new CommonResult(200,"查询数据成功!",paymentById);
}else{
return new CommonResult(444,"查询数据失败,你查询的id:" + id,null);
}
}
}
我们使用Postman软件进行测试
首先发送get请求进行数据的查询:
http://localhost:8001/payment/get/1
发送post请求进行数据存储:
http://localhost:8001/payment/create?serial=下页小风筝
请求存储数据serial 小风筝
然后我们看一下数据内容:
以上就是支付Moudle的创建
加油。感觉SpringCloud挺有意思的。