SpringBoot
注解 | 作用 |
---|---|
@Configuration | 配置类,保持并拓展springboot配置,类型必须是WebMvcConfiguer |
@Bean | 注入配置 |
@PropertySource | 加载指定的配置文件【1】 |
@configurationProperties | 默认从全局配置文件中获取值 【2】 |
@validated | 校验数据 【3】 |
@Conditional | 根据不同的条件判断,如果满足指定的条件,整个配置类里面的配置就会生效 |
@Async | 告诉Spring这是一个异步方法,但是要让这个注解生效,我们还需要在主程序上添加一个注解@EnableAsync ,开启异步注解功能;【4,5,6】 |
@Scheduled(cron = “0 * * * * 0-7”) | 定时任务(秒 分 时 日 月 周几)【5】 |
@EnableScheduling | 开启基于注解的定时任务,在主程序上添加【6】 |
@Reference | 远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名【7】 |
@Accessors(chain = true) | 链式写法 默认false |
【3】
@NotNull(message=“名字不能为空”)
private String userName;
@Max(value=120,message=“年龄最大不能查过120”)
private int age;
@Email(message=“邮箱格式错误”)
private String email;
空检查
@Null 验证对象是否为null
@NotNull 验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
Booelan检查
@AssertTrue 验证 Boolean 对象是否为 true
@AssertFalse 验证 Boolean 对象是否为 false
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内
@Length(min=, max=) string is between min and max included.
日期检查
@Past 验证 Date 和 Calendar 对象是否在当前时间之前
@Future 验证 Date 和 Calendar 对象是否在当前时间之后
@Pattern 验证 String 对象是否符合正则表达式的规则
mybatis 层
注解 | 作用 |
---|---|
@Param | 基本类型的参数或者String类型,需要加上,引用类型不需要【1】 |
【1】
Sql中引用的就是@Param("")中设定的属性名
#{}加注解能很大程度防止sql注入。preparedStatement
${}中传入的值会进行拼接,无法防止sql注入 statement
spring层
注解 | 作用 |
---|---|
@Autowired | 在Spring中自动注入,通过类型。【1】 |
@Qualifier | 可指定beanId【2】 |
@Resource | 在Spring中是java层面的注解,也可实现自动装配。并且是组合注解【3】 |
@Nullable | 段标记了这个注解,说明这个字段可以为null; |
@Component @Repository @Service @Controller | 在Spring中组件放在类上,说明这个类被Spring管理了,就是bean【4】 |
@Value | 赋值【5】 |
@Scope() | 作用域:singleton,prototype【6】 |
@Aspect 、@Before、@Round | 在AOP中标注这个类是个切面,要开启aop注解支持 |
【1】
@Compoent和@Value可代替。
@Autowired就是自动装配属性,相当于
<!--property 可以直接注入属性的值-->
<bean id="user" class="com.my.User" p:name="属性值" p:age="属性值"/>
只负责自动装配p:name=" “,而具体属性的值由@Qualifier(value=”")来配合使用
使用注解前提:
1.导入约束
xmlns:context="http://www.springframework.org/schema/context"
======================================
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
2.开启注解的支持【重要】
<context:annotation-config/>
3.注意:最终还是通过byName实现,要注意实体类属性名和beanId一致。
【2】
如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
【3】
@Resource(name=“beanId”)指定beanId
自动注入通过名字,名字没找到,通过类型。相当于@Autowired+@Qualifier。
【4】
//等价于 <bean id="user" class="com.my.pojo.User"/>
@Component
public class User {
public String name="aaa";
}
@Conponent有几个衍生注解,我们在web开发中 ,会按照mvc三层框架分层!
#pojo 【@Conponent】
#dao 【@Repository】
#service 【@Service】
#controller 【@Controller】/*@Controller包含了映射器和适配器*/
它们四个是等价的,都会被装配到Spring中。
【5】
相当于<property name="name" value="张三"/>
@Component
public class User {
@Value("张三")
public String name;
}
【6】
singleton建议用于单线程,容器创建的对象的个数全局唯一。
不管调用多少次,只用全局这一个。
<bean id="user" class="com.my.User" p:name="张三" p:age="34" scope="singleton"/>
prototype建议用于多线程,容器创建的对象有多个。
调用一次,创建一次。
<bean id="user" class="com.my.User" p:name="张三" p:age="34" scope="prototype"/>
springMVC层
注解 | 作用 |
---|---|
@RequestMapping、 @GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping | 在Springmvc中可以使用约束请求类型 |
@PathVariable | 在Springmvc中可以使用@PathVariable注解,RestFul风格让方法参数的值对应绑定到一个Url模板变量 【1】 |
@RequestParam | 在Springmvc中@RequestParam约束1.参数只能从前端传过来,不能是随意的值 2.只能是约束的值 【2】 |
@ResponseBody | 在类上使用。不走视图解析器,直接在页面返回字符串【3】,配合Controller使用 |
@RestController | 指定类只返回字符串(JSon转换) |
【1】
RestFul风格
//以post方式去请求,不可以在url中传参
@PostMapping("/hello/{a}/{b}")//绑定URL模板,路径可以是任何符号
public String hello(@PathVariable String a, @PathVariable String b,Model model){
String res=a+b;
model.addAttribute("restful","结果为:PostMapping:"+res);
return "hello";//返回字符串是视图的名字,会被解析器解析
}
【2】
/*@Controller包含了映射器和适配器*/
@Controller
public class MyController {
/*@RequestParam约束1.参数只能从前端传过来,不能是随意的值 2.只能是username。*/
//url:http://localhost:8080/test1/t1?username=章
@GetMapping("/t1")
public String test1(@RequestParam("username")String name, Model model){
//1.从前端传到值,看控制台
System.out.println(name);
//2.将返回值的结果传送给前端,用Model
model.addAttribute("msg",name);
//视图跳转
return "add";
}
}
【3】
@Controller
public class UserController {
@RequestMapping("/json1")
@ResponseBody
public String json1() throws JsonProcessingException {
//创建一个jackson的对象映射器,用来解析数据
ObjectMapper mapper = new ObjectMapper();
//创建一个对象
User user = new User("s", 3, "男");
//将我们的对象解析成为json格式
String str = mapper.writeValueAsString(user);
//由于@ResponseBody注解,这里会将str转成json格式返回;十分方便
return str;
}
}