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.
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: xxx3. 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: autoAdd @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.
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.
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.
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.
