@data注解_还怕不记得Spring Boot注解吗?5类注解全在这里了(建议收藏)

前言

使用注解的优势:

1.采用纯java代码,不在需要配置繁杂的xml文件

2.在配置中也可享受面向对象带来的好处

3.类型安全对重构可以提供良好的支持

4.减少复杂配置文件的同时亦能享受到springIoC容器提供的功能

87c251a4-2244-4a5e-8ee1-0071b3424c50

Spring Boot的核心就是注解。Spring Boot通过各种组合注解,极大地简化了Spring项目的搭建和开发。在Spring Boot中有一些注解是其中的关键,必须掌握。接下来就给大家做详细的介绍。

一、注解(annotations)列表

@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。

@Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。

@EnableAutoConfiguration 自动配置。

@ComponentScan 组件扫描,可自动发现和装配一些Bean。

@Component可配合CommandLineRunner使用,在程序启动后执行一些基础任务。

@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。

@Autowired自动导入。

@PathVariable获取参数。

@JsonBackReference解决嵌套外链问题。

@RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。

二、注解(annotations)详解

@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

package com.example.myproject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScanpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

@ResponseBody:表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用。示例代码:

@RequestMapping(“/test”)@ResponseBody public String test(){return”ok”;}

@Controller:用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。示例代码:

@Controller @RequestMapping(“/demoInfo”)publicclass DemoController {@Autowired private DemoInfoService demoInfoService;@RequestMapping("/hello")public String hello(Map map){System.out.println("DemoController.hello()");map.put("hello
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面给出一个简单的用户登录案例,使用了Spring Boot框架、MyBatis、MySQL数据库。 首先,需要在pom.xml文件中添加相关依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> </dependencies> ``` 然后,在application.properties文件中配置数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml ``` 接下来,创建一个User实体: ```java @Data public class User { private Integer id; private String username; private String password; } ``` 然后,在resources目录下创建mapper/UserMapper.xml文件,编写SQL语句: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="userMap" type="com.example.demo.entity.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="findByUsernameAndPassword" resultMap="userMap"> select * from user where username=#{username} and password=#{password} </select> </mapper> ``` 然后,在com.example.demo.mapper包下创建UserMapper接口: ```java @Mapper public interface UserMapper { User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password); } ``` 接着,在com.example.demo.service包下创建UserService接口和实现UserServiceImpl: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findByUsernameAndPassword(String username, String password) { return userMapper.findByUsernameAndPassword(username, password); } } ``` 最后,在com.example.demo.controller包下创建UserController: ```java @Controller public class UserController { @Autowired private UserService userService; @PostMapping("/login") @ResponseBody public String login(String username, String password) { User user = userService.findByUsernameAndPassword(username, password); if (user == null) { return "登录失败"; } else { return "登录成功"; } } } ``` 这样,我们就完成了一个简单的用户登录案例。需要注意的是,这里只是一个简单的示例,实际应用中还需要进行参数校验、异常处理等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值