Mybatis
@Alias("hello")
public class User {}
@Alias:类别名
@Delete("delete from user where id=#{id}")
void deleteUser02(@Param("id") int id);
@Select:查
@Insert:增
@Update:改
@Delete:删
@Param:
- 基本类型的参数或者String类型,需要加上
*- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,但是建议大家都加上!
- 我们在SQL中引用的就是我们这里的@Param()中设定的属性名!
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String password;
}
@Data:get,set,toString,hashcode,equals,其中无参构造是默认加的,不是@Data注解加的。
@AllArgsConstructor:有参构造
@NoArgsConstructor:无参构造
@SuppressWarnings("all") //抑制警告
public class IDUtils {
public static String getId(){
return UUID.randomUUID().toString().replaceAll("-","");
}
}
@SuppressWarnings(“all”):抑制警告,值为all代抑制所有
Spring
@component (将普通JavaBean实例化到spring容器中,Spring容器统一管理,用起来不用自己new了.
- 相当于配置文件中的
@ComponentScan:指定要扫描的package;
@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑。
public class People {
//如果显示的定义了Autowired的required的属性为false,说明这个对象可以为null,否则不允许为空
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
@Resource(name="cat502")
private Cat cat;
}
@Autowired:实现自动装配
@Nullable:说明这个字段可以为null
@Resource:实现自动装配
@Resource和@Autowired的区别
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】
- @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】
- 执行顺序不同:@Autowired通过byType的方式实现.
package com.kuang.config;
import com.kuang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//这个也会Spring容器托管,注册到容器中,因为他本来就是一个@Component
//@Configuration代表这一个配置类,就和我们之前看的beans.xml一样
@Configuration
@ComponentScan("com.kuang.pojo")
@Import(KuangConfig2.class)
public class KuangConfig {
//注册一个bean,就相当于我们之前写的一个bean标签
//这个方法的名字,就相当于bean标签中的id属性
//这个方法的返回值,就相当于bean标签中的class属性
@Bean
public User getUser(){
return new User();
}
}
@Configuration:代表这一个配置类,就和我们之前看的beans.xml一样
@Bean:注册一个bean,就相当于我们之前写的一个bean标签
SpringMVC
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody //他就不会走视图解析器,会直接返回一个 字符串
public String hello(Model model){
//封装数据
model.addAttribute("msg","hello,springmvcAnnotation");
return "hello";
}
}
@Controller:为了让Spring IOC容器初始化时自动扫描到;
@RequestMapping:为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/HelloController/hello;
@ResponseBody:他就不会走视图解析器,会直接返回一个字符串