星光不问赶路人,时光不负有心人
@PathVariable
@PathVariable 映射 URL 绑定的占位符通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。
一般与@RequestMapping(method = RequestMethod.GET)一起使用,若方法参数名称和需要绑定的uri中变量名称一致时,可以简写。
@RequestMapping("/delete/{id}")
@ResponseBody //返回json给前端
public String delete(@PathVariable("id") Integer id){
Integer count=accountService.deleteAccount(id);
return ""+count;
@Resource
@Resource和@Autowired注解都是用来实现依赖注入的。只是@AutoWried按by type自动注入,而@Resource默认按byName自动注入。
@Autowired
自动装配通过类型,如果Autowired 不能唯一自动装配属性,则需要通过@Qualifier(value=“xxx”)
@Autowired
//调用spring容器
private AccountService accountService;
@Nullable
字段标记了这个注解,说明这个字段可以为null
@Repository
dao层,相当于在spring中声明数据访问层(跟component效果差不多)
@Component
组件,放在类上,说明这个类被spring管理了,就是bean,默认bean的id是类名是类名首字母小写。
@Component
public class AccountMapper {
}
@bean
bean跟component都可以实例一个bean,放入到IOC容器中;
@Bean:是放在方法上面的,表示把方法得返回值放入IOC容器中,方法的名称就是bean的id。
@value
相当于bean中的name值 ,给变量赋值;根据key读取属性文件。
@service
业务层,相当于在spring中声明业务层
@Service
public class AccountServiceImpl implements AccountService {
}
@controller
控制层,相当于在spring中声明控制层。
@Controller
@RequestMapping("/account")
public class AccountController {
}
@import
@Import注解支持导入普通的java类,并将其声明成一个bean。
@Repository
当Service需要使用Spring创建的名字叫“userDao”的UserDaoImpl实例时,就可以使用@Resource(name = “userDao”)注解告诉Spring,
Spring把创建好的userDao注入给Service即可。
@scope
作用域:默认是单例模式,即scope=”singleton”。另外scope还有prototype、request、session、global session作用域。scope=”prototype”多例
@RequestBody
主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。
@RequestMapping(value = "/test", Method = RequestMethod.POST, produces = "application/json;charset="UTF-8")
@ResponseBody
public JSONObject class Test(@RequestBody Test test) {
String name = test.getName();
// 将 name 回显
Map<String, Object> map = new HashMap<>();
map.put("name", name);
JSONObject obj = JSONObject.fromObject(map);
return obj;
}
@ResponseBody
该注解用于将 Controller 的方法返回的对象,通过适当的 HttpMessageConverter 转换为指定格式后,写入到 Response 对象的body 数据区
@RequestMapping("/delete/{id}")
@ResponseBody //返回json给前端
public String delete(@PathVariable Integer id){
Integer count=accountService.deleteAccount(id);
return ""+count;
}
@RequestParam
@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
@RestController
此注解相当于@ResponseBody + @Controller合在一起的作用。如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
@Lazy
注解用于标识bean是否需要延迟加载,源码如下:
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
/**
* Whether lazy initialization should occur.
*/
boolean value() default true;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
以上三个注解自动生成实体类中方法,使用时idea需要下载lombok插件,pom.xml中需要导入lombok依赖
@Data //生成get set tostring方法
@NoArgsConstructor //生成无参构造
@AllArgsConstructor //生成有参构造
public class Account {
private Integer id;
private String name;
private Double money;
}
@Configuration
@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的,作用为:配置spring容器(应用上下文)
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration容器启动初始化---");
}
}