springboot常用注解

记录一下springboot常用注解及使用场景

 1.@SpringBootApplication

该注解用在运行类之上,相当于@EnableAutoConfiguration、@ComponentScan和@Configuration的合集。

@SpringBootApplication
public class SpringbootDemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootDemoApplication.class, args);
        
    }

}

  2.@Repository

注解在类上,表示这是一个数据访问层bean。

用于mapper软件包或dao软件包

@Repository
public interface ICommDAO {

    public List<Commodity> selectCommdity();
}

3.@Mapper

作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类

用于mapper或dao层的接口上

@Repository
@Mapper
public interface IStaffDAO {

    public List<Staff> selectStaffAndDept();
}

4.@MapperScan

作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类

添加位置:是在Springboot启动类上面添加

@MapperScan("com.example.springboot_demo.dao")
@SpringBootApplication
@EnableTransactionManagement
public class SpringbootDemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootDemoApplication.class, args);

    }

}

5.@Service

用于service层,标记当前类是一个service类

@Service
public class DeptServiceImpl implements DeptService{


    @Autowired
    private IDeptDAO deptDAO;

    @Override
    public List<Staff> staff_selectAll(){
//        System.out.println(deptDAO.staff_selectAll());
        return this.deptDAO.staff_selectAll();
    }
}

6.@Autowired

自动装配,帮助注入类的属性,方法等等

也可以用于注入接口等等

场景如上

7.@RestController

用于标注控制层组件,用在控制层实现异步请求,页面局部刷新。

@RestController
@RequestMapping("/DeptBoot")
public class DeptController {

    @Autowired
    private DeptService deptService;
}

8.@Controller

用于标注控制层组件,用在控制层实现同步请求,页面全部刷新

@Controller
@RequestMapping("/DeptBoot")
public class DeptController {

    @Autowired
    private DeptService deptService;
}

9.@RequestMapping

提供路由信息,负责URL到Controller中的具体函数的映射。

可以在方法上使用,也可以再类上使用

该注解有六个属性:

params:指定request中必须包含某些参数值是,才让该方法处理。

headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

value:指定请求的实际地址,指定的地址可以是URI Template 模式

method:指定请求的method类型, GET、POST、PUT、DELETE等

consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;

produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回

@RestController
@RequestMapping("/DeptBoot")
public class DeptController {

    @Autowired
    private DeptService deptService;


    @RequestMapping("/getstaff")
    public String GetStaff(Model model){
        List<Staff> list = this.deptService.staff_selectAll();
        System.out.println(list);
        model.addAttribute("list",list);
        return "index";
    }
}

10.@ResponseBody

用于方法上,帮助将列表或者对象封装成json返回前端

    @RequestMapping("/getstaff")
    @ResponseBody
    public HashMap selectStaff(){


        return hashMap;
    }

11.@RequestBody

接收请求体,接收到的对象,必须是实体类中定义的对象

一个请求最多只有一个@RequestBody

    @RequestMapping("/updatestaff")
    public String updatestaff(@RequestBody Staff staff){
}

12.@PathVariable

参数注入,用于Request风格传参,若没有(“”),默认寻找和定义变量相同字段的值

    @RequestMapping("/deletestaff/{id}")
    public String deleteStaff(@PathVariable("id") Integer id){
        this.staffService.deleteStaff(id);
        return "success";
    }

13.@RequestParam

用于接收前端传来的参数

 public HashMap selectStaffByDeptNameOrStaffName(
            @RequestParam(value="dept",required =false) String dept,
            @RequestParam(value="name",required = false)  String name){
        System.out.println(dept);
        System.out.println(name);
}

有几个参数就写几个@RequestParam,required表示校检该字段为空时是否报错。

14.@Configuration

多用于config软件包,表示该类是 Bean 配置的信息源,多与@Bean一起使用

@Configuration
public class CorsConfig {

    // 跨域请求处理
    @Bean
    public CorsFilter corsFilter() {
    }

}

15.@Data

lombok插件提供的直接,帮助实体类自动生成set,get,tostring方法

@Data
public class Staff implements Serializable {

    private Integer Id;

    private String name;

    private String wages;

    private String time;

    @JsonBackReference
    private Dept dept;

}

使用需要先去pom.xml文件中引入,及下载lombok插件

16.@JsonBackReference

用于解决RequestMap的无限递归

使用方法如上,在需要阻止递归的变上添加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值