Spring MVC 相关注解
@InitBinder
类型转换,一般用于时间转换或者数据类型转换.
页面上时间传递字符串,DB中是datetime
例:
1 private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 2 3 @InitBinder 4 public void dataBinder(WebDataBinder dataBinder) { 5 dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { 6 @Override 7 public String getAsText() { 8 if (getValue() != null) { 9 return sdf.format((Date) getValue()); 10 } 11 return null; 12 } 13 14 @Override 15 public void setAsText(String text) throws IllegalArgumentException { 16 if (text != null) { 17 try { 18 setValue(sdf.parse(text)); 19 } catch (ParseException e) { 20 log.error("日期解析错误", e); 21 } 22 } 23 24 } 25 }); 26 }
@RequestMapping
用来定义访问的URL, 一般在类名上指定,也可以在方法上指定
类名:
@Controller @RequestMapping("/test") public class Test extends BaseController { //例1 "/test/tmp1" @RequestMapping(value = "tmp1", method = RequestMethod.GET) public String tmp1(){ return "1"; } //例2: "/test/tmp2/10" @RequestMapping(value = "/tmp2/{id}",method = RequestMethod.GET) public String tmp2(@PathVariable("id") Long id){ return "2"; } //例3: "/test/tmp2?id=10" @RequestMapping(value = "/tmp2",method = RequestMethod.GET, params = "id = id") public String tmp3(){ return "3"; } }
@Controller
负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名
@Controller
public class Test {}
@Controller ("tmpTest")
public class Test {}
@Autowired
controller bean注入
@Service
service bean注入
@Repository
dao bean注入
@Value
@Value("${i.url}")
private String IUrl;//注入配置中的值
@ResponseBody
将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)
@RequestBody
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
详情:http://blog.csdn.net/kobejayandy/article/details/12690555
@PageableDefault
@RequestParam
用来处理简单类型的绑定,通过Request.getParameter() 获取的值,request默认为true
1 @RequestMapping(value = "/test", method = RequestMethod.GET) 2 @ResponseBody 3 public Result Test(@RequestParam(value = "id", required = true) Long id) { 4 5 return null; 6 }
@Transactional
事务注解
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
时间类型转换注解
Lombok
@Slf4j
日志注解
@EqualsAndHashCode
重写equals和hashCode方法注解
@Data
重写get/set方法注解
@ToString
重写toString方法注解