首页 > 编程语言 > 详细

详细SpringCloud环境的搭建

时间:2021-03-05 23:50:04      阅读:45      评论:0      收藏:0      [点我收藏+]

详细SpringCloud环境的搭建

开始前默认已配置基础环境:

基础环境:jdk 1.8、 IDEA、 MySQL数据库或其他数据库、Maven

一、学习方向

1. 主要学习springcloud分布式开发
2. 学习其设计模式以及设计思路
3. 学习更多的小技巧

二、学习内容

  1. 建立父项目: cloud2021

    1. 配置父项目的POM.XML添加依赖的版本控制,锁定子moulde的版本号,
  2. 建立子项目:cloud-spring-payment ->支付模块

    1. 配置子项目的POM.XML添加所需依赖,依赖可不写版本号

    2. 查看子项目是否引用父项目

    3. 查看父项目cloud-spring-payment标签是否注册

    4. 创建application.yml 配置子项的各项配置:

      1. 配置子项目的端口号

      2. 配置DataSource

      3. 配置mybatis的扫包路径及所有Entity别名类所在包。(若不使用mybatis可不配置,可以自行配置,该配置主要是基于配置xml文件)

        mapperLocations: classpath:mapper/*.xmltype-aliases-package: com.motcs.springcloud.entity    # 所有Entity别名类所在包
        
      4. 配置spring-application-name: cloud-spring-payment #项目名称(具体暂时不清楚)

    5. 创建主启动类:

      @MapperScan("com.motcs.springcloud.dao")@SpringBootApplicationpublic class CloudProviderPaymentApplication {    public static void main(String[] args) {        SpringApplication.run(CloudProviderPaymentApplication.class, args);    }}
      
    6. 创建entity类所在包:com.motcs.springcloud.entity (包名可根据自己习惯书写)

      1. 常见所需要的各种实体类

        @Data@ToString
        @AllArgsConstructor
        @NoArgsConstructor
        public class Payment implements Serializable {    
            private Long id;    
            private String serial;
        }
        这里使用了lombok,所以不再写get、set方法以及构造方法
        
      2. 创建主接口返回的泛型类

        @Data@ToString
        @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); 
            	//创建一个返回数据为空的构造方法,用来处理异常   
            }
        }
        
    7. 创建dao层接口所在包:com.motcs.springcloud.dao

      1. 创建对应功能的接口

        @Mapper //Mapper注解修饰类
        public interface PaymentDao {    
            int create(Payment payment);    //添加一个支付流水号
            Payment getPaymentById(@Param("id") Long id);//通过id查询支付流水号
        }
        
      2. 编写dao的实现xml文件

        1. 在resources文件夹下创建mapper文件夹

        2. 在mapper文件夹下常见xml文件

          <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
          <mapper namespace="com.motcs.springcloud.dao.PaymentDao">    
              <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">        
              	insert into payment(serial) values (#{serial})    
              	<!--插入支付流水号-->
              </insert>    
              <resultMap id="BaseResultMap" type="com.motcs.springcloud.entity.Payment">        
                  <id column="id" property="id" jdbcType="BIGINT"></id>     
                  <id column="serial" property="serial" jdbcType="VARCHAR"></id>    	  <!-- 定义一个map用来对应获取到的数据库信息,以免出错 -->
              </resultMap>   
              <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">        
             		 select * from payment where id=#{id}    
             		 <!-- 根据id查询支付流水信息,并封装到id为BaseResultMap的map中-->
              </select>
          </mapper>
          
      3. 创建service接口所在包: com.motcs.springcloud.service

        1. 创建service接口

          public interface PaymentService {    
              public int create(Payment payment);//添加一个流水号    
              public Payment getPaymentById(@Param("id") Long id);
              //根据id查询流水信息
          }
          
      4. 创建service实现类serviceimpl所在包:com.motcs.springcloud.service.impl

        1. 创建servic实现类serviceimpl

          @Service
          public class PaymentServiceImpl implements PaymentService { 
          
              @Autowired(required = false) //根据类型注入PaymentDao
              private PaymentDao paymentDao;  
              
              @Override    
              public int create(Payment payment) {        
              	return paymentDao.create(payment);  
                  //返回创建影响的数据行数
              }    
              
              @Override    
              public Payment getPaymentById(Long id) {        
              	return paymentDao.getPaymentById(id); 
                  //返回查询到的支付流水号信息
              }
          }
          
      5. 编写业务控制层controller所在包:com.motcs.springcloud.controller

        1. 创建controller类

          @RestController //使返回值为json类型
          @Slf4j //开启日志注解
          public class PaymentController {
          
              @Resource //注入PaymentService,同AutoWrite注解相同
              private PaymentService paymentService;
              
              @PostMapping(value = "/payment/create") //提交接口   
              public CommonResult create(Payment payment) {        
                  int result = paymentService.create(payment);//开始查询        
                  log.info("******插入结果:" + result);   //日志打印信息    
                  if (result > 0) {   //判断是否插入成功        
                 		 return new CommonResult(200, "插入数据库成功", payment);
                       //插入成功返回信息
                  } else {            
                 		 return new CommonResult(444, "插入数据库失败", null);    
                       //插入失败返回信息
                  }    
              }    
              
              @GetMapping(value = "/payment/getPaymentById/{id}")   
              public CommonResult getPaymentById(@PathVariable("id") Long id) { 
                  Payment payment = paymentService.getPaymentById(id);//执行查询
                  log.info("******查询结果:" + payment);     //日志打印查询结果   
                  if (payment != null || !"".equals(payment)) {  //判断查询结果是否不为空 
                 		 return new CommonResult(200, "查询数据库成功", payment);  
                       //查询到数据返回信息
                  } else {            
                  	return new CommonResult(444, "没有对应径路,查询ID:" + id, null);       		   //查询不到数据返回信息
                  }    
              }    
              
              @RequestMapping("/get")    
              public CommonResult get() {        
              	return new CommonResult(200, "查询数据库成功", null);
                  //测试方法,主要验证不连接数据库时能否正常跑通
              }
          }
          
      6. 启动主启动类:CloudProviderPaymentApplication 最小化IDEA编辑器

      7. 打开浏览器输入URL:localhost:8001//get 测试是否畅通(以QQ浏览器为例)

        {"code":200,"message":"查询数据库成功","data":null}
        查询到反馈信息无误,证明程序可以连接畅通。
        
      8. 输入URL:localhost:8001//payment/getPaymentById/1 查询ID为1的流水信息

        {"code":200,"message":"查询数据库成功","data":{"id":1,"serial":"202103041827"}}
        查询信息无误,程序正常运行。
        
      9. 输入URL:localhost:8001//payment/create?serial=2021030521581134 创建流水信息

        Whitelabel Error Page

        This application has no explicit mapping for /error, so you are seeing this as a fallback.

        Fri Mar 05 22:46:24 CST 2021

        There was an unexpected error (type=Method Not Allowed, status=405).

        Request method ‘GET‘ not supported

        org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET‘ not supported

        A: 很明显,发生了错误了,呜呜,怎么办呢?

        B: 不急,别慌,稳住,看我的

        1. 由于我们是创建流水信息,为POST请求,而浏览器,主要是查看也就是GET,对于POST请求支持不是特别友好,所以这个时候可以换一种方法。
        2. 如果你电脑有postman那就省事了,没有的话可以下载一下。连接地址:下载Postman

        A: 只有这种方法吗?

        B:不急,还有别的,先把这个说了

        1. 打开postman,输入我们的POST请求的URL,然后Send,之后在下方查看结果吧

          {

          ? "code": 200,

          ? "message": "插入数据库成功",

          ? "data": {"id": 5,"serial": "2021030521581134"}

          }

          可以看到,已经成功插入了呢。serial可以根据自己的定义进行输入

        2. 好了,postman完事了,那么没有postman的小伙伴,可以选择比较费劲的方法了,书写一个简单的界面,定义FORM表单,然后输入流水号就可以啦,当然要注意字段的对应哦,还有action也要写对呢,最好吧metohd也写上,哎呀,知道有些时候懒得写,来给你安排

          <form action="payment/getPaymentById/" method="post">   
          <!-- action要根据自己实际来写哦 -->
              <input type="text" name="serial">    
              <input type="submit" value="提交">
          </form>
          
        3. 还有其他方法,这里就不一一赘述了,作为开发人员,如果连这些都不自己去熟悉,那么可以趁早转行了。


      三、总结

      1. 越学越感觉自己的无知,学无止境,学海无涯苦中寻乐方能行稳致远
      2. 通过再次书写博客对springcloud的基础搭建有了更深的了解
      3. 编程还是需要多动手、多动脑、多动记事本哦,好记性不如烂笔头,每天把所学的再复习一遍,思路会更加的清晰明了,也能极大程度的增加记忆
      4. 关于整个流程:配置环境-->创建项目-->配置POM -->创建并配置YML --> 书写具体的业务代码
      5. 对于编程要牢记的:约定>配置>编码
      1. 引自尚硅谷阳哥的话,确实有道理

      第一次写这个,有不对的地方还请多指正,谢谢!

详细SpringCloud环境的搭建

原文:https://www.cnblogs.com/jin-zesheng/p/14489013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!