How to Set Up MyBatis-Flex with Spring Boot 4: A Step-by-Step Guide
I needed to set up MyBatis-Flex with Spring Boot 4 for a new project and ran into some confusion about the correct dependencies and configuration. Here’s what I learned.
Adding Dependencies
First, I added the MyBatis-Flex Spring Boot 4 starter to my pom.xml:
<dependencies> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-spring-boot4-starter</artifactId> <version>1.11.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>4.0.3</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.4.240</version> <scope>runtime</scope> </dependency></dependencies>The mybatis-flex-spring-boot4-starter provides auto-configuration for MyBatis-Flex in Spring Boot 4 applications. I also included the Spring Boot JDBC starter and an H2 database for this example.
Configuring the Datasource
Next, I configured the datasource in my application.yml:
spring: datasource: driver-class-name: org.h2.Driver url: jdbc:h2:mem:mybatisflex username: sa password: sql: init: mode: alwaysThis sets up an in-memory H2 database. The sql.init.mode: always ensures any schema scripts run on startup.
Enabling Mapper Scanning
Finally, I enabled mapper scanning in my main application class:
package com.example;
import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication@MapperScan("com.example.mapper")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}The @MapperScan annotation tells MyBatis-Flex where to find your mapper interfaces.
In this post, I showed you how to set up MyBatis-Flex with Spring Boot 4 by adding the starter dependency, configuring the datasource, and enabling mapper scanning. MyBatis-Flex supports over 40 databases including MySQL, PostgreSQL, Oracle, SQL Server, and H2.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments