_
_———————————————_Spring
@Component使用在类上用于实例化Bean@Controller使用在web层类上用于实例化Bean@Service使用在service层类上用于实例化Bean@Repository使用在dao层类上用于实例化Bean@Autowired使用在字段上用于根据类型依赖注入@Qualifier结合@Autowired+@Qualifer,按照名称进行注入@Resource相当于@Autowired+@Qualifer,按照名称进行注入@Value注入普通属性@Scope标注Bean的作用范围范围:prototype-多例singleton-单例@PostConstruct使用在方法上标注该方法时Bean的初始化方法@PreDestroy使用在方法上标注该方法时Bean的销毁方法@Configuration用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注解@ComponentScan用于指定Spring在初始化容器时要扫描的包作用和在Spring的xml配置文件中的<context:component-scan base-package="xxx"/>一样@Bean使用在方法上,标注将该方法的返回值存储到Spring容器中@PropertySource用于加载.properties文件中的配置@Import用于导入其他配置类@Aspect标注当前类是一个切面类注意:使用注解开发需要在配置文件配置AOP自动代理:<aop:aspectj-autoproxy/>@Before配置前置通知@AfterReturning后置通知@Around环绕通知@AfterThrowing异常抛出通知@After最终通知@EnableCaching注解@EnableCaching注解是spring framework中的注解驱动的缓存管理功能。自spring版本3.1起加入了该注解。如果你使用了这个注解,那么你就不需要在XML文件中配置cache manager了。当你在配置类(@Configuration)上使用@EnableCaching注解时,会触发一个post processor,这会扫描每一个spring bean,查看是否已经存在注解对应的缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。
———————————————Junit
@Test用来修饰方法,表示该方法是一个测试方法@Before用来修饰方法,该方法会在每个测试方法执行之前执行一次@After用来修饰方法,该方法会在每一个测试方法执行之后执行一次@BeforeClass用来修饰方法,该方法会在所有测试方法执行之前执行一次@AftorClass用来修饰方法,该方法会在所有测试方法执行之后执行一次
———————————————SpringBoot
@RunWith(SpringRunner.class)加载Junit运行环境@SpringBootTest(classes=xxx.class)读取SpringBoot启动类@Reference注入容器,注入分布式的远程服务对象,需要配合dubbo配置使用。@Autowired注入容器,将对象注入容器中,如果容器中已经创建对象,name后续调用蒋志杰从容器中拿取。@Resource很简单就是注入资源,与@Autowired比较接近,只不过是按照变量名(beanid)注入。@Bean给容器中添加组件@Slf4j注解实现日志输出:4个等级:log.debug();log.info();log.error();log.warn();@RestController@RestController注解相当于@Controller+@ResponseBody合在一起的作用标记控制层的类和设置返回值为JSON对象@Controller: Spring框架提供的表现层控制类注解,这个控制层里面的每个方法都可以去调用@Service标识的类@ResponseBody:标记Controller类中的方法,retrun返回值为JSON对象@RequestMapping()指定前端的访问路径@ControllerAdvice声明全局性可以结合@ExceptionHandler用于全局异常的处理@ControllerAdvice是在类上声明的注解,其用法主要有三点:@ExceptionHandler注解标识的方法:用于补货Controller中抛出的不同类型的异常,从而达到异常全局处理的目的@InitBinder注解标注的方法:用于请求中注册自定义参数的解析,从而达到自定义请求参数格式的目的@ModelAttribute注解标注的方法:表示此方法会执行目标Controller方法之前还行@PathVariable()@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值@EnableCaching开启Spring的注解缓存支持
**
@TableField
mybatis-plus的自动填充时间在实体类上加上@TableField//字段插入自动填充@TableField(fill = FieldFill.INSERT)private Date createTime;//字段更新自动填充@@TableField(fill = FieldFill.INSERT_UPDATE)private Date updateTime;属性介绍:DEFAULT, 默认,不填充INSERT, 插入时填充UPDATE, 更新时填充INSERT_UPDATE; 插入更新都填充
@**Document**
模型类中@Document注解是 mongodb提供的一个注解。@Document注解指定了这个模型类型所对应的集合名称@Document(collection=‘名称’)@Id 自动生成的主键ID 主键,不可重复,自带索引,可以在定义的列名上标注,需要自己生成并维护不重复的约束。如果自己不设置@Id主键,mongo会自动生成一个唯一主键,并且插入时效率远高于自己设置主键@Document标注在实体类上,类似于hibernate的entity注解,标明由mongo来维护该表。把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。@Document(collection="mongodb 对应 collection 名")// 若未加 @Document ,该 bean save 到 mongo 的 userInfo collection// 若添加 @Document ,则 save 到 User collection@Document(collection = "User")public class UserInfo@Indexed声明该字段需要加索引,加索引后以该字段为条件检索将大大提高速度。唯一索引的话是@Indexed(unique = true)。也可以对数组进行索引,如果被索引的列是数组时,MongoDB会索引这个数组中的每一个元素。也可以对整个Document进行索引,排序是预定义的按插入BSON数据的先后升序排列。————————————————@CompoundIndex复合索引,加复合索引后通过复合索引字段查询将大大提高速度。@Field代表一个字段,可以不加,不加的话默认以参数名为列名。给映射存储到 mongodb 的字段取别名
@**PathVariable()**
@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
@EnableCaching
开启Spring的注解缓存支持
@Component
@EnableScheduling
开启定时任务支持
@Scheduled
配置定时执行的方法:
—————Elasticsearch
@Document
当映射ES的文档:
参数:
indexName:索引库
type:类型
shards:分片数量
replicas:副本数量
@Id
当映射ES文档:
文档主键映射
@Field
当映射ES文档:
参数:
index:该字段是否索引(如果该字段参与搜索,该字段就必须索引)
analyzer:指定分词器(如果该索引字段需要被分词后搜索,该字段通常指定分词器)
常用分词器:
ik_smart:最小分词。 例:我是程序员->我 是 程序员
ik_max_word:最细分词 例:我是程序员->我 是 程序员 程序 员
store:该字符串是否存储(如果该字段在结果中需要被显示出来,该字段就要存储)默认是true
type:该字段的类型
Text:字符串类型,分词的类型
Keyword:字符串类型,不分词的类型
Integer/Long/Float/Double :数值类型,不分词的类型
Date:日期类型,不分词的类型
Boolean:布尔类型,不分词的类型
Object :对象类型,包含自定义对象,List<对象>,Set..,Map..,在ES中对象类型,里面的每个属性都是索引和分词的
—————SpringCloud
Feign
@FeignClient(”xxx”)
通过nacos查询服务名称,来进行远程调用
@EnableFeignClients
开启Feign接口
@EnableDiscoveryClient
—————SpringTask
开启定时任务
@Scheduled
开启定时任务 参数一:fixedRate = 毫秒
固定ping
—————lombok
@Setter
生成setter方法,final变量不包含
@Getter
生成getter方法,final变量不包含
@NoArgsConstructor
生成空参构造
@AllArgsConstructor
生成全部参数构造
@RequiredArgsConstructor
将标记为@NoNull的属性生成一个构造器
如果运行中标记为@NoNull的属性为null,会抛出空指针异常。
@ToString
生成所有属性的toString()方法
@EqualsAndHashCode
生成equals()方法和hashCode方法
@Data(常用)
@Data直接修饰POJO or beans, getter所有的变量,setter所有不为final的变量。如果你不需要默认的生成方式,直接填写你需要的annotation的就可以了。默认生成的所有的annotation都是public的,如果需要不同权限修饰符可以使用AccessLevel.NONE选项。当然@Data 也可以使用staticConstructor选项生成一个静态方法。
=@Setter+@Getter+@EqualsAndHashCode+@NoArgsConstructor
@Builder
构造Builder模式的结构。通过内部类Builder()进行构建对象。
//原始类
@Builder
public class TestEntity {
private String name;
private Integer age;
private final String type = "person";
}
//反编译的类
public class TestEntity {
private String name;
private Integer age;
private final String type = "person";
@ConstructorProperties({"name", "age"})
TestEntity(String name, Integer age) {
this.name = name;
this.age = age;
}
public static TestEntity.TestEntityBuilder builder() {
return new TestEntity.TestEntityBuilder();
}
public static class TestEntityBuilder {
private String name;
private Integer age;
TestEntityBuilder() {
}
public TestEntity.TestEntityBuilder name(String name) {
this.name = name;
return this;
}
public TestEntity.TestEntityBuilder age(Integer age) {
this.age = age;
return this;
}
public TestEntity build() {
return new TestEntity(this.name, this.age);
}
public String toString() {
return "TestEntity.TestEntityBuilder(name=" + this.name + ", age=" + this.age + ")";
}
}
}
//Builder模式使用方法
@Test
public void test(){
TestEntity testEntity = TestEntity.builder()
.name("java")
.age(18)
.build();
@Value
与@Data相对应的@Value, 两个annotation的主要区别就是如果变量不加@NonFinal ,@Value会给所有的弄成final的。当然如果是final的话,就没有set方法了。
//原始类
@Value
public class TestEntity {
@Setter(AccessLevel.PRIVATE)
private String name;
private Integer age;
private final String type = "person";
}
//反编译的类
public final class TestEntity {
private final String name;
private final Integer age;
private final String type = "person";
@ConstructorProperties({"name", "age"})
public TestEntity(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public Integer getAge() {
return this.age;
}
public String getType() {
this.getClass();
return "person";
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof TestEntity)) {
return false;
} else {
TestEntity other;
label44: {
other = (TestEntity)o;
String this$name = this.getName();
String other$name = other.getName();
if(this$name == null) {
if(other$name == null) {
break label44;
}
} else if(this$name.equals(other$name)) {
break label44;
}
return false;
}
Integer this$age = this.getAge();
Integer other$age = other.getAge();
if(this$age == null) {
if(other$age != null) {
return false;
}
} else if(!this$age.equals(other$age)) {
return false;
}
String this$type = this.getType();
String other$type = other.getType();
if(this$type == null) {
if(other$type != null) {
return false;
}
} else if(!this$type.equals(other$type)) {
return false;
}
return true;
}
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
Integer $age = this.getAge();
result1 = result1 * 59 + ($age == null?43:$age.hashCode());
String $type = this.getType();
result1 = result1 * 59 + ($type == null?43:$type.hashCode());
return result1;
}
public String toString() {
return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
}
}
@Synchronized
同步方法
法
//原始类
public class TestEntity {
private String name;
private Integer age;
private final String type = "person";
@Synchronized
public void write(){
//do something
}
}
//反编译的类
public class TestEntity {
private final Object $lock = new Object[0];
private String name;
private Integer age;
private final String type = "person";
public TestEntity() {
}
public void write() {
Object var1 = this.$lock;
synchronized(this.$lock) {
;
}
}
}
@Cleanup @SneakyThrows
自动调用close方法关闭资源
//原始类
public class TestEntity {
private String name;
private Integer age;
private final String type = "person";
@SneakyThrows
public void outputStream(){
@Cleanup OutputStream outputStream = new FileOutputStream(new File("/Users/hello"));
}
}
//反编译的类
public class TestEntity {
private String name;
private Integer age;
private final String type = "person";
public TestEntity() {
}
public void outputStream() {
try {
FileOutputStream $ex = new FileOutputStream(new File("/Users/hello"));
if(Collections.singletonList($ex).get(0) != null) {
$ex.close();
}
} catch (Throwable var2) {
throw var2;
}
}
}
—————Hibernate Validator
| Constraint | 详细信息 |
|---|---|
| @Valid | 被注释的元素是一个对象,需要检查此对象的所有字段值 |
| @Null | 被注释的元素必须为 null |
| @NotNull | 被注释的元素必须不为 null |
| @AssertTrue | 被注释的元素必须为 true |
| @AssertFalse | 被注释的元素必须为 false |
| @Min(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
| @Max(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
| @DecimalMin(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
| @DecimalMax(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
| @Size(max, min) | 被注释的元素的大小必须在指定的范围内 |
| @Digits (integer, fraction) | 被注释的元素必须是一个数字,其值必须在可接受的范围内 |
| @Past | 被注释的元素必须是一个过去的日期 |
| @Future | 被注释的元素必须是一个将来的日期 |
| @Pattern(value) | 被注释的元素必须符合指定的正则表达式 |
| 被注释的元素必须是电子邮箱地址 | |
| @Length | 被注释的字符串的大小必须在指定的范围内 |
| @NotEmpty | 被注释的字符串的必须非空 |
| @Range | 被注释的元素必须在合适的范围内 |
| @NotBlank | 被注释的字符串的必须非空 |
| @URL(protocol=,host=, port=,regexp=, flags=) | 被注释的字符串必须是一个有效的url |
| @CreditCardNumber | 被注释的字符串必须通过Luhn校验算法,银行卡,信用卡等号码一般都用Luhn计算合法性 |
—————swagge-ui
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
—————MyBatis
@Delete
删除
@Delete("delete from xxx where xxx=xxx")
void xxx(xxx,xxx);
@Update
修改
@Update("update xxx set xxx where xxx")
void xxx(xxx xxx);
@Insert
新增
@Insert("INSERT INTO xxx VALUES xxx;")
void xxx(xxx xxx);
@SelectKey
xml文件配置selectKey
<insert id="addUser2" parameterType="user">
INSERT INTO user (username, birthday, sex, address) VALUES (#{username}, #{birthday}, #{sex}, #{address});
<selectKey resultType="int" keyColumn="id" keyProperty="id">
SELECT last_insert_id();
</selectKey>
</insert>
@SelectKey属性
statement :要执行的SQL语句
keyProperty:实体类中主键的属性
keyColumn:
