How to Build a SpringBoot + MyBatis Multi‑Module Project in IntelliJ IDEA
This guide walks through creating a SpringBoot and MyBatis multi‑module project in IntelliJ IDEA, covering environment setup, parent and child module creation, dependency configuration, bean scanning, MyBatis integration, and troubleshooting common startup errors, culminating in a runnable demo endpoint.
Introduction
Our company is refactoring a project and has chosen SpringBoot + MyBatis. This article records the process of building a SpringBoot multi‑module project in IntelliJ IDEA.
Development tools and system environment
IDE: IntelliJ IDEA 2018.2
Operating system: macOS
Project directory structure
biz layer: business logic layer
dao layer: data persistence layer
web layer: request handling layer
Setup steps
1. Create parent project
In IDEA toolbar select File → New → Project…
Select Spring Initializr, keep the default, click Next.
Fill the input fields and click Next.
Skip the next step and click Next.
Click Finish to create the project.
The final project directory structure looks like this:
Delete unnecessary .mvn, src, mvnw and mvnw.cmd files, leaving only .gitignore and pom.xml.
2. Create sub‑modules
Right‑click the project root (beta) and choose New → Module.
Select Maven and click Next.
Enter the ArtifactId and click Next.
Modify the module name to include a hyphen for readability and click Finish.
Similarly add beta‑dao and beta‑web sub‑modules. The resulting directory structure is shown below:
3. Run the project
Create package com.yibao.beta.web and entry class BetaWebApplication.java:
@SpringBootApplication
public class BetaWebApplication {
public static void main(String[] args) {
SpringApplication.run(BetaWebApplication.class, args);
}
}Add a controller with a test endpoint:
@RestController
@RequestMapping("demo")
public class DemoController {
@GetMapping("test")
public String test() {
return "Hello World!";
}
}Run the BetaWebApplication main method. The default port is 8080; accessing http://localhost:8080/demo/test returns the expected result.
4. Configure inter‑module dependencies
Dependency relationships: biz depends on dao, web depends on biz.
Declare all sub‑module dependencies in the parent pom.xml using dependencyManagement and dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-biz</artifactId>
<version>${beta.version}</version>
</dependency>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-dao</artifactId>
<version>${beta.version}</version>
</dependency>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-web</artifactId>
<version>${beta.version}</version>
</dependency>
</dependencies>
</dependencyManagement>Add beta-biz as a dependency in beta-web and beta-dao as a dependency in beta-biz:
<dependencies>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-biz</artifactId>
</dependency>
</dependencies> <dependencies>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-dao</artifactId>
</dependency>
</dependencies>When the DemoController autowires DemoService, a startup error occurs because the bean cannot be found. Fix it by adding package scanning to the entry class:
@SpringBootApplication(scanBasePackages = "com.yibao.beta")
@MapperScan("com.yibao.beta.dao.mapper")
public class BetaWebApplication {
public static void main(String[] args) {
SpringApplication.run(BetaWebApplication.class, args);
}
}After this change the application starts correctly and the endpoint returns the expected response.
5. Integrate MyBatis
Add mybatis-spring-boot-starter and lombok dependencies to the parent pom.xml (managed under dependencyManagement).
<dependencyManagement>
<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>
</dependencyManagement>Add the same dependencies to the beta-dao module.
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>Generate DAO files (DO, Mapper, XML) with MyBatis Generator and place them under com.yibao.beta.dao. Add the following properties to 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.entityUpdate DemoService to autowire UserMapper and return data from the database:
package com.yibao.beta.biz.service.impl;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private UserMapper userMapper;
@Override
public String test() {
UserDO user = userMapper.selectByPrimaryKey(1);
return user.toString();
}
}Running the application now fails because UserMapper bean is missing. Fix it by adding @MapperScan("com.yibao.beta.dao.mapper") to the entry class (already shown earlier). After the fix, the application starts and the endpoint returns the database record.
Conclusion
A well‑structured multi‑module SpringBoot + MyBatis project is easier to maintain and serves as a solid foundation for future micro‑service migration. Additional layers such as a common component layer or a server layer (e.g., Dubbo) can be added later.
Future work will integrate logback, disconf, Redis, Dubbo, and other components.
Unmentioned pitfalls
During setup we encountered an issue with the internal Maven repository, which mirrors Alibaba's remote repository and lacks some artifact versions, causing occasional build failures.
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
