注释的总结

DAO层

@Param

package Mapper; 


public interface Mapper { 

@Select("select s_id id,s_name name,class_id classid from student where  s_name= #{aaaa} and class_id = #{bbbb}") 
    public Student select(@Param("aaaa") String name,@Param("bbbb")int class_id);  

@Delete...... 

@Insert...... 

} 

1.@Select表示执行select语句
2.s_id id, s_name name, class_id classid ,格式是字段名+属性名
3.where s_name= #{aaaa} and class_id = #{bbbb} 表示sql语句要接受2个参数,一个参数名是aaaa,一个参数名是bbbb,如果要正确的传入参数,那么就要给参数命名,因为不用xml配置文件,那么我们就要用别的方式来给参数命名,这个方式就是@Param注解
4.在方法参数的前面写上@Param(“参数名”),表示给参数命名,名称就是括号中的内容public Student select(@Param(“aaaa”) String name,@Param(“bbbb”)int class_id); 给入参 String name 命名为aaaa,然后sql语句….where s_name= #{aaaa} 中就可以根据aaaa得到参数值了

Web层

基本使用
处理uri的部分
@PathVariable
当使用someUrl/{paramId},这时候的paramId可以通过@Pathvariab;e注解绑定它传过来的值到方法的参数上
代码:

@Controller 
@RequestMapping("/owners/{ownerId}") 
public class RelativePathUriTemplateController { 

  @RequestMapping("/pets/{petId}") 
  public void findPet(@PathVariable String ownerId,@PathVariable String petId, Model model) {     
    // implementation omitted 
  } 
}
@Controller  
@RequestMapping("/owners/{ownerId}")  
public class RelativePathUriTemplateController {  

  @RequestMapping("/pets/{petId}")  
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
    // implementation omitted  
  }  
}  

上面代码把URI template中变量ownerld的值和petld的值,绑定到方法的参数上。若方法参数名称和uri template中变量名称不一致,需要在@PathVariable(‘name’)指定uri template·中的名称

处理request body部分的注解:@RequestParam, @RequestBody;
@Requestparam

A)注解有两个属性:value,required: value用来指定要传入的id名称,required用来指定参数是否必须1绑定
B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;
C)常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String–> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;

@Controller 
@RequestMapping("/pets") 
@SessionAttributes("pet") 
public class EditPetForm { 

    // ... 

    @RequestMapping(method = RequestMethod.GET) 
    public String setupForm(@RequestParam("petId")int petId, ModelMap model) { 
        Pet pet = this.clinic.loadPet(petId); 
        model.addAttribute("pet", pet); 
        return "petForm"; 
    } 
@Controller  
@RequestMapping("/pets")  
@SessionAttributes("pet")  
public class EditPetForm {  

    // ...  

    @RequestMapping(method = RequestMethod.GET)  
    public String setupForm(@RequestParam("petId") int petId, ModelMap model) {  
        Pet pet = this.clinic.loadPet(petId);  
        model.addAttribute("pet", pet);  
        return "petForm";  
    }  

@RequestBody

返回的是一个字符串,而不是一个页面

@RequestHeader和CookieValue
@RequestHeader注解,可以把Request请求header部分的值绑定到方法的参数上
Request的header部分

Host                    localhost:8080 
Accept                  text/html,application/xhtml+xml,application/xml;q=0.9 
Accept-Language         fr,en-gb;q=0.7,en;q=0.3 
Accept-Encoding         gzip,deflate 
Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive              300 

获取代码

@RequestMapping("/displayHeaderInfo.do") 
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding, 
                              @RequestHeader("Keep-Alive")long keepAlive)  { 

  //... 

} 

上述方法将Accept-Encoding绑定到了encoding中,
Keep-Alive参数绑定到了keepAlive上

@CookieValue可以把Request header中关于cookie的值绑定在方法的参数上

[java] view plain copy print?
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84  
@RequestMapping("/displayHeaderInfo.do") 
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie)  { 

  //... 

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值