Notes on common annotations in Java (continuously updated):
Table of contents
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, @Override
the annotation identifies that the method Dog
of the class move
overrides the method Animal
in 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, @Deprecated
the annotation identifies that the class OldClass
is obsolete and the method oldMethod
is also obsolete and not recommended.
3. @SuppressWarnings
Used to suppress compiler warnings.
@SuppressWarnings("unchecked")
List<String> list = new ArrayList();
In the above example, @SuppressWarnings
the 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, @SafeVarargs
the 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, @FunctionalInterface
the annotation identifies that the interface GreetingService
is 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, @Select
the annotation indicates that the SQL string is mapped to getUserById
the 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, @Update
the annotation means to map the SQL string to updateUserById
the 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, @Insert
the annotation means to map the SQL string to addUser
the method, where #{name}
and #{age}
represent placeholders, which are User
replaced 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, @Delete
the annotation indicates that the SQL string is mapped to deleteUserById
the 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 @Autowired
is automatically injected , and there is no need to manually create the object through the keyword.UserDao
new
2. @Service
Used to identify service layer components.
@Service
public class UserServiceImpl implements UserService {
// ...
}
In the above example, @Service
the 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, @Controller
the annotation indicates that the class is a Controller layer component that can process /user
the 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, @RequestMapping
the 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, @SpringBootApplication
the 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 JSON
formatted 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, @RestController
the annotation indicates that the class can handle /user
the request and return JSON
formatted 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, @RequestMapping
the 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, @Table
the 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, @Column
the 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, @GeneratedValue
the 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, @Id
the annotation indicates that the field is the primary key.