Integrating Spring Boot with MyBatis-Plus and MySQL Database

Build Configuration and Dependencies

Initialize the project by declaring the required libraries in the Maven descriptor. This setup establishes the core web framwork, incorporates the MyBatis-Plus persistence extension, adds the official MySQL driver, and integrates the Druid connection pool for efficient resource handling.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

    <groupId>com.example.dataops</groupId>
    <artifactId>mp-integration-demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Environment and DataSource Properties

Configure the runtime server settings and database connectivity parameters. The following YAML structure defines the embedded server port, JDBC connection string, and connection pool thresholds to ensure stable database interactions under load.

server:
  port: 9090
  servlet:
    context-path: /api-core

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/operations_db?useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: admin
    password: SecurePass2024!
    type: com.alibaba.druid.pool.DruidDataSource
    initial-size: 5
    max-active: 15
    min-idle: 3
    max-wait: 5000
    test-while-idle: true
    validation-query: SELECT 1
    pool-prepared-statements: true
    max-open-prepared-statements: 10

mybatis-plus:
  mapper-locations: classpath*:/mapping/**/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Application Bootstrap

Create the primary execution class to launch the Spring IoC container. The annotations trigger auto-configuration and instruct MyBatis to scan the designated package for repository interfaces.

package com.example.dataops;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.example.dataops.repository")
public class DataOpsApplication {

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

Entity Mapping

Define a plain Java object that represents the database table structure. Implementing Serializable ensures safe data transfer across distributed layers, while standard accessors facilitate object-relational mapping.

package com.example.dataops.entity;

import java.io.Serializable;

public class TechProfile implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long profileId;
    private String techLeadName;
    private Integer experienceLevel;
    private Boolean isActive;

    public Long getProfileId() { return profileId; }
    public void setProfileId(Long profileId) { this.profileId = profileId; }

    public String getTechLeadName() { return techLeadName; }
    public void setTechLeadName(String techLeadName) { this.techLeadName = techLeadName; }

    public Integer getExperienceLevel() { return experienceLevel; }
    public void setExperienceLevel(Integer experienceLevel) { this.experienceLevel = experienceLevel; }

    public Boolean getIsActive() { return isActive; }
    public void setIsActive(Boolean isActive) { this.isActive = isActive; }

    @Override
    public String toString() {
        return "TechProfile{" +
                "profileId=" + profileId +
                ", name='" + techLeadName + '\'' +
                ", level=" + experienceLevel +
                ", active=" + isActive +
                '}';
    }
}

Persistence Layer Interface

Extend the framework's base mapper interface. This inheritance grants immediate access to pre-built CRUD methods without requiring manual XML SQL statements.

package com.example.dataops.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dataops.entity.TechProfile;
import org.springframework.stereotype.Repository;

@Repository
public interface ProfileRepository extends BaseMapper<TechProfile> {
}

Business Service Implementation

Encapsulate trensactional logic and query execution. Using constructor injection promotes immutability and testability, while the query wrapper dynamically builds SQL conditions based on application requirements.

package com.example.dataops.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dataops.entity.TechProfile;
import com.example.dataops.repository.ProfileRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class ProfileBusinessService {

    private final ProfileRepository profileRepository;

    public ProfileBusinessService(ProfileRepository profileRepository) {
        this.profileRepository = profileRepository;
    }

    public List<TechProfile> retrieveActiveProfiles() {
        QueryWrapper<TechProfile> filter = new QueryWrapper<>();
        filter.eq("is_active", Boolean.TRUE)
              .orderByDesc("experience_level");
        return profileRepository.selectList(filter);
    }
}

REST API Endpoint

Expose the service functionality over HTTP. The controller maps incoming GET requests to the business layer and returns structured JSON responses using standard HTTP status codes.

package com.example.dataops.controller;

import com.example.dataops.entity.TechProfile;
import com.example.dataops.service.ProfileBusinessService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/staff")
public class ProfileApiController {

    private final ProfileBusinessService profileService;

    public ProfileApiController(ProfileBusinessService profileService) {
        this.profileService = profileService;
    }

    @GetMapping("/active")
    public ResponseEntity<List<TechProfile>> fetchActiveRecords() {
        List<TechProfile> results = profileService.retrieveActiveProfiles();
        return ResponseEntity.ok(results);
    }
}

Tags: spring-boot mybatis-plus MySQL druid Maven

Posted on Fri, 28 Aug 2026 16:07:12 +0000 by cli_man