One lesson and one lesson for spring boot

#Spring boot common annotations introduction#

Spring Boot is a very popular Java development framework that provides many powerful annotations to make it easier for developers to write code and manage applications. This article will introduce some commonly used Spring Boot annotations and their usage based on the spring projects in the course.

Table of Contents

#Spring boot common annotations introduction#

1.@SpringBootApplication

2.Annotations used by entity (entity class)

@Data

@TableName

@TableId

3.Annotations used by the Mapper class

@Mapper

@Results and @Result

@Select

@Insert and @Update

4.Annotations used in the service class

@Service

@Autowired

5.Annotations used by the Controller class

@Controller

@RequestMapping

@PathVariable

@ModelAttribute

@RequestParam

6. Summary

1.@SpringBootApplication
The @SpringBootApplication annotation is one of the most important annotations in Spring Boot. It combines @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan. When you use the @SpringBootApplication annotation on your application's main class, Spring Boot will automatically configure your application and scan the components in your application. Commonly used in the main program are as follows:
@SpringBootApplication
public class DemoCarApplication {

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

}

Annotations used by 2.entity (entity class)
@Data

@Data: This annotation comes from the Lombok library and is used to automatically generate getter, setter, equals, hashCode and toString methods at compile time. By using this annotation, you can reduce writing duplicate code.

@TableName

@TableName: This annotation is usually used in database operations to specify the table name. It helps a database manipulation library or framework understand how to name tables. For example: @TableName(“Parking_Lot”), Parking_Lot is the table name of the entity parking lot in the project.

@TableId

@TableId(type = IdType.AUTO): This annotation is used to identify the mapping relationship between the data entity class and its primary key column in the database table. Here, IdType.AUTO indicates that the primary key generation strategy is automatically determined by the database. The specific generation strategy depends on the database and database connection library you use.

For example, in the project @Data, @TableName, @TableId are all applied to the entity class:

3.Annotations used by the Mapper class

These annotations come from the MyBatis framework and are used to map the relationship between SQL operations and Java objects.

@Mapper

@Mapper: This annotation is used to identify an interface, which defines a series of methods, each method is used to perform a database operation, such as query, insert, update, etc. In MyBatis, all Mapper interfaces can be found by scanning this annotation.

@Results and @Result

@Results and @Result: These two annotations are used to map the relationship between SQL query result sets and Java objects. @Results is used to map a result set to multiple Java objects, while @Result is used to map a result set to a Java object. Through these two annotations, you can customize how to map data in the database to Java objects. For example:

@Mapper
public interface ParkingLotMapper extends BaseMapper<ParkingLot> {
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "pricing_standard_id",property = "pricingStandard",one = @One(select = "com.example.mapper.PricingStandardMapper.selectById")),
    })
@Select

@Select: This annotation is used to mark a method, which is used to perform a SQL query operation. In MyBatis, this annotation can easily map SQL queries and Java objects. You can use the @Param annotation in method parameters to specify the name of the parameter, and then use that name in the SQL query. For example:

@Select("select * from Parking_Lot join pricing_standard " +
        "on pricing_standard.id=Parking_Lot.pricing_standard_id")
List<ParkingLot> getAll();
@Select("select * from Parking_Lot join pricing_standard " +
        "on pricing_standard.id=Parking_Lot.pricing_standard_id " +
        "where Parking_Lot.id=#{id}")
ParkingLot getById(Long id);
@Insert and @Update

@Insert and @Update: These two annotations are used to mark a method that is used to perform insert or update operations. You can use the @Param annotation in method parameters to specify the name of the parameter, and then use that name in the SQL query. With these annotations, you can easily map Java objects to database tables and perform insert or update operations. For example:

@Insert("insert into Parking_Lot set name=#{name} " +
        ",volumetric=#{volumetric} " +
        ",pricing_standard_id=#{pricingStandard.id}")//Remember to synchronize the ID with the name in ParkingLot
int insert(ParkingLot entity);

@Update("update Parking_Lot set name=#{name} " +
        ",volumetric=#{volumetric} " +
        ",pricing_standard_id=#{pricingStandard.id}" +
        " where Parking_Lot.id=#{id}")//Remember to have spaces, otherwise there is a problem with the SQL recognition statement 500, and the updata will not come out.
int update(ParkingLot entity);

These annotations can help you use the MyBatis framework for database operations more easily. By mapping SQL queries to Java objects, you can write code more easily and reduce the complexity of hand-written SQL statements.

4.Annotations used by service class

@Service and @Autowired are two important annotations in the Spring framework.

@Service

The @Service annotation is used to declare an ordinary Java class as a Spring service component and automatically register it with the Spring container. When a class is annotated with @Service, the Spring container will automatically create an instance of this class at startup and save it to the Spring container. In this way, when this class needs to be used elsewhere, an instance of this class can be obtained through the Spring container. For example:

@Autowired

The @Autowired annotation is used for automatic assembly. It can annotate class member variables, methods and constructors to complete the work of automatic assembly. By default, matching beans are searched for in the container by type matching. When there is and is only one matching bean, Spring injects it into the variable marked with @Autowired. Its function is to eliminate getters/setters and properties in bean properties in Java code.

5.Annotations used by the Controller class

These annotations are common annotations in the Spring MVC framework.

@Controller

@Controller: This annotation is used to declare a common Java class as a Spring MVC controller component. After using this annotation, the Spring MVC framework will be able to identify and call the request processing method in this class.

@RequestMapping

@RequestMapping: This annotation is used to map HTTP requests to specific processing methods. By using this annotation on a controller class or method, different HTTP requests can be mapped to different processing methods. You can use the @RequestMapping annotation to specify conditions such as URL path, request method, request header, etc., in order to route the request to the correct processing method.

@RequestMapping("/list")
public String list(Model model){
    List<ParkingLot> entities=service.getAll();
    model.addAttribute("list",entities);//The model is packaged into a list attribute to complete the transfer of the value of the list object
    return "/parking/list";
}
@RequestMapping(value = "/insert")
public String insert(Model model){
    model.addAttribute("parking",new ParkingLot());
    List<PricingStandard> standardList=pricingStandardServer.getAll();
    model.addAttribute("standardList",standardList);
    return "/parking/edit";
}
@PathVariable

@PathVariable: This annotation is used to map parameters in the URL path to parameters of the controller method. By using the @PathVariable annotation on the parameters of the controller method, you can easily obtain the parameter value in the URL path and pass it to the processing method.

@ModelAttribute

The @ModelAttribute annotation is mainly used in Spring MVC to bind request parameters to Java objects, namely Model. Its function is to map parameters in HTTP requests (such as data submitted by forms) to Java objects, so that Java objects can be directly manipulated when processing requests without having to manually splice request parameters into Java objects.

In addition, the @ModelAttribute annotation can also be used to bind custom annotations. For example, you can create a custom annotation and use it with the @ModelAttribute annotation to achieve more complex model data binding and manipulation. For example:

@RequestMapping(value="/save",method = RequestMethod.POST)
public String save(@ModelAttribute("parking") ParkingLot entity, @RequestParam("standardId") Long standardId){
    PricingStandard standard=pricingStandardServer.getById(standardId);
    entity.setPricingStandard(standard);
    boolean result=entity.getId()!=null?service.update(entity):service.insert(entity);
    return "redirect:/parking/list";//The URL corresponding to the front-end UI
}
@RequestParam

@RequestParam: This annotation is used to map the parameters in the HTTP request to the parameters of the controller method. By using the @RequestParam annotation on the parameters of the controller method, you can easily obtain the parameter value in the HTTP request and pass it to the processing method. You can use this annotation to specify the name of the parameter, whether it is required, and its default value.

These annotations can help you write Spring MVC controller code more easily, simplify the process of request mapping and parameter passing, and make the code clearer and easier to maintain.

6. Summary

The function of annotations is very powerful, making it easier for developers to write code and manage applications. However, when you first come into contact with it in the course, you will be a little confused. You will not understand the conflicts between annotations and annotations, and you will forget to write or omit annotations, causing projects to fail. BUG. For example, the error I encountered, page not found: If the @RequestMapping annotation is not used correctly, it may cause a page not found error. For example, if the path of the @RequestMapping annotation is incorrect, or the request method does not match, it may cause a page not found error.

The above spring boot annotations are not complete. They are only introduced based on the annotations encountered in the projects in the course. If there are any deficiencies or errors, please point them out in the comment area.