Step-by-Step Guide to Building a Multi-Module Spring Boot + MyBatis Project in IntelliJ IDEA
This tutorial walks through setting up a Spring Boot and MyBatis multi‑module project in IntelliJ IDEA, covering environment preparation, directory layout, parent and child module creation, Maven dependency configuration, bean scanning, MyBatis integration, and verification of the running application.
In this article we record the process of constructing a SpringBoot multi‑module project using IntelliJ IDEA, with MyBatis as the persistence layer.
Development Tools and System Environment
IDE: IntelliJ IDEA 2018.2
System: macOS
Project Directory Structure
biz layer: business logic
dao layer: data persistence
web layer: request handling
Setup Steps
1. Create Parent Project
In IDEA select File → New → Project... , choose Spring Initializr , accept defaults, and click Next through the wizard until Finish, which generates the initial project skeleton.
2. Create Sub‑Modules
Right‑click the project root, choose New → Module , select Maven, set the artifactId , and rename the module (e.g., beta-biz , beta-dao , beta-web ) for readability.
3. Run the Project
Create the entry class BetaWebApplication.java in package com.yibao.beta.web :
@SpringBootApplication
public class BetaWebApplication {
public static void main(String[] args) {
SpringApplication.run(BetaWebApplication.class, args);
}
}Add a simple controller:
@RestController
@RequestMapping("demo")
public class DemoController {
@GetMapping("test")
public String test() {
return "Hello World!";
}
}Run the main method; the application starts on port 8080 and http://localhost:8080/demo/test returns Hello World! .
4. Configure Module Dependencies
Declare inter‑module dependencies in the parent pom.xml using dependencyManagement and dependencies . For example, beta-web depends on beta-biz , which depends on beta-dao .
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-biz</artifactId>
<version>${beta.version}</version>
</dependency>
...
</dependencies>
</dependencyManagement>5. Integrate MyBatis
Add mybatis-spring-boot-starter and lombok dependencies to the beta-dao module:
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>Generate DAO classes with MyBatis Generator and configure datasource in application.properties :
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.1.1/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = test
spring.datasource.password = 123456
mybatis.mapper-locations = classpath:mybatis/*.xml
mybatis.type-aliases-package = com.yibao.beta.dao.entity6. Resolve Bean Scanning Issues
When DemoService or UserMapper beans are not found, add package scanning to the entry class:
@SpringBootApplication(scanBasePackages = "com.yibao.beta")
@MapperScan("com.yibao.beta.dao.mapper")
public class BetaWebApplication { ... }After adding the annotations, the application starts successfully and http://localhost:8080/demo/test returns the expected result.
Conclusion
A well‑structured multi‑module SpringBoot + MyBatis project simplifies maintenance and prepares the codebase for future micro‑service migration, with optional extensions such as a common layer, server layer (e.g., Dubbo), logback, Disconf, Redis, etc.
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.
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.