Java common annotations (continuously updated)

Notes on common annotations in Java (continuously updated):

1. Java basic annotations

1. @Override

Used to identify method rewriting, so that the rewritten method can be called when calling the parent class method.

public class Animal {
    
    
    public void move() {
    
    
        System.out.println("动物在移动");
    }
}

public class Dog extends Animal {
    
    
    @Override
    public void move() {
    
    
        System.out.println("狗在奔跑");
    }
}

In the above example, @Overridethe annotation identifies that the method Dogof the class moveoverrides the method Animalin the parent class move.

2. @Deprecated

Used to identify that the method or class is obsolete and not recommended.

@Deprecated
public class OldClass {
    
    
    // ...
}

public class NewClass {
    
    
    /**
     * @deprecated 不推荐使用
     */
    @Deprecated
    public void oldMethod() {
    
    
        // ...
    }
}

In the above example, @Deprecatedthe annotation identifies that the class OldClassis obsolete and the method oldMethodis also obsolete and not recommended.

3. @SuppressWarnings

Used to suppress compiler warnings.

@SuppressWarnings("unchecked")
List<String> list = new ArrayList();

In the above example, @SuppressWarningsthe annotation suppresses the compiler warning, and List<String> list = new ArrayList()the statement no longer emits a warning message.

4. @SafeVarargs

It is safe to identify a generic type in a generic variadic constructor or method declaration.

@SafeVarargs
public final <T> void doSomething(T... args) {
    
    
    // ...
}

In the above example, @SafeVarargsthe annotation identifies that the generic variable parameter type is safe, and the compiler will no longer issue a warning message.

5. @FunctionalInterface

Used to identify an interface as a functional interface, that is, an interface that contains only one abstract method.

@FunctionalInterface
public interface GreetingService {
    
    
    void greet(String name);
}

In the above example, @FunctionalInterfacethe annotation identifies that the interface GreetingServiceis a functional interface that contains only one abstract method greet.

Two, MyBatis annotations

1. @Select

Used to map a query SQL.

@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(Long id);

In the above example, @Selectthe annotation indicates that the SQL string is mapped to getUserByIdthe method, where #{id}the placeholder is replaced by the method parameter.

2. @Update

Used to map an update SQL.

@Update("UPDATE user SET name = #{name} WHERE id = #{id}")
int updateUserById(Long id, String name);

In the above example, @Updatethe annotation means to map the SQL string to updateUserByIdthe method, where #{id}and #{name}represent placeholders, which are replaced by method parameters.

3. @Insert

Used to map an insert SQL.

@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
int addUser(User user);

In the above example, @Insertthe annotation means to map the SQL string to addUserthe method, where #{name}and #{age}represent placeholders, which are Userreplaced by the properties of the object.

4. @Delete

Used to map a delete SQL.

@Delete("DELETE FROM user WHERE id = #{id}")
int deleteUserById(Long id);

In the above example, @Deletethe annotation indicates that the SQL string is mapped to deleteUserByIdthe method, where #{id}the placeholder is replaced by the method parameter.

3. Spring annotations

1. @Autowired

Autowiring for automatic dependency injection.

@Service
public class UserServiceImpl继续:

```java
@Autowired
private UserDao userDao;

public User getUserById(Long id) {
    
    
    return userDao.getUserById(id);
}

In the above example, the annotation indicates that the component @Autowiredis automatically injected , and there is no need to manually create the object through the keyword.UserDaonew

2. @Service

Used to identify service layer components.

@Service
public class UserServiceImpl implements UserService {
    
    
    // ...
}

In the above example, @Servicethe annotation indicates that the class is a service layer component that can be automatically injected by other components.

3. @Controller

Used to identify Controller layer components.

@Controller
@RequestMapping("/user")
public class UserController {
    
    
    // ...
}

In the above example, @Controllerthe annotation indicates that the class is a Controller layer component that can process /userthe requested data.

4. @RequestMapping

Used to map request paths and request methods.

@Controller
@RequestMapping("/user")
public class UserController {
    
    
    
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String userList() {
    
    
        // ...
    }
}

In the above example, @RequestMappingthe annotation indicates that the request path is /user/list, the request method is GET, and the corresponding processing method is userList.

Four, Spring Boot annotations

1. @SpringBootApplication

It is used to identify the startup class, including three annotations of @Configuration, @EnableAutoConfiguration, and .@ComponentScan

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

In the above example, @SpringBootApplicationthe annotation indicates that the class is the startup class of the Spring Boot application.

2. @EnableAutoConfiguration

Automatic configuration, automatically configures automatically according to the dependencies under the class path and the beans defined by the project.

3. @RestController

Similar @Controller, but all methods in it return JSONformatted data.

@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
    
    
        return userService.getUserById(id);
    }
}

In the above example, @RestControllerthe annotation indicates that the class can handle /userthe request and return JSONformatted data.

4. @RequestMapping

Used to map request paths and request methods.

@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
    
    
        // ...
    }
}

In the above example, @RequestMappingthe annotation indicates that the request path is /user/{id}, the request method is GET, and the corresponding processing method is getUserById.

5. MySQL Notes

1. @Table

Used to identify table names.

@Table(name = "user")
public class User {
    
    
    // ...
}

In the above example, @Tablethe annotation indicates the table name corresponding to the entity class user.

2. @Column

Used to identify field names.

@Table(name = "user")
public class User {
    
    

    @Column(name = "user_id")
    private Long id;
    // ...
}

In the above example, @Columnthe annotation indicates the column name corresponding to the field user_id.

3. @GeneratedValue

Used to identify that this field is automatically generated.

@Table(name = "user")
public class User {
    
    

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // ...
}

In the example above, @GeneratedValuethe annotation indicates that the field is auto-generated and auto-incremented by the database.

4. @Id

Used to identify the primary key field.

@Table(name = "user")
public class User {
    
    

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // ...
}

In the above example, @Idthe annotation indicates that the field is the primary key.

Guess you like

Origin blog.csdn.net/weixin_52357829/article/details/131351984