(1)一路next
(2)一路next
(3)一路next
(4)字符编码集配置
(5)注解生效激活配置
(6)java编译版本选择8
(7)删除父工程的src文件夹
(8)编写pom文件
<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>com.wsh.springcloudgroupId>
<artifactId>demo20220821artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.16.18lombok.version>
<mysql.version>5.1.47mysql.version>
<druid.version>1.1.16druid.version>
<mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
properties>
<dependencyManagement>
<dependencies>
<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>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<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>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
<optional>trueoptional>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
<addResources>trueaddResources>
configuration>
plugin>
plugins>
build>
project>
微服务模块搭建步骤
(1)创建模块
(2)编写pom文件
(3)编写yml文件
(4)编写启动类
(5)依次编写dao、service、controller
(1)在父工程新建子模块
(2)
(3)创建完子模块后,查看父工程的pom文件,发现多出子模块信息
(4)编写pom文件
<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>demo20220821artifactId>
<groupId>com.wsh.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-provider-payment8001artifactId>
<dependencies>
<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>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
(5)编写application.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
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: wsh666
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.wsh.springcloud.entity
(6)创建启动类
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
(7)创建数据库
(8)创建实体类Payment
@Data
@EqualsAndHashCode
public class Payment {
private Long id;
private String serial;
}
(9)创建消息响应体CommonResult
@Data
@EqualsAndHashCode
@AllArgsConstructor
public class CommonResult<T> {
private Integer code;
private String msg;
private T data;
}
(10)创建dao接口及对应的mapper文件
@Mapper
public interface PaymentDao {
public int insert(Payment payment);
public Payment selectById(@Param("id") Long id);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wsh.springcloud.dao.PaymentDao">
<insert id="insert" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values (#{serial});
insert>
<resultMap id="PaymentResultMap" type="Payment">
<id column="id" property="id"/>
<id column="serial" property="serial"/>
resultMap>
<select id="selectById" parameterType="Long" resultMap="PaymentResultMap">
select * from payment where id=#{id};
select>
mapper>
(11)编写service接口及其实现类
public interface PaymentService {
public int insert(Payment payment);
public Payment selectById(Long id);
}
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentDao paymentDao;
@Override
public int insert(Payment payment){
return this.paymentDao.insert(payment);
}
@Override
public Payment selectById(Long id){
return this.paymentDao.selectById(id);
}
}
(12)编写controller
@Slf4j
@RequestMapping("/payment")
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService;
@RequestMapping(value = "", method = RequestMethod.POST)
public CommonResult<Integer> insert(Payment payment){
int i = paymentService.insert(payment);
log.info(String.valueOf(i));
return new CommonResult<>(200, "插入完毕", i);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CommonResult<Payment> selectById(@PathVariable("id") Long id){
Payment payment = paymentService.selectById(id);
if (payment != null){
log.info(payment.toString());
}
return new CommonResult<>(200, "查询完毕", payment);
}
}
(13)测试
(1)
(2)
(3)编写pom文件
<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>demo20220821artifactId>
<groupId>com.wsh.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-consumer-order80artifactId>
<dependencies>
<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.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
(4)编写application.yml文件
server:
port: 80
(5)创建启动类
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
(6)编写配置类,创建RestTemplate实例并将其加入IOC容器中
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
(7)创建实体类Payment
@Data
@EqualsAndHashCode
public class Payment {
private Long id;
private String serial;
}
(8)创建消息响应体CommonResult
@Data
@EqualsAndHashCode
@AllArgsConstructor
public class CommonResult<T> {
private Integer code;
private String msg;
private T data;
}
(9)编写controller
@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
private static final String URL_PREFIX = "http://localhost:8001";
@RequestMapping(value = "", method = RequestMethod.POST)
public CommonResult<Integer> insert(Payment payment){
return restTemplate.postForObject(URL_PREFIX + "/payment", payment, CommonResult.class);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CommonResult<Payment> selectById(@PathVariable("id") Long id){
return restTemplate.getForObject(URL_PREFIX + "/payment/" + id, CommonResult.class);
}
}
(10)测试
上述两个子模块均使用到实体类Payment及消息响应体CommonResult,故我们可以新建一个子模块并将这两个类转移到该通用模块中。
(1)
(2)编写pom文件
<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>demo20220821artifactId>
<groupId>com.wsh.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-api-commonartifactId>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.8.1version>
dependency>
dependencies>
project>
(3)删除两个子模块的Payment及CommonResult类,将其创建在通用模块中
(4)另外两个子模块在pom添加该通用模块依赖