Spring Boot 中的几个常用的注解(用停车场练习项目演示)

本文详细介绍了SpringBoot中的关键注解,如@SpringBootApplication(主类标识)、@Autowired(自动装配)、@Service(服务层)、@RequestMapping(请求映射)、@Data(简化通用方法)、@Mapper(MyBatis映射器)和@PathVariable(路径变量),帮助开发者提升开发效率。
摘要由CSDN通过智能技术生成

目录

1. @SpringBootApplication(主类标识注解)

2. @Autowired(自动装配Bean注解)

3. @Service(服务层注解)

4. @RequestMapping(映射控制器方法注解)

5.@Data(自动生成通用方法注解)

6.@Mapper(接口类注解)

7.@RequestMapping(请求映射注解)

8.@PathVariable(参数传递注解)


在现代的 Java 开发中,Spring Boot 成为了构建企业级应用的首选框架之一。Spring Boot 的优雅设计和丰富的功能使得开发者能够更专注于业务逻辑,而不必过多关心繁琐的配置。本文将深入探讨 Spring Boot 中一些常用的注解,这些注解为开发者提供了强大的功能和便利的开发体验。

1. @SpringBootApplication

@SpringBootApplication 注解标识了一个 Spring Boot 应用程序的主类。这个注解实际上是一个组合注解,包括 @Configuration@EnableAutoConfiguration@ComponentScan。通过这个注解,Spring Boot 能够自动扫描组件,自动配置应用程序,并基于 Java 配置进行配置。

package com.example;
//使用样例
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ParkingMisApplication {

    public static void main(String[] args) {
        SpringApplication.run(ParkingMisApplication.class, args);
    }

}

2. @Autowired

@Autowired 注解用于自动装配 Spring 容器中的 Bean。可以在字段、构造函数、Setter 方法上使用。通过 @Autowired,我们无需手动创建 Bean,Spring 会自动为我们解析依赖关系。

package com.example.service;
import com.example.entity.ParkingLot;
import com.example.entity.Vehicle;
import com.example.mapper.VehicleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class VehicleService {
    VehicleMapper mapper;
    @Autowired
//使用样例
    public VehicleService(VehicleMapper mapper){
        this.mapper=mapper;
    }
    public List<Vehicle> getAll(){
        return mapper.getAll();
    }
    public Vehicle getById(Long id) {
        return mapper.getById(id);
    }
    public int update(Vehicle entity) {
        return mapper.update(entity);
    }
    public int insert(Vehicle entity) {
        return mapper.insert(entity);
    }
    public boolean delete(Long id) {
        return mapper.deleteById(id)>0?true:false;
    }
}

3. @Service

@Service 注解用于标识一个类为服务层(Service),通常用于标记业务逻辑的实现类。与 @Component 相似,但更具业务语义。

package com.example.service;
import com.example.entity.ParkingLot;
import com.example.entity.Vehicle;
import com.example.mapper.VehicleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
//使用样例(与上图相同)
public class VehicleService {
    VehicleMapper mapper;
    @Autowired
    public VehicleService(VehicleMapper mapper){
        this.mapper=mapper;
    }
    public List<Vehicle> getAll(){
        return mapper.getAll();
    }
    public Vehicle getById(Long id) {
        return mapper.getById(id);
    }
    public int update(Vehicle entity) {
        return mapper.update(entity);
    }
    public int insert(Vehicle entity) {
        return mapper.insert(entity);
    }
    public boolean delete(Long id) {
        return mapper.deleteById(id)>0?true:false;
    }
}

4. @RequestMapping

@RequestMapping 注解用于映射 HTTP 请求路径到具体的控制器方法。可以用在类级别和方法级别。通过这个注解,我们能够定义 RESTful 风格的 API。

package com.example.controller;

import com.example.entity.SysUser;
import com.example.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/user")
//使用样例
public class SysUserController {
    SysUserService service;
    @Autowired
    public SysUserController(SysUserService service){
        this.service=service;
    }
    @RequestMapping("/list")
    public String list(Model model){
        model.addAttribute("list",service.getAll());//model打包成list属性完成list对象的值传递
        return "/user/list";
    }

5.@Data

在SPringleBoot中,@Data 注解是 Lombok 提供的一个注解,用于自动生成类的一些通用方法,如 toStringequalshashCode 等。使用 @Data 注解可以减少冗余代码,提高开发效率。

package com.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
//收费标准
@Data
//@Data注解使用
@TableName("pricing_standard")
public class PricingStandard {
    @TableId(type= IdType.AUTO)
    private Long id;
    @TableField("hourly_rate")
    private Integer hourlyRate;//小时计费
    @TableField("daily_rate")
    private Integer dailyRate;//按天计费
    @TableField("exceeding_hours")
    private Integer exceedingHours;//超时数,超过多少小时按天计费
    @TableField("update_time")
    private Date updateTime;//更新日期

}

6.@Mapper

在 Spring Boot 中,@Mapper 注解通常与 MyBatis 框架一起使用,用于标识一个接口或类是 MyBatis 的映射器(Mapper)。MyBatis 是一个持久层框架,它通过 XML 或注解的方式提供了简单的 SQL 映射配置。@Mapper 注解则起到了标识 MyBatis 映射器的作用,使 Spring Boot 能够扫描并注册这些映射器。

package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Vehicle;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface VehicleMapper extends BaseMapper<Vehicle> {
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "parking_lot_id", property = "parkingLot", one = @One(select = "com.example.mapper.ParkingLotMapper.getById")),
    })
    @Select("select * from vehicle join parking_lot on parking_lot.id=parking_lot_id")
    List<Vehicle> getAll();
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "parking_lot_id", property = "parkingLot", one = @One(select = "com.example.mapper.ParkingLotMapper.getById")),
    })
    @Select("select * from vehicle join parking_lot on parking_lot_id=parking_lot.id where vehicle.id=#{id}")
    Vehicle getById(Long id);
    @Insert("insert into vehicle set licence_plate=#{licencePlate},pic_url=#{picUrl},is_active=#{isActive},parking_lot_id=#{parkingLot.id}")
    int insert(Vehicle entity);
    @Update("update vehicle set licence_plate=#{licencePlate},pic_url=#{picUrl},is_active=#{isActive},parking_lot_id=#{parkingLot.id} where id=#{id}")
    int update(Vehicle entity);
}

7.@RequestMapping

在 Spring Boot 中,@RequestMapping 注解是一个用于处理请求映射的注解。它可以用在类级别和方法级别,用于指定请求的路径、HTTP 方法以及其他相关的属性。@RequestMapping 注解提供了灵活的方式来定义 URL 映射规则,用于将 HTTP 请求映射到相应的处理方法。

@RequestMapping("/list")
    public String list(Model model){
        model.addAttribute("list",service.getAll());//model打包成list属性完成list对象的值传递
        return "/vehicle/list";
    }

8.@PathVariable

在 Spring Boot 中,@PathVariable 注解用于从 URL 中获取路径变量的值。路径变量是 URL 中的一部分,用于在请求中传递数据。@PathVariable 注解可以将路径变量映射到控制器方法的参数上,方便获取并处理这些变量。

 @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id")Long id, Model model){
        model.addAttribute("vehicle",service.getById(id));
        model.addAttribute("parkingLots",parkingLotService.getAll());
        return "vehicle/edit";
    }

spring Boot 提供了丰富而强大的注解来简化应用程序的开发和配置。通过深入了解这些注解的用途和原理,我们能够更好地利用 Spring Boot 的优势,提高开发效率,减少样板代码的编写。希望本文对你深入理解 Spring Boot 注解有所帮助。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值