1.@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
2. @Column标记表示所持久化属性所映射表中的字段,用来标识实体类中属性与数据表中字段的对应关系。
@Column属性详解:
- name:定义了被标注字段在数据库表中所对应字段的名称;
- unique:表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用 @Table标记中的@UniqueConstraint。
- nullable:表示该字段是否可以为null值,默认为true。
- insertable:表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。
- updatable:表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
- columnDefinition(大多数情况,几乎不用):表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。(也就是说,如果DB中表已经建好,该属性没有必要使用。)
- table:表示当映射多个表时,指定表的表中的字段。默认值为主表的表名。
- length:表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。
- precision和scale:precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。
示例
Example 1: 指定字段“tradeNo”交易编号的长度为50,且值不能为null
@Column(name = "tradeNo", length = 50, nullable = false)
private String tradeNo;
Example 2: 指定字段“totalAmount”交易金额的精度(长度)为10,小数点位数为2位,且值不能为null
@Column(name = "totalAmount", precision = 10, scale = 2, nullable = false)
private BigDecimal totalAmount;
4.基于注解的bean定义(不需要提供setter方法
Spring为此提供了四个注解,这些注解的作用与上面的XML定义bean效果一致,在于将组件交给Spring容器管理。组件的名称默认是类名(首字母变小写),也可以自己修改:
@Component:当对组件的层次难以定位的时候使用这个注解
@Controller:表示控制层的组件
@Service:表示业务逻辑层的组件
@Repository:表示数据访问层的组件
基于Java类的bean定义(需要提供setter方法)
@Bean 基础声明
Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。
SpringIOC 容器管理一个或者多个bean,这些bean都需要在@Configuration注解下进行创建,在一个方法上使用@Bean注解就表明这个方法需要交给Spring进行管理。
使用@Bean注解需要引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
5.@ApiModelProperty
@RequestParam加与不加的区别
最简单的两种写法,加或不加@RequestParam注解
@RequestMapping("/findBy")
public String test(int userName) {
return "userName";
}
@RequestMapping("/findBy")
public String test(@RequestParam int userName) {
return "userName";
}
第一种写法参数为非必传,第二种写法参数为必传。参数名为userId。
第二种写法可以通过@RequestParam(required = false)设置为非必传。因为required值默认是true,所以默认必传。
第二种写法可以通过@RequestParam("userId")或者@RequestParam(value = "userId")指定参数名。
第二种写法可以通过@RequestParam(defaultValue = "0")指定参数默认值
用法如下:
@RequestMapping("/findBy")
public String test(@RequestParam(value = "userName", defaultValue = "0", required = userName) int userId) {
return "list";
}
@ApiParam @PathVariable @RequestParam三者区别
1.@ApiParam 顾名思义,是注解api的参数,也就是用于swagger提供开发者文档,文档中生成的注释内容。
@ApiOperation( value = "编辑公告", notes = "编辑公告", httpMethod = "POST" )
@RequestMapping( value = "/edit", method = RequestMethod.POST )
public RequestResult edit(
@ApiParam(name = "title", value = "公告标题", required = true) @RequestParam("title") String title,
@ApiParam(name = "content", value = "公告内容", required = true) @RequestParam("content") String content){
2.@RequestParam,是获取前端传递给后端的参数,可以是get方式,也可以是post方式。其中如果前端传递的参数和后端你接受的参数起的名字字段是一致的可以省略不写,也可以直接写@RequestParam String title,如果不一致一定要完整写,不然获取不到,如下面的bis_key就必须写。
@ApiOperation( value = "编辑公告", notes = "编辑公告", httpMethod = "POST" )
@RequestMapping( value = "/edit", method = RequestMethod.POST )
public RequestResult edit(
@ApiParam(name = "bis_key", value = "bis_key", required = true) String bisKey,
@ApiParam(name = "title", value = "公告标题", required = true) @RequestParam String title,
@ApiParam(name = "content", value = "公告内容", required = true) String content,
3.@PathVariable,是获取get方式,url后面参数,进行参数绑定
@ApiOperation(value = "删除公告", notes = "删除公告", httpMethod = "POST")
@RequestMapping(value = "/delete/{bisKey}", method = RequestMethod.POST)
public RequestResult remove(@ApiParam(name = "bisKey", value = "需要删除的公告ids", required = true) @PathVariable String bisKey) {
Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解
参考链接:https://blog.csdn.net/summerSunStart/article/details/78676781
参考链接:https://blog.csdn.net/summerSunStart/article/details/78676781
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如”请求参数没填好”
response:抛出异常的类
@ApiOperation("获取用户信息")
@ApiImplicitParams({@ApiImplicitParam(paramType="header",name="name",dataType="String",required=true,value="用户的姓名",defaultValue="zhaojigang"),
@ApiImplicitParam(paramType="query",name="pwd",dataType="String",required=true,value="用户的密码",defaultValue="wangna")
})
@ApiResponses({ @ApiResponse(code=400,message="请求参数没填好"),
@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
})
@RequestMapping(value="/getUser",method= RequestMethod.GET)
public User getUser(@RequestHeader("name") String name,@RequestParam("pwd") String pwd) {
System.out.println(name);
System.out.println(pwd);
return userRepository.getUserByNameAndPwd(name,pwd);
}