Boost Your Spring Boot Apps with a Reusable MyBatisPlus Controller Layer

This article walks through creating a reusable MyBatisPlus‑based controller framework for Spring Boot, covering dependency setup, utility methods for query building and reflection, a generic BaseController with full CRUD endpoints, pagination configuration, and concrete controller extensions, enabling rapid development of RESTful APIs.

Architecture Digest
Architecture Digest
Architecture Digest
Boost Your Spring Boot Apps with a Reusable MyBatisPlus Controller Layer

Introduce MyBatisPlusPro, a wrapper that provides CRUD operations at the controller layer by extending a generic BaseController.

Step 1: Add MyBatis‑Plus starter dependency to pom.xml.

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

Step 2: Create a utility class ApprenticeUtil that contains methods for converting between camelCase and snake_case, building a QueryWrapper from an entity, and retrieving field values via reflection.

public class ApprenticeUtil {
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    public static String humpToLine(String str) { /* ... */ }
    public static String lineToHump(String str) { /* ... */ }
    public static <E> QueryWrapper<E> getQueryWrapper(E entity) { /* ... */ }
    public static <E> Object getValueForClass(E entity, String value) { /* ... */ }
}

Step 3: Define a generic BaseController that implements common CRUD endpoints (insert, delete, update, getById, save, list, page, count) using the service layer and the utility methods.

public class BaseController<S extends IService<E>, E> {
    @Autowired protected S baseService;

    @PostMapping("/insert")
    public ResponseUtils insert(@RequestBody E entity) {
        baseService.save(entity);
        return ResponseUtils.success("添加成功");
    }
    // other CRUD methods ...
}

Step 4: Configure MyBatis‑Plus pagination by creating a MybatisPlusConfig class that registers a PaginationInnerInterceptor for MySQL.

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

Step 5: Extend BaseController in concrete controllers, for example DynamicController and BlogController, supplying the specific service and entity types.

@RestController
@RequestMapping("/apprentice/dynamic")
public class DynamicController extends BaseController<IDynamicService, Dynamic> { }

After these steps the application gains ready‑made RESTful endpoints for any entity without writing repetitive controller code.

Demo UI
Demo UI
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaSpring BootPaginationREST APIControllerCRUDMybatisPlus
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.