spring
--------------------------Spring------------------------------------------------------- Component 组件 零件 Service 更加语义化 Controller 更加语义化 Autowired 装配 车间 Mapper AnnotationConfigApplicationContext 扫描包 会先扫描有Component注解的类,创建对象 再去扫描Autowired注解,拿创建好的对象直接去注入依赖 @Qualifier("foodMapperImpl1") //装配时,如果发现有多个匹配的对象,使用Qualifier限定注解,指定一个对象 @Scope 作用域 @Repository 仓库 mapper访问数据库 //如果某个类的对象需要容器创建管理,但此类是第三方提供的 @Configuration //javaconfig方式必须创建一个有@Configuration的类 @Bean //证明是一个Bean ------------------------------Spring-AOP----------------------------------------------- @Component 组件(一般不是controller、service、mapper的去使用) @Aspect 定义此类为方面代码,即是一个通知。 证明这是一个切面类 @EnableAspectJAutoProxy 自动可切入代理 写在切面类上 和Aspect、Component同级 @Pointcut 切入点 配置路径用 示例:@Pointcut("execution(* com.neuedu.spring.aop.service.*.*(..))") execution 执行 返回值 引用(不是路径) 1*:所有类 2*:所有方法 (..):所有种类的参数类型 SpringAOP一共有5种通知的类型 前置通知 后置通知 返回通知 异常通知 环绕通知 @Before @After @AfterReturning @AfterThrowing @Around --------------------------------Spring+Mybatis----------------------------------------- @Configuration //声明该类是核心配置类 @MapperScan("com.xxx.sm.mapper") 包扫描 @EnableTransactionManagement 事务管理 @Bean @Transactional(rollbackFor = Throwable.class) 事务注解 在多个业务中,一个出现异常,让语句整体提交失败
springMvc
-----------------------------------------------------SpringMvc-------------------------------------------------------------
@RequestMapping("/hello") //请求映射,配置地址url
@ResponseBody //通知SpringMva将方法的返回值,直接以响应的形式发送给客户端
可加到方法上,可加在类上(类中所有方法都可以直接返回)
@Configuration
@ComponentScan("com.neuedu.springmvc") 组件扫描
@EnableWebMvc //使用EnableWebMvc开启springmvc注解方式配置
@RequestMapping("/user") 多了一级路径
@RequestMapping(value = "/lt666",method = RequestMethod.POST) 添加请求
@GetMapping("/pp") 地址映射 get请求,是上方RequestMapping的简写
@PostMapping("/m2") 地址映射 post请求
@RequestParam("n") 放在参数前面 用于前后端参数传递名字不一致。
public String m3(@RequestParam("n") String name)
*@RequestBody 通知SpringMvc前端数据的格式是 JSON 放在参数前
上传文件
类型:MultipartFile 多部件文件
Rest
--------------------rest使用一个URL,来对应4种对资源数据的操作-----------------------------
@RestController //相当于@Controller和@ResponseBody的组合
@PostMapping//post请求对应增加请求
@DeleteMapping("/{no}") // /{}对应的是变化的地址。delete请求对应删除请求
@PathVariable("no")
@PutMapping //put对应修改请求
@GetMapping //get对应查询所有数据请求
springboot
-----------------------------------------------------springboot--------------------------------------------------------
@SpringBootApplication //会默认扫描主启动类所在的包以及子包
@Value("${server.port}") //使用Value注解可以获取到配置文件中的某一个配置项的配置数据
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8") //将日期格式进行转换,实体类中
pom
--------------------------------------------------------------------->ssm中pom
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.txt</include> </includes> </resource> </resources>
yml
--------------------------------------------------------------------->ssm中yml
# 服务器设置 server: port: 5678 servlet: context-path: /ssm # jdbc数据库配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8&serverTimezone=GMT%2B8 username: root password: root # mybatis po别名 mybatis: type-aliases-package: com.neuedu.ssm.po mapper-locations: classpath:com.neuedu.ssm.mapper/*.xml # 日志配置 logging: level: com.neuedu.ssm: debug
外卖自学注解
启动类Application
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
Controller层
@RestController
@RequestMapping("/admin/category")
@Slf4j 日志 log.info("xxx:{}",emp)
@Api(tags = "员工相关接口") 定义后端接口文档名称
@Autowired
@GetMapping("/page")
@ApiOperation("分类分页查询") 定义后端接口文档具体功能名称
@RequestBody@PathVariable("id")
config 配置
@Configuration 配置
@Slf4j
Handler 全局业务处理
@RestControllerAdvice
@Slf4j
@ExceptionHandler
interceptor 拦截器
@Component
@Slf4j
pojo 实体类
dto
@Data
@TableId(value = "empno",type = IdType.ASSIGN_ID) //主键列注解 mybatisplus
//必须使用TableId注解来指定主键列的映射 //如果属性名和主键列名不一致,配置value属性映射。 //必须设置type属性指定主键类型 自己指定id:IdType.ASSIGN_ID 自动递增:IdType.AUTO
@TableField("job"):字段注解(非主键) mybatisplus
//如果非主键列,名字和属性名不一致使用TableField注解映射
@TableName("emp"):表名注解,标识实体类对应的表。 数据库和表名不一致 mybatisplus
entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
vo
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
自定义注解和切面
@Target(ElementType.METHOD) //指定注解作用范围 为 方法 @Retention(RetentionPolicy.RUNTIME) //固定套路,不知道嘎哈的
@Aspect //切面注解 @Component //交给spring容器管理 @Slf4j //日志