springCould-从小白开始【1】

2023-12-18 20:43:22

目录

1.说明

2.父工程?

3.服务端?

4.消费者

5.公共模块

6.RestTemplate


1.说明??????

创建三个模块,服务者,消费者,公共api

注:spring boot和spring cloud有版本约束

2.父工程???????

约定版本号配置

注意:jdk版本,maven版本

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.18.28</lombok.version>
        <mysql.version>8.0.33</mysql.version>
        <druid.verison>1.2.16</druid.verison>
        <mybatis.spring.boot.verison>2.2.2</mybatis.spring.boot.verison>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring boot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2.1.0.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.verison}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.verison}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.服务端???????

1.创建模块

在父工程下创建子模块

注意jdk版本

2.导入pom依赖

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--引入自己的api-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

3.创建主启动类

在主包下面创建主启动类,扫描此包及其所有子包

@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class);
    }
}

4.编写yml配置文件

  • 1.修改端口
  • 2.添加mysql配置
  • 3.添加mybatis配置
server:
  port: 8001

spring:
  application:
    name: could-payment-serivce
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud
    username: root
    password: 123456


mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.xz.springcloud.entity

5.编写业务类

①dao

dao层上一定要加@Mapper

@Mapper
public interface PaymentDao {


    /**
     * 增
     *
     * @param payment
     * @return
     */
    public int create(Payment payment);

    /**
     * 查
     * @param id
     * @return
     */
    public Payment getPaymentById(@Param("id") Integer id);

    /**
     * 删除
     * @param id
     * @return
     */
    public int deletePaymentById(@Param("id") Integer id);
}

②service

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentDao paymentDao;

    @Override
    public int create(Payment payment) {

        return paymentDao.create(payment);
    }

    @Override
    public Payment getPaymentById(Integer id) {
        return paymentDao.getPaymentById(id);
    }

    @Override
    public int deletePaymentById(Integer id) {
        return  paymentDao.deletePaymentById(id);
    }
}

③controller


@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    /**
     * 增添数据
     * @param payment
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入结果:" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据成功!", result);
        } else {
            return new CommonResult(404, "插入数据失败", null);
        }
    }

    /**
     * 查询数据
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Integer id) {
        Payment paymentResult = paymentService.getPaymentById(id);
        if (paymentResult != null) {
            return new CommonResult(200,"查询成功~",paymentResult);
        }else {
            return new CommonResult(404,"查询失败",null);
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public CommonResult deleteById(@PathVariable("id") Integer id){
        int result = paymentService.deletePaymentById(id);
        if (result!=0){
            return new CommonResult(200,"删除成功",result);
        }else{
            return new CommonResult(404,"删除失败",null);
        }
    }
}

至此服务类已经完成构建

4.消费者??????

1.创建模块

在父工程下创建子模块

注意jdk版本

2.导入pom依赖

因为是消费者模块,所以不用连接数据库,只需springboot的pom文件

注:如果配置类数据库的依赖,在yml里有没有数据库的配置,启动时会报错哦~

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

3. 创建主程序类

在主包下面创建主启动类,扫描此包及其所有子包

@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class);
    }
}

4.编写yml配置文件

因为是消费者模块,所以无需任何配置,只改端口号

server:
  port: 80

5.编写业务类

①config

要引入RestTemplate,必须将其放入ioc容器中

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

②controller

引入RestTemplate,调用服务端

@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
    public static final String PAYMENT_URL = "http://localhost:8001";


    @Autowired
    private RestTemplate restTemplate;


    /**
     * 增
     *
     * @param payment
     * @return
     */
    @GetMapping("/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        log.info("进入添加功能成功~");
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    /**
     * 查
     *
     * @param id
     * @return
     */
    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Integer id) {
        log.info("进入查询功能成功");
        return restTemplate.getForObject(PAYMENT_URL + "payment/selectById/" + id, CommonResult.class);
    }
}

5.公共模块??????

1.创建模块

在父工程下创建子模块

注意jdk版本

2.导入pom依赖?

不做业务,无需引入其他依赖


   <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.0</version>
        </dependency>
    </dependencies>

3.编写公共资源

①实体类

其他模块必须引入此模块的pom依赖

注:一定要是实现序列化接口

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    private Integer id;
    private String serial;
}

②包装类

给前传返回业务信息

/**
 * 包装类
 *
 * @param <T>
 */
@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);
    }
    
}

6.RestTemplate??????

1.什么是RestTemplate

  • RestTemplate提供了多种便捷访问远程Http服务的方法
  • 是一种简单便捷的访问restful服务模板类,
  • Spring提供的用于访问Rest服务的客户端模板工具集

2.参数含义

  • url:REST请求地址
  • requestMap:请求参数
  • ResponseBean.class:HTTP响应转换被转换成的对象类型

3.使用方法?

  • 1. 注入到spring容器中
  • 2.自动导入,直接使用
@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

文章来源:https://blog.csdn.net/dfdg345/article/details/135034532
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。