SpringCloud基础

cloud技术选型

cloud
服务注册中心
zookeeper
consul
Nacos
服务调用
Ribbon
LoadBalancer
OpenFegin
服务降级
resilience4j
sentinel
服务网关
gateway
服务配置
nacos
服务总线

devtools热部署

在父工程的pom中添加

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
</dependency>

在父工程的pom中添加

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

在idea的complier中,选项全都打勾,ctrl+shfit+alt+/,把最后为running的一条打勾
yml中配置

spring:
  devtools:
    restart:
      enabled: true  #设置开启热部署
  freemarker:
    cache: false

注意:热部署只能在debug下使用

RestTemplate

  1. 将RestTemplate注入spring容器
@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced//开启负载均衡
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
  1. 使用
 @Resource
 private RestTemplate restTemplate;
// 方法1
 restTemplate.postForObject(String url,Object request,Class responseType);
// 参数二是请求时需要传递的参数对象,参数三是请求完成后服务器返回的对象类型class
//比如做数据库添加操作是,参数二就是要添加的对象,这和请求的url需要的参数是匹配的
 //方法2
  restTemplate.getForObject(String url,Class responseType);

注意:restTemplate传递post时,因为不能把参数写在head上传递,所以restTemplate会把数据转换成json字符串写入请求体中,所以被请求的controller需要使用@RequestBody绑定参数。
@PostMapping可以接收来自请求体中的数据,但是如果是请求头中的数据,如果可以和参数匹配的上 ,那么也会接收

实体类层

@Data @AllArgsConstructor @NoArgsConstructor

< dependency >
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
 </ dependency >

@Data 提供默认get和set方法
@NoArgsConstructor 无参构造
@AllArgsConstructor 有参构造

@RequestBody

简单来说就是可以把前端传来的字符串中的key-value解析成参数对象中属性,并赋值给参数对象
注意:RequestBody接收的数据正如名字一样是在方法体中的,所以只能在post请求时使用,使用此注解后 ,不能接收写在请求头中的数据,只能接收json字符串。
@RequestBody详解

@PostMapping

这里有个注意事项,标注该注解,并不是只能接受请求体中的数据,即使把数据写在url中,也可以接收

dao接口层

@Mapper

用在mapper接口上

MapperScanner(“com.sw.dao”)

用在启动类上

@param

使用@param的 四种情况
注意上面的文章在多参数时有错误,多参数时不能使用parameterType

Controller层

mapper.xml层

useGeneratedKeys=“true” keyProperty=“id”

用于自增主键的插入,自动返回新增的主键,必须使用在insert标签内
注意事项,自动返回的主键值存在与实体类的id中,接口返回值表示的是插入数据的输量。

public interface PaymentDao {
    public int add(Payment payment);
}
  <insert  id="add" parameterType="Payment" keyProperty="id" useGeneratedKeys="true">
        insert into payment(serial) values (#{serial})
    </insert>

这里接口口PaymentDao的返回值是插入的记录数,而最新的id值自动存入参数中的实体类中

resultMap

当数据库的字段和实体类的字段不能自动匹配时,比如实体类使用了驼峰命名admin_username,实体类是驼峰命名adminUsername,这时需要使用resultMap

  <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT"></id>
        <id column="serial" property="serial" jdbcType="VARCHAR"></id>
    </resultMap>
    <select id="queryPaymentById" resultMap="BaseResultMap" parameterType="Long">
        select
          id, serial
        from payment
        where id = #{id}
    </select>
    # column表示实体类的字段,property表示数据库的字段,让他们手动匹配

mapUnderscoreToCamelCase

启用驼峰映射,即数据库的下划线命名对应实体类的驼峰命名

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

或者
mybatis:
map-underscore-to-camel-case: true

typeAliases

启用别名,这样mapper的parameter就可以不适用全类名

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.sweeter.editor.domain"/>
    </typeAliases>
</configuration>

或者
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities

多参数

  1. 按照顺序写入,不利于阅读,不推荐

当有多个参数传递时,不能使用parameterType

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
  1. 通过map封装,大量数据时推荐
public List<XXXBean> getXXXBeanList(HashMap map);  

<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">

  select 字段... from XXX where id=#{xxId} code = #{xxCode}  

</select>  

其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。
  1. 通过list封装
public List<XXXBean> getXXXBeanList(List<String> list);  

<select id="getXXXBeanList" resultType="XXBean">
  select 字段... from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>  

foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')
  1. 使用@param
public List<XXXBean> getXXXBeanList(List<String> list);  

<select id="getXXXBeanList" resultType="XXBean">
  select 字段... from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>  

foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')

bindExecption

检查配置文件是否错误,有错误会出现黄色高亮,mybatis应该是一级配置,不要放在spring下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值