@Test //测试
@Override //重写方法
JavaWeb:
@WebServlet("/servletDemo1") //配置servlet的映射
//注解详解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
MyBatis:
@Select("SELECT * FROM student") //查询
@Insert("...")
@Update("...")
@Delete("...")
mybatis的多表操作
public interface CardMapper {
//查询全部
@Select("SELECT * FROM card")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "number",property = "number"),
@Result(
property = "p", // 被包含对象的变量名
javaType = Person.class, // 被包含对象的实际数据类型
column = "pid", // 根据查询出的card表中的pid字段来查 询person表
one = @One(select = "com.itheima.one_to_one.PersonMapper.selectById") )
})
public abstract List<Card> selectAll();
}
Spring:
@Component
@Controlle
r@Service
@Repository //类定义上方,设置该类为spring管理的bean
@Scope //类定义上方,设置该类作为bean对应的scope属性
@PostConstruct
@PreDestroy //方法定义上方,设置该类作为bean对应的生命周期方法
@Bean
@Value //属性注解、方法注解,传参
@Value("${jdbc.username}")
@Autowired //属性注解、方法注解,设置对应属性的对象或对方法进行引用类型传参
@Qualifier //属性注解、方法注解,设置对应属性的对象或对方法进行引用类型传参
@Autowired(required = false)
@Qualifier("userDao")
private UserDao userDao;
//@Autowired默认按类型装配,指定@Qualifier后可以指定自动装配的bean的id
@Primary //类注解,设置类对应的bean按类型装配时优先装配
@Inject //@Inject与@Named是JSR330规范中的注解,功能与@Autowired和@Qualifier完全相同,适用
于不同架构场景
@Named
@Resource //@Resource是JSR250规范中的注解,可以简化书写格式
@PropertySource //加载properties文件中的属性值
@PropertySource(value = "classpath:filename.properties")
public class ClassName {
@Value("${propertiesAttributeName}")
private String attributeName;
}
@Configuration //类注解,设置当前类为spring核心配置加载类
@ComponentScan
@Configuration
@ComponentScan("scanPackageName")
public class SpringConfigClassName{ }
@Import //类注解,导入第三方bean作为spring控制的资源
@Configuration
@Import(OtherClassName.class)
public class ClassName { }
@DependsOn //类注解、方法注解,控制bean的加载顺序,使其在指定bean加载完毕后在加载
@DependsOn("beanId") public class ClassName { }
@Order(1) //类注解、方法注解,控制配置类的加载顺序
@Lazy //类注解、方法注解,控制bean的加载时机,使其延迟加载
//**AOP**
@Aspect //类注解,设置当前类为切面类
@Pointcut //方法注解,使用当前方法名作为切入点引用名称
@Pointcut("execution(* *(..))")
public void pt() {
}
@Before //方法注解,标注当前方法作为前置通知
@Before("pt()")
public void before(){ }
@After //方法注解,标注当前方法作为后置通知
@AfterReturning //返回后通知
@AfterReturning(value="pt()",returning = "ret")
public void afterReturning(Object ret) { }
@AfterThrowing //异常后通知
@AfterThrowing(value="pt()",throwing = "t")
public void afterThrowing(Throwable t){ }
@Around //环绕通知
@Around("pt()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object ret = pjp.proceed();
return ret;
}
@EnableAspectJAutoProxy //Spring注解配置类定义上方,设置当前类开启AOP注解驱动的支持,加载AOP注解
@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig { }
//**事务**
@Transactional //方法注解,类注解,接口注解,设置当前类/接口中所有方法或具体方法开启事务,并指定相关事务属性
@Transactional( readOnly = false,
timeout = -1,
isolation = Isolation.DEFAULT,
rollbackFor = {ArithmeticException.class, IOException.class},
noRollbackFor = {},
propagation = Propagation.REQUIRES_NEW
)
//tx:annotation-driven beans标签,开启事务注解驱动,并指定对应的事务管理器
@EnableTransactionManagement //Spring注解配置类上方,开启注解驱动,等同XML格式中的注解驱动
@Configuration @ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JDBCConfig.class,MyBatisConfig.class,TransactionManagerConfig.class })
@EnableTransactionManagement
public class SpringConfig { }
SpringMVC:
@Configuration
@ComponentScan(
value = "com.itheima",
includeFilters =@ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = {Controller.class} ) )
@RequestMapping("/requestParam2")
@ResponseBody //基于jackon技术,使用@ResponseBody注解可以将返回的POJO对象转成json格式数据
@RequestHeader("Accept-Encoding") //形参注解,获取指定名称请求头的值,并赋值给方法形参,用于替换 request.getHeader("请求头名称") 相关方法
@CookieValue("JSESSIONID") //形参注解,作用:获取指定名称Cookie的值,并赋值给方法形参,用于替换 request.getCookies(),Cookie.getValue() 系列方法
@SessionAttribute(value="name",required = false) //形参注解,作用:获取Session域中指定key对应的value,并赋值给方法形参,用于替换 session.getAttribute("key") 方法
@SessionAttribute(value="name",required = false) //形参注解,批量将所在类中,原本存入Model中的指定名称的数据存入session域
@CrossOrigin //使用@CrossOrigin开启跨域访问
@ControllerAdvice //编写异常处理类
@Component
@ControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
@ResponseBody
public Result doBusinessException(BusinessException e){
// 获取异常的状态的码
Integer code = e.getCode();
// 获取异常的提示信息 String message = e.getM essage();
// 将其封装到Result对象
Result result = new Result(message, code); return result;
} }
//**restful风格编码**
@RestController
@RequestMapping("/user")
@GetMapping("/{id}")
@PostMapping()
@DeleteMapping("/{id}")
@PutMapping()
springBoot:
@SpringBootApplication // 编写SpringBoot启动类
@ConfigurationProperties //可以实现让配置文件中的配置和实体类对象中的属性进行绑定的功能
@Component
@ConfigurationProperties("person")
public class Person {
private String name;
private int age;
}
@EnableConfigurationProperties(Person.class) // 启动类(启动类自身也是配置类)
@SpringBootTest //SpringBoot 整合junit
@RunWith(SpringRunner.class)
//类注解
//编写mapper接口
@Mapper
@Repository //引入UserMapper时不报红,但不影响
@Conditional //用于在向容器中注册Bean时添加一个自定义条件,满足@Conditional注解指定
的条件,才会将bean注册到Spring的ioc容器中
@Import
@SpringBootConfiguration
@SpringBootConfiguration
@EnableAutoConfiguration
@AutoConfigurationPackage
@MapperScan("com.itheima.mp.mapper") //添加Mapper包扫描
@TableName("tb_user") // 指定表名
@Data
@TableId(type = IdType.AUTO) 设置id生成策略:AUTO 数据库自增
//@TableField("user_name")
@PropertySource("classpath:mongo.properties")
MongoDB
@Document(collection = "ap_comment") //实体类注解,添加注解 @Document 与mongodb中的集合对应上