Essential MyBatis, Spring MVC, Spring, SpringBoot & Lombok Annotations Explained
This article provides a concise overview of the most commonly used annotations in MyBatis, Spring MVC, Spring, SpringBoot, and Lombok, explaining their purposes and typical usage scenarios for Java backend development.
Preface
OOP (Object‑Oriented Programming), IoC (Inversion of Control), AOP (Aspect‑Oriented Programming) are programming paradigms, and DI (Dependency Injection) is a concrete implementation of IoC.
1. Common MyBatis Annotations
@Select – query operation.
@Insert – insert operation.
@Update – update operation.
@Delete – delete operation.
@Mapper – generates a proxy object for the Mapper interface and registers it in the IoC container.
@MapperScan – eliminates the need to annotate each Mapper with @Mapper; specify the package to automatically generate proxies for all Mapper interfaces in that package.
2. Common Spring MVC Annotations
@RequestMapping – placed on methods to define endpoint paths (often used for id‑based queries/deletes) or on classes to define a module prefix (e.g., "/users").
@RequestParam – maps a request parameter (key=value) to a method argument when names differ.
@RequestBody – binds a JSON string from a POST request to a method argument.
@ResponseBody – returns a raw string or converts an object to JSON (requires Jackson).
@PathVariable – extracts a path variable (e.g., "/users/1") and binds it to a method parameter.
@GetMapping – GET request for query operations.
@PostMapping – POST request for add/create operations.
@PutMapping – PUT request for update operations.
@DeleteMapping – DELETE request for delete operations. These four follow REST conventions, widely adopted in enterprise development.
3. Common Spring Annotations
1. IoC Annotations
@Controller – creates a controller bean in the IoC container.
@Service – creates a service bean in the IoC container.
@Repository – creates a mapper/repository bean in the IoC container.
@Component – creates a generic bean (neither controller, service, nor mapper) in the IoC container.
@Configuration + @Bean – @Configuration marks a class as a configuration source; @Bean on a method registers the method’s return object as a bean.
@RestController – combines @Controller and @ResponseBody, so individual methods need not be annotated with @ResponseBody.
2. DI Annotations
@Value – injects basic types such as String.
@Autowired – injects custom or third‑party objects.
3. Transaction Annotations
@Transactional – typically placed on service‑layer methods to manage transactions: it starts a transaction before method execution, commits on success, and rolls back on failure.
Note: By default, runtime exceptions trigger a rollback, while checked exceptions do not; you can use the rollbackFor attribute to specify additional exception types. If a try‑catch block handles the exception, the transaction will not roll back.4. Common SpringBoot Annotations
@SpringBootApplication – marks the main class of a SpringBoot application (entry point).
@SpringBootTest – indicates a test class for SpringBoot unit testing.
5. Lombok Annotations
@NoArgsConstructor – generates a no‑argument constructor.
@AllArgsConstructor – generates an all‑argument constructor.
@Getter – generates getter methods.
@Setter – generates setter methods.
@ToString – generates a toString method.
@EqualsAndHashCode – generates equals and hashCode methods.
@Data – combines @Getter, @Setter, @ToString, @EqualsAndHashCode, and also generates required constructors.
Summary: Using @NoArgsConstructor, @AllArgsConstructor, and @Data together usually suffices.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
