微服务概念
每个微服务运行在其独立的进程中,服务和服务间采用轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API)。
版本说明
- cloud Hoxton.SR1
- boot 2.2.2RELEASE
- cloud alibaba 2.1.0 RELEASE
- java java8
- Maven 3.5以上
- Mysql 5.7以上
微服务模块 cloud-provider-payment8001
- 如何构件一个微服务模块: 建立module => 改写POM => 写YML => 编写springboot主启动类 => 业务类。
- 其中业务类中的顺序一般又是:建表SQL、entities、dao(还要在resources中添加mapper,编写Mapper.xml)、service、controller。
- 前后端分离后,传参不能传到底了。传给前端的应该是json串。
需熟练掌握
-
手写从sql;
-
手写dao;
-
手写mapper;
<?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.atguigu.springcloud.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values (#{serial}); </insert> <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id}; </select> </mapper>
知识点
-
Maven中的DependencyManagement和Dependencies的区别?
DependencyManagement元素来提供了一种管理依赖版本号的方式,通常会在一个组织或者项目的最顶层的父POM中看到DependecyManagement。使用pom.xml中的dependencyManagement元素,能让所有在子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层向上奏,知道找到一个拥有dependencyManagement元素的项目,然后就会用这个dependencyManagement元素中指定的版本号。
-
优点:能够统一管理子项目的版本号。如果子项目不想和父pom同版本,只需声明version即可。
-
同时还要注意,dependencyManagement只是声明依赖,并不实现引入,因此子项目需要在子项目的pom显示声明需要用的依赖。
-
-
SpringBoot中@Mapper和Repository的区别?
他们都是用来表示dao层的注解的。但是推荐使用@Mapper。
- @Mapper单独使用,然后通过xml里面的namespace里面的接口地址,生成Bean后注入Service中。
- @Repository:配合@MapperScan注解一起使用,即需要配置扫描地址。
-
@Controller和@RestController注解区别?
- @Controller:在对应的方法上,视图解析器可以解析return的jsp和html页面,并跳转。若需要返回json等内容到页面,则需要加@ResponseBody注解
- @RestController:相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要再方法前加@ResponseBody注解了。但是不能返回jsp,html页面。所以这个注解更加适合前后端分离的情况。
-
@RequestParam和@PathVariable注解的区别?
- @RequestParam和@PathVariable都能够完成类似的功能——因为本质上,它们都是用户的输入,只不过输入的部分不同,一个在URL路径部分,另一个在参数部分。
- URL设计都是可以的:通过@PathVariable,例如/blogs/1 ;通过@RequestParam,例如blogs?blogId=1。
- 当URL指向的是某一具体业务资源(或资源列表),例如博客,用户时,使用@PathVariable;当URL需要对资源或者资源列表进行过滤,筛选时,用@RequestParam。
- 例如我们会这样设计URL:/blogs/{blogId} ;
/blogs?state=publish而不是/blogs/state/publish来表示处于发布状态的博客文章
-
@PathVariable
@RequestMapping(value = "/user/{username}/blog/{blogId}") public String getUserBlog(@PathVariable String username, @PathVariable int blogId) { return "user:" + username + "blog->" + blogId; }
这种情况下,Spring能够根据名字自动赋值对应的函数参数值,当然也可以在@PathVariable中显示地表明具体的URL变量值。
-
@RequestParam
这样,当我们访问/user/?id=123时,SpringMVC帮助我们将Request参数id的值绑定到了处理函数的参数blogId上.
@RequestMapping(value="/user") public String getUserBlog(@RequestParam(value="id") int blogId) { return "blogId="+blogId; }
tips
-
浏览器对post请求支持较差,因此需要用postman来进行post测试。
-
每个方法,要写成controller自测后,才算完成。
代码地址:https://github.com/AJ-Spade/cloud2020/tree/master