java相关注解

1.@RequestParam注解

//属性defaultValue	:设置默认值(注意:必须是string类型的)
public Object list(String username, String mobile,
                   @RequestParam(defaultValue = "1") Integer page,
                   @RequestParam(defaultValue = "10") Integer limit,
                   @Sort @RequestParam(defaultValue = "add_time") String sort,
                   @Order @RequestParam(defaultValue = "desc") String order) {
    List<LitemallUser> userList = userService.querySelective(username, mobile, page, limit, sort, order);

2.@Order注解

在方法上面:@order(0) 里面的值最小,优先级越高

3.@ApiOperation注解

是swagger里的注解是用来构建Api文档的
@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”;其他参数可参考源码;

4.@ApiImplicitParams:注解

/*
	参数:	name :参数名。 
			value : 参数的具体意义,作用。 
			required : 参数是否必填。 
			dataType :参数的数据类型。 
			paramType :查询参数类型,这里有几种形式:
					类型    作用
					path    以地址的形式提交数据
					query    直接跟参数完成自动映射赋值
					body    以流的形式提交 仅支持POST
					header    参数在request headers 里边提交
					form    以form表单的形式提交 仅支持POST
*/
@ApiImplicitParams({
@ApiImplicitParam(name = "title", value = "课程标题", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "courseStatus", value = "课程状态", paramType = "query", dataType = "Integer")
})

5.@RequestParam,@PathVariable和@RequestBody三者区别

@RequestParam注解:获取参数,即是获取传送过来的参数
如:
//http://localhost:8090/hello?id=2
//使用@RequestParam注解获取id
public String Demo1(@RequestParam String id){
    System.out.println("链接中请求参数的id:"+id);
    return null;
}

@PathVariable注解:路径变量,即是获取链接路径上的变量
如:
http://localhost:8090/hello/2
//使用@PathVariable注解获取
public String getBook(@PathVariable Integer id) {
     try {
            system.out.println("路径上的id:"+id);
        } catch (ParseException e) {
            e.printStackTrace();
    }
    return null;
}

@RequestBody 注解: 用于获取json数据
如:
//传过来的是json 数据    
public String hello(@requestBody User user){      
	System.out.println(user.name);    
}

6.@RequiresPermissions注解

shiro的注解,必须包含的权限 如:@RequiresPermissions(value = {"course"})

7.@NotNull,@NotBlank,@Length

@NotNull(message = "课程介绍不能为空")
@NotBlank(message = "课程介绍参数错误")
@Length(min = 1, max = 100000, message = "课程介绍参数请在1~100000字符")

8.@Validated注解

	效验数据:在参数对象前面加上这个注解,就回自动效验这个对象(这个对象已经加了@NotNull等条件)

BindingResult效验参数: 
	和其他效验注解一起使用,如:
public ResultVO courseList(@Validated CourseQueryForm courseQueryForm, BindingResult bindingResult){	bindingResult.hasErrors()
}

9.@RequestParam注解

参数条件,参数有:  
Value:请求中传入参数的名称,如果不设置,那么传入的参数必须和方法中的参数名一致,否则封装不进去
	Required:true或者false,是否必须传
	defaultValue:参数的默认值,如果请求中没有同名的参数时,该变量默认为此值

10.@ConfigurationProperties注解

@ConfigurationProperties(prefix = "constants", ignoreUnknownFields = false) // 前缀(字段自动封装)	ignoreUnknownFields 忽略未知的字段

11.@PropertySource注解

@PropertySource("classpath:application-test.properties") // 指明属性文件路径

12.@Slf4j

日志注解:使用这个注解后,下面可以使用log.info(“xxxxxxxxx”);
注意:使用改注意,必须下载lombok插件

13.lombok插件相关注解:

 使用lombok插件:①idea下载lombok插件;②导入包
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
    <scope>provided</scope>
</dependency>
@Data:这个注解,包含Getter,Setter,equals,canEqual,hasCode,toString等方法,注解后在编译时会自动加进去。
@AllArgsConstructor:使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数
@RequiredArgsConstructor:部分参数的构造方法
@NoArgsConstructor:使用后创建一个无参构造函数
@Getter:自动生成getter方法
@Setter:自动生成setter方法
@EqualsAndHashCode:自动生成equals和hasCode方法
@Cleanup:自动调用close()方法
如:不使用@Cleanup的写法
import java.io.*;

public class CleanupExample {
  public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    try {
      OutputStream out = new FileOutputStream(args[1]);
      try {
        byte[] b = new byte[10000];
        while (true) {
          int r = in.read(b);
          if (r == -1) break;
          out.write(b, 0, r);
        }
      } finally {
        if (out != null) {
          out.close();
        }
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }
}
使用@Cleanup的写法
import lombok.Cleanup;
import java.io.*;

public class CleanupExample {
  public static void main(String[] args) throws IOException {
    @Cleanup InputStream in = new FileInputStream(args[0]);
    @Cleanup OutputStream out = new FileOutputStream(args[1]);
    byte[] b = new byte[10000];
    while (true) {
      int r = in.read(b);
      if (r == -1) break;
      out.write(b, 0, r);
    }
  }
}

14.@PostConstruct说明、@PreConstruct说明

@PostConstruct:修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。
被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
@PreConstruct:修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。
被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

15.Mybatis相关注解

自动生成的使用:@Table(name="t_Teacher")//解决类名和表名不对应
			  @Column(name="id")//解决属性名和字段名不对应
			  @Column(length=50)//限制字符串长度
			  @Temporal(TemporalType.DATE)//限制时间格式:只记录日期不记录时间。默认是时间日期都记录
			  @Id
			  @Transient//隐藏属性,不会被映射到表中
			  @Column
			  @Enumerated(EnumType.STRING)//把枚举里的sring存储进去
			  @Enumerated(EnumType.ORDINAL)//把存在的顺序编号int存储进去

16.@MapperScan、@ComponentScan

@MapperScan@ComponentScan可以同时使用
@MapperScan:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。
@ComponentScan:会自动扫描包路径下面的所有@Controller@Service@Repository@Component 的类,并把符合扫描规则的类装配到spring容器中。

未完待遇。。。。。。。

发布了31 篇原创文章 · 获赞 3 · 访问量 904

猜你喜欢

转载自blog.csdn.net/S_L__/article/details/103112553