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进去。

spring-boot 注解解析的更多相关文章

  1. Spring boot注解(annotation)含义详解

    Spring boot注解(annotation)含义详解 @Service用于标注业务层组件@Controller用于标注控制层组件(如struts中的action)@Repository用于标注数 ...

  2. Spring Boot 注解之ObjectProvider源码追踪

    最近依旧在学习阅读Spring Boot的源代码,在此过程中涉及到很多在日常项目中比较少见的功能特性,对此深入研究一下,也挺有意思,这也是阅读源码的魅力之一.这里写成文章,分享给大家. 自动配置中的O ...

  3. Spring Boot注解大全,一键收藏了!

    本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天是猿灯塔“365天原创计划”第5天. 今天呢!灯塔君跟大家讲: Spring Boot注解大全 一.注解(annotations)列表 @Spr ...

  4. Spring boot 注解简单备忘

    Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...

  5. 73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 针对于Spring Boot提供的注解,如果没有好好研究一下的话,那么想应用自如Spring Boot的话,还是有点困难的,所以我们这小节,说说S ...

  6. Spring 缓存注解解析过程

    Spring 缓存注解解析过程 通过 SpringCacheAnnotationParser 的 parseCacheAnnotations 方法解析指定方法或类上的缓存注解, @Cacheable ...

  7. Spring Boot 注解详解

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  8. Spring boot注解使用

    1:@SpringBootApplication 注解 a:scanBasePackages 与scanBasePackageClasses配置Spring启动时扫描的包路径或者扫描的字节码文件 b: ...

  9. spring mvc注解和spring boot注解

    1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...

  10. spring boot注解

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

随机推荐

  1. C#内联函数 特性 MethodImplOptions.AggressiveInlining)

    [MethodImpl(MethodImplOptions.AggressiveInlining)] 内联函数 Impl:implement的缩写 内联函数 在计算机科学中,内联函数(有时称作在线函数 ...

  2. OJ教程--排序算法

    1 算法分类 十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间非比较类排序:不 ...

  3. MongoDB聚合查询及Python连接MongoDB操作

    今日内容概要 聚合查询 Python操作MongoDB 第三方可视化视图工具 今日内容详细 聚合查询 Python操作MongoDB 数据准备 from pymongo import MongoCli ...

  4. (第二章第四部分)TensorFlow框架之TFRecords数据的存储与读取

    系列博客链接: (第二章第一部分)TensorFlow框架之文件读取流程:https://www.cnblogs.com/kongweisi/p/11050302.html (第二章第二部分)Tens ...

  5. VMware虚拟机三种网络模式

    VMware三种网络模式 在学习中经常遇到Vmware虚拟机网络设置有问题,可能是因为你没有理解这三种网络模式的工作原理.VMware虚拟机常见的网络类型有bridged(桥接).NAT(地址转换). ...

  6. Git拉取远程仓库代码并更新本地仓库

    1.git pull:获取最新代码到本地,并自动合并到当前分支 //查询当前远程分支 $ git remote -v //直接拉取并合并最新代码 $ git pull origin master [示 ...

  7. think php 登录日记

    */ public function save(Request $request) { // $params = $request->param(); $file = $request-> ...

  8. linux下更改文件字符格式为uft-8

    liunx下发布的.net Core 程序,发现短信签名不错误不能发出.后来检查发现配配文件中的字符为乱码才知道是因为字符格式问题. 因为服务器批较多,还是使用命令来解决比较快.使用iconv来更改. ...

  9. CF549G题解

    变菜了,一年前做这种题10min出结论,现在对着样例胡半天都没结果 首先考虑从判断无解入手. 定义两个位置 \((i,j)\),若 \(a[i]=a[j]+(j-i)\),则 \(i\) 和 \(j\ ...

  10. Java GUI 实现发送邮件以及附件

    实现代码: 注意点: 需要的jar包:JavaMail API 和Java Activation Framework (JAF) ,下载可参考菜鸟教程 默认使用QQ邮箱发送,需要设置授权,设置--&g ...