1.@SpringBootApplication:
@SpringBootApplication是Spring Boot中最核心的注解,是Spring Boot的启动类注解。
@SpringBootApplication是一个组合注解,由@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan三个注解组合而成。
作用:标记说明这个类是Spring Boot的主配置类,Spring Boot可以通过运行这个类里面的main()方法来启动程序,同时这个注解会扫描主配置类所在的包以及子包中的类12。
代码如下显示:
@@SpringBootApplication
public class SpringbootMyTestApplication {
public static void main(String[] args) {SpringApplication.run(SpringbootMyTestApplication.class,args);
}
}
2.@RestController:
@RestController是一个SpringFramework中的注解,用于标识一个类是一个控制器,并且该控制器中的方法返回的数据不是视图页面,而是直接作为响应内容发送到客户端,通常是以JSON格式返回数据。
@RestController注解的主要作用是将控制器中的方法的返回值直接序列化为JSON或其他格式的数据,而不是渲染为视图页面。
在Spring中,通常有两种方式来创建控制器类:@Controller和@RestController。@Controller用于传统的MVC模式,它返回视图页面;而@RestController用于RESTful API,它返回数据。
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}
}
在这个例子中,@GetMapping注解定义了一个GET请求的处理器方法,并使用@ResponseBody注解将返回的字符串作为响应体返回给客户端。@RestController也支持@RequestMapping注解,用于映射请求
3.@RequestMapping:
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。
- 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
- 用于方法上,表示该方法处理请求的路径,是类路径与该方法路径的拼接。
例如,可以在@RestController中定义一个处理GET请求的方法,并使用@RequestMapping注解指定请求的URL和请求方法,如下所示:
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
// 根据id查询用户信息
User user = userService.getUserById(id);
return user;
}
}
@RequestMapping还可以对类进行标记,这样类中的处理方法在映射请求路径时,会自动将类上@RequestMapping设置的value拼接到方法中映射路径之前,如下:
@Controller
@RequestMapping(value="/demo")
public class DemoController{
@RequestMapping(value="/home",method=RequestMethod.GET)
public String home(){
return "/home";}
}
4. @Autowired:
@Autowired注解用于实现自动装配
@Autowired注解可以用于成员变量、方法及构造函数上,让Spring完成自动装配1。
@Autowired注解默认是按照类型进行匹配,如果需要按名称进行装配,则需要配合@Qualifier注解使用。
(1)用于自动装配 Bean,通过类型进行依赖注入。
@Component
public class BeanConfig{
@Autowired
private BeanConfig beanConfig;
@Autowired
public BeanConfig(BeanConfig beanConfig) {
this.beanConfig = beanConfig;
}
}
(2)可以标注在字段、构造方法、Setter 方法上,Spring 会自动寻找匹配的 Bean 进行注入。
在成员变量上使用Autowired注解,这种方法平时用的最多。
@Service
public class UserService {
@Autowired
private IUser user;
}
在构造器上使用Autowired注解:
@Service
public class UserService {
private IUser user;
@Autowired
public UserService(IUser user) {
this.user = user;
System.out.println("user:" + user);
}
}
在普通方法上加Autowired注解:
@Service
public class UserService {
@Autowired
public void test(IUser user) {
user.say();
}
}
也可以在setter方法上Autowired注解:
@Service
public class UserService {
private IUser user;
@Autowired
public void setUser(IUser user) {
this.user = user;
}
}
5.@Service
、@Repository
、@Component
:
(1)用于标识一个类为服务层、数据访问层或普通组件。
@Service:对应服务层,主要涉及一些复杂的逻辑,需要用到Dao层
@Service
public class UserServiceImpl implements UserService {
// 业务逻辑代码
}
@Repository:对应持久层(Dao)主要用于数据库相关操作
@Repository
public class UserDaoImpl implements UserDao {
// 数据访问代码
}
@Component:通用注解,可标注任意类为Spring组件。如果一个Bean不知道属于哪个层,可以使用@Component注解标注
@Component
public class MyComponent {
// 其他代码
}
(2)Spring 会自动扫描并注册这些 Bean,使它们可以被注入到其他需要的地方。
@Service
public class VehicleService extends ServiceImpl<VehicleMapper, Vehicle> {
@Autowired
public VehicleService(VehicleMapper mapper){
this.mapper=mapper;
}
public List<Vehicle> getAll(){
List<Vehicle> result=mapper.getAll();
return mapper.getAll();
}
public Vehicle getById(Long id) {
return mapper.getById(id);
}
public boolean insert(Vehicle entity) {
int result=mapper.insert(entity);
return result>0?true:false;
}
public boolean update(Vehicle entity) {
int result=mapper.update(entity);
return result>0?true:false;
}
public boolean delete(Long id) {
int result=mapper.deleteById(id);
return result>0?true:false;
}
6.@GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
:
(1)用于处理特定 HTTP 方法的请求,分别对应 GET、POST、PUT、DELETE 请求。
- @GetMapping
@GetMapping
注解用于处理 GET 请求。它允许你通过 URL 查询参数来获取数据。
@RestController
public class ExampleController {
@GetMapping("/example")
public String getExample() {
return "GET request received";
}
}
2. @PostMapping
@PostMapping
注解用于处理 POST 请求。它通常用于创建新的数据。
@RestController
@RequestMapping("/example")
public class ExampleController {
@PostMapping("/create")
public ResponseEntity<String> create(@RequestBody ExampleRequest request) {
//处理请求的逻辑
return ResponseEntity.ok("Success");
}
}
3.@putMapping
@PutMapping
注解用于处理 PUT 请求。它通常用于更新现有的数据。
@RestController
@RequestMapping("/example")
public class ExampleController {
@PutMapping("/{id}")
public ResponseEntity<?> updateExample(@PathVariable("id") long id, @RequestBody Example obj) {
// TODO: 实现更新逻辑
return ResponseEntity.ok().build();
}
}
4.DeleteMapping
@DeleteMapping 是一个用于处理HTTP DELETE请求的注解,它用于映射删除资源的操作1。
@DeleteMapping 注解通常用于RESTful API中,用于删除指定资源。其中 "{id}" 表示该资源的唯一标识符,是一个动态的参数,可以通过路径变量获取。
@RestController
@RequestMapping("/users")
public class UserController {
// 删除用户
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteUser(@PathVariable Long id) {
// 执行删除用户的业务逻辑
boolean success = userService.deleteUserById(id);
// 根据成功与否返回不同的响应
if (success) {
return new ResponseEntity<>("User deleted successfully", HttpStatus.OK);
} else {
return new ResponseEntity<>("Failed to delete user", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
(2)可以用在控制器的处理方法上,指定处理特定类型的 HTTP 请求。
7.@PathVariable
:
(1)用于将 URL 中的模板变量绑定到方法的参数上。
(2)可以在处理方法的参数上使用,从而获取 URL 中的变量值
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleController {
@RequestMapping("/example/{id}")
public String getExample(@PathVariable String id) {
// 你的逻辑代码,例如根据id获取数据
return "Received ID: " + id;
}
}
在这个例子中,当请求URL包含一个路径参数(例如/example/123
),Spring会将这个路径参数(也就是123
)绑定到getExample
方法的id
参数上。这样你就可以在方法体中使用这个参数进行相应的操作。
8.@RequestParam
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleController {
@RequestMapping("/example")
public String getExample(@RequestParam(name="id") String id) {
// 你的逻辑代码,例如根据id获取数据
return "Received ID: " + id;
}
}
(1)用于将 HTTP 请求参数绑定到方法的参数上。
(2)可以在处理方法的参数上使用,从而获取特定名称的请求参数值。
用法:@RequestParam注解可以用在方法的参数上,用于获取请求中的参数值1。
参数名称匹配:默认情况下,@RequestParam会根据参数的名称来匹配请求中的参数名1。
指定参数名:可以使用@RequestParam注解的name属性来显式指定请求参数的名称1。
必需参数:默认情况下,@RequestParam指定的参数是必需的,如果请求中缺少该参数,将会报错1。
默认值:可以使用defaultValue属性来设置请求中参数的默认值1。