ssm框架常用注解整理
SpringBoot
- Controller层:
@RestController
* 启动类:
@MapperScan("edu.cque.mapper")
@SpringBootApplication
+ 其他情况:
@Autowired
@component
@Bean
@Bean 和 @Autowired 做了两件完全不同的事情:
@Bean 告诉 Spring:“这是这个类的一个实例,请保留它,并在我请求时将它还给我”。
@Autowired 说:“请给我一个这个类的实例,例如,一个我之前用@Bean注释创建的实例”。
SpringMvc专用注解
- 启动类:
@SpringBootApplication//启动类SpringMVC专用注解
//spring提供的,表示自己是一个启动类,用来启动服务器
- Controller层:
- 类上:
@RestController
@RequestMapping("car")
@CrossOrigin //放行js的访问请求
@PropertySource(value = "classpath:/mysql.properties",encoding = "utf-8")//读取/引入resource下的properties和yml配置文件
@Value("${mysql.user}")//和propertySource配置使用,根据配置文件里面的key对字段进行依赖注入
- 方法上:
@RequestMapping("test1")
- 字段上:
@Autowired
@Value("${mysql.user}")
参数前:
@Param(“uId”)
restFull类型参数解析:
@path Variable无参则默认绑定后面的变量名以适配请求路径参数名,
如果指定参数,则按指定参数名去匹配请求路径参数名.
Spring专用注解
- 启动类:
@Configuration
@ComponentScan("edu.cque.day05")
@EnableAspectJAutoProxy //开启 Aspect 生成代理对象
- 代理类:
- 类名上:
@Aspect
@Component
- 方法上:
//切点表达式
@Pointcut("execution(* edu.cque.day05.*.add(..))")
//通知类型
@Before("pointcut()")
@After("pointcut()")
@AfterReturning("pointcut()")
@AfterThrowing("pointcut()")
@Around("pointcut()")
Mybatis专用注解
- 实体类:
@Accessors(chain = true) //开启链式加载
@Data
@NoArgsConstructor//无参构造
@AllArgsConstructor//全参构造
自己的学习笔记,后续持续更新中…
- 课外拓展:
@MapperScan- 1.首先了解@Mapper
在持久层的接口上添加@Mapper注解,编译后会生成相应的接口实现类,但由于要在每个接口上都进行配置,所以产生了@MapperScan。 - 2.@MapperScan
指定要编译成接口实现类的包路径,在编译完成后这个包下的所有接口都会生成相应的接口实现类。
- 1.首先了解@Mapper
- @ComponentScan
1.会自动扫描包路径下的@Controller、@Service、@Repository、@Component类,符合扫描规则的类会装配到spring容器中。
2.@MapperScan和@ComponentScan可以同时使用
如果@MapperScan和@ComponentScan扫描的是同一路径会产生错误,
原文链接:https://blog.youkuaiyun.com/weixin_45948234/article/details/110954215