spring-boot 注解解析
package com.hllq.quan.controller; import com.hllq.quan.mapper.WeiboUserMapper; import com.hllq.quan.model.WeiboUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; import com.hllq.quan.mapper.WeiboUserMapper; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @RestController public class WeiboUserController { @Autowired private WeiboUserMapper weiboUserMapper; @RequestMapping("/selectall") public List<WeiboUser> selectAll(){ return weiboUserMapper.selectAll(); } // @RequestMapping("/selectbyid/{id}") // public WeiboUser selectById(@PathVariable("id") int id){ // return weiboUserMapper.selectById(id); // } @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } // 获取url参数值,默认方式,需要方法参数名称和url参数保持一致http://localhost:8080/selectbyid?id=2 @RequestMapping(value = "/selectbyid",method = RequestMethod.GET) public WeiboUser selectById(@RequestParam Integer id){ return weiboUserMapper.selectById(id); } @RequestMapping(value = "/insert", method = RequestMethod.GET) public int insertWeiboUser( WeiboUser weiboUser){ weiboUserMapper.insertWeiboUser(weiboUser); return 1; } @RequestMapping(value = "/updatenamebyid" ,method = RequestMethod.GET) public void updateWeiboUserName(WeiboUser weiboUser){ weiboUserMapper.updateWeiboUserName(weiboUser); } @RequestMapping(value = "/deletebyid",method = RequestMethod.GET) public void deleteById(@RequestParam Integer id){ weiboUserMapper.deleteById(id); } }
@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集
@RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射。
value: 指定请求的实际地址 method: 指定请求的method类型, GET、POST、PUT、DELETE等 consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html; produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回 params: 指定request中必须包含某些参数值是,才让该方法处理 headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求
@Autowired:自动导入依赖的bean
@PathVariable指定URL变量名,前提是RequesrMapping需要用{}表面他的变量部分
@RequestMapping("/users/{username}") {username}就是我们定义的变量规则,username是变量的名字 获取的时候: public String userProfile(@PathVariable String username) 或者: public String userProfile(@PathVariable("username") String username) 存在多个的时候: @RequestMapping("/user/{username}/blog/{blogId}") @ResponseBody public String getUerBlog(@PathVariable String username , @PathVariable int blogId) 也可以使用正则表达式,匹配更加精确的变量,定义语法是{变量名:正则表达式} @RequestMapping("/user/{username:[a-zA-Z0-9_]+}/blog/{blogId}")
@RequestParam:将请求参数绑定到你控制器的方法参数上
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””) value:参数名 required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。 defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,
在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,
Spring就会去找到对应的editor进行转换,然后再SET进去。
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在 .NET 中使用内存映射文件构建高性能的进程间通信队列
· 一个 java 空指针异常的解决过程
· 揭开 SQL Server 和 PostgreSQL 填充因子的神秘面纱
· 没有调度器的协程不是好协程,零基础深入浅出 C++20 协程
· 别做抢活的导演:代码中的抽象层次原则
· 今年失业的程序员兄弟姐妹们,你们都去干什么了?
· TinyEditor v4.0 alpha 版本发布:表格更强大,表情更丰富,上传体验超乎想象!
· .NET周刊【7月第2期 2025-07-13】
· 圆方树学习笔记 —— 一种关于点双连通分量的思考方式
· MySQL 17 如何正确地显示随机消息?