Integrating MyBatis-Plus with Spring Cloud for CRUD Operations

This tutorial demonstrates how to set up a Spring Cloud project, add MyBatis-Plus dependencies, configure the data source and MyBatis-Plus settings, and perform full CRUD operations with Java code examples, providing a practical guide for backend developers.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Integrating MyBatis-Plus with Spring Cloud for CRUD Operations

This tutorial demonstrates how to set up a Spring Cloud practical project (PassJava) and integrate MyBatis-Plus to perform full CRUD operations.

1. Add MyBatis-Plus dependency

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

2. Configure the data source

Import the MySQL driver (choose version 8.0.17 which is compatible with MySQL 5.7).

<!-- Add MySQL driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>

Configure application.yml with the datasource information:

spring:
  datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://129.211.188.xxx:3306/passjava_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: xxx

3. Configure MyBatis-Plus

Add mapper locations and global config in application.yml:

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto

Add @MapperScan("com.jackson0714.passjava.question.dao") to the main application class:

@MapperScan("com.jackson0714.passjava.question.dao")
@SpringBootApplication
public class PassjavaQuestionApplication {
    public static void main(String[] args) {
        SpringApplication.run(PassjavaQuestionApplication.class, args);
    }
}

4. Test MyBatis-Plus CRUD methods

Create a record in the type table:

@Autowired
TypeService typeService;

// Create a new type
@Test
void testCreateType() {
    TypeEntity typeEntity = new TypeEntity();
    typeEntity.setType("javaBasic");
    typeService.save(typeEntity);
    System.out.println("创建成功");
}

Update the record with id=1:

// Update type=jvm
@Test
void testUpdateType() {
    TypeEntity typeEntity = new TypeEntity();
    typeEntity.setId(1L);
    typeEntity.setType("jvm");
    typeService.updateById(typeEntity);
    System.out.println("修改成功");
}

Query the record with id=1:

// Query type
@Test
void testSelectType() {
    List<TypeEntity> typeEntityList = typeService.list(new QueryWrapper<TypeEntity>().eq("id",1L));
    typeEntityList.forEach(item -> System.out.println(item));
    System.out.println("查询成功");
}

Delete the record with id=1:

// Delete type record
@Test
void testRemoveType() {
    typeService.removeById(1L);
    System.out.println("删除成功");
}

The tutorial also provides links to related resources such as environment setup, Docker installation, and automatic code generation for front‑end and back‑end components.

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.

BackendJavadatabasemybatis-plusCRUDSpring Cloud
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

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.