应用平台 APPSTAGE-构建Spring Cloud工程:创建子工程ServiceA

时间:2024-10-21 10:40:12

创建子工程ServiceA

  1. 创建Maven工程。

    图2 创建Maven工程

  2. 新建src目录。

    图3 新建src目录

  3. 编写业务代码。

    图4 业务代码文件
    1. 编写启动类
      package com.huawei.demo.servicea;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
      
      /**
       * 启动类
       *
       * @author XXX
       * @since 2023-12-05
       */
      @SpringBootApplication
      @EnableEurekaClient
      public class ServiceASpringbootApplication {
          public static void main(String[] args) {
              SpringApplication.run(ServiceASpringbootApplication.class, args);
          }
      }
    2. 编写Controller类
      package com.huawei.demo.servicea.controller;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import com.huawei.demo.servicea.pojo.UserInfo;
      import com.huawei.demo.servicea.service.UserService;
      
      /**
       * 用户对外接口
       *
       * @author XXX
       * @since 2023-12-06
       */
      @RestController
      @RequestMapping("/user")
      public class UserController {
          @Autowired
          private UserService userService;
      
          @GetMapping("/{userId}")
          public UserInfo getUserByName(@PathVariable String userId) {
              return userService.getUserById(userId);
          }
      }
    3. 编写Mapper类
      package com.huawei.demo.servicea.mapper;
      
      import org.apache.ibatis.annotations.Mapper;
      import org.apache.ibatis.annotations.Select;
      
      import com.huawei.demo.servicea.pojo.UserInfo;
      
      /**
       * 用户查询
       *
       * @author XXX
       * @since 2023-12-06
       */
      @Mapper
      public interface UserMapper {
          @Select("select * from demo_user_info where user_id = #{userId}")
          UserInfo getUserById(String userId);
      }
    4. 编写Pojo类
      package com.huawei.demo.servicea.pojo;
      
      import lombok.Data;
      
      /**
       * user信息
       *
       * @author XXX
       * @since 2023-12-06
       */
      @Data
      public class UserInfo {
          private String userId;
      
          private String userName;
      
          private String phone;
      
          private String address;
      }
    5. 编写Service类
      package com.huawei.demo.servicea.service;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import com.huawei.demo.servicea.mapper.UserMapper;
      import com.huawei.demo.servicea.pojo.UserInfo;
      
      /**
       * userService
       *
       * @author XXX
       * @since 2023-12-06
       */
      @Service
      public class UserService {
          @Autowired
          private UserMapper userMapper;
      
          public UserInfo getUserById(String userId) {
              return userMapper.getUserById(userId);
          }
      }
    6. 配置微服务
      server:
        port: 8081
      spring:
        application:
          name: demoServiceA
        datasource:
          url: jdbc:mysql://127.0.0.1:3306/spring_cloud_demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          username: root
          password: ***
          driver-class-name: com.mysql.jdbc.Driver
      
      mybatis:
        configuration:
          map-underscore-to-camel-case: true
        type-aliases-package: com.huawei.dmo.servicea

support.huaweicloud.com/devg-appstage/appstage_06_0028.html