栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

项目中的基础知识

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

项目中的基础知识

文章目录
      • Boolean.TRUE 和 true 性能对比
      • 字符串为空isEmpty和isBlank的区别
      • Swagger-ApiModel 和ApiModelProperty
      • Swagger-ApiOperation和ApiParam
      • BeanUtils.copyProperties

Boolean.TRUE 和 true 性能对比
  • A,D > B,C
// 7.844
Boolean A(){ 
    return Boolean.TRUE;
}
// 10.109
boolean B(){
    return Boolean.TRUE
}
// 7.906
Boolean C(){
    return true;
}
// 7.828
boolean D(){
    return true;
}
字符串为空isEmpty和isBlank的区别
  • isEmpty仅仅是判断空和长度为0字符串
  • isBlank判断的是空,长度为0,空白字符(包括空格,制表符t,换行符n,换页符f,回车r)组成的字符串
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("aaa") = false
StringUtils.isEmpty("t n r f") = false
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUitls.isBlank(" ") = true
StringUtils.isEmpty("aaa") = false
StringUtils.isEmpty("t n r f") = true
  • hutool工具:StrUtil
System.out.println(StrUtil.isEmpty(null)); // true
System.out.println(StrUtil.isEmpty("")); // true
System.out.println(StrUtil.isEmpty(" ")); // false
System.out.println(StrUtil.isEmpty("aaa")); // false
System.out.println(StrUtil.isEmpty("r t n f")); // false
System.out.println(StrUtil.isBlank(null)); // true
System.out.println(StrUtil.isBlank("")); // true
System.out.println(StrUtil.isBlank(" ")); // true
System.out.println(StrUtil.isBlank("aaa")); // false
System.out.println(StrUtil.isBlank("r n t f")); // true
Swagger-ApiModel 和ApiModelProperty
  • ApiModel 注解:该属性就是对所需要特别说明的接口相关实体类进行描述
属性名称属性类型默认值作用
value()String空自定义类的名称
description()String空为类添加长文本描述信息
  • ApiModelProperty 注解:该属性就是对实体类中的参数进行一个描述
属性名称属性类型默认值作用
value()String空定义参数描述信息
name()String空定义参数名称
required()booleanfalse定义参数是否必传
hidden()booleanfalse定义参数是否隐藏
allowEmptyValue()booleanfalse定义参数是否允许为空
@ApiModel(value = "用户实体,存储用户相关字段", description = "用户实体中包含用户相关的所有业务字段,如有需要请另行添加")
public class User{
    // do something...
}
@ApiModelProperty(value = "用户Id")
private Integer id;
Swagger-ApiOperation和ApiParam
  • @ApiOperation 注解不是Spring 自带的,它是swagger里的com.wordnik.swagger.annotations.ApiOperation;
  • 注解@ApiOperation 和@ApiParam是用来构建Api 文档的
  • @ApiOperation(value=“接口说明”,httpMethod=“接口请求方式”,response=“接口返回参数类型”,notes=“接口发布说明”),用在请求的方法上,说明方法的用途、作用
  • @ApiParam(required =“是否必须参数”,name=“参数名称”,value=“参数具体描述”)

其他注解:

  • @ApiImplicitParams:用在请求的方法上,表示一组参数说明
  • @Api:用在请求的类上,表示对类的说明
  • @ApiImplicitParams:用在请求的方法上,表示一组参数说明
  • @ApiResponses:用在请求的方法上,表示一组响应
  • 参考链接
BeanUtils.copyProperties
  • 浅拷贝,如果都是单一的属性,那么不涉及到深拷贝的问题,适合用BeanUtils

  • BeanUtils.copyProperties(a, b);

    • b中的存在的属性,a中一定要有,但是a中可以有多余的属性;
    • a中与b中相同的属性都会被替换,不管是否有值;
    • a、 b中的属性要名字相同,才能被赋值,不然的话需要手动赋值;
    • Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法;
    • 如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,则spring会认为属性不同,不会copy;
    • spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下

    ​

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017506.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号