Understanding Spring Boot Starters with MyBatis: mybatis‑spring vs mybatis‑spring‑boot‑starter

This article explains the concept and purpose of Spring Boot starters, compares mybatis‑spring with mybatis‑spring‑boot‑starter through code examples, and details how the starter simplifies MyBatis integration by handling dependencies, configuration files, auto‑configuration, and mapper scanning for Java backend projects.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Spring Boot Starters with MyBatis: mybatis‑spring vs mybatis‑spring‑boot‑starter

The article uses MyBatis as an example to illustrate what a Spring Boot Starter is, why it exists, and how it works. It first defines a Starter as a Maven module that packages a component's dependencies and auto‑configuration, enabling developers to add functionality with a single dependency.

It then compares the traditional mybatis-spring integration with the mybatis-spring-boot-starter approach, showing that the starter reduces boilerplate configuration such as creating mybatis-config.xml, defining DataSource, SqlSessionFactoryBean, and MapperScannerConfigurer beans.

Spring (non‑Boot) integration steps include:

Adding Spring, MyBatis, and JDBC dependencies.

Creating mybatis-config.xml with property placeholders, settings, mapper packages, and bean definitions for data source, session factory, and mapper scanner.

Writing mapper XML files and Java interfaces.

Calling mapper methods in business code.

For Spring Boot, the process is simplified to:

Adding the mybatis-spring-boot-starter dependency.

Configuring the datasource and mapper locations in application.properties.

spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.url=jdbc:mysql://...
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml

Creating mapper interfaces annotated with @Mapper and corresponding XML files.

Injecting the mapper with @Autowired and invoking methods.

The starter automatically handles:

Dependency management, avoiding version conflicts.

Discovery and registration of an existing DataSource.

Creation and registration of SqlSessionFactory and SqlSessionTemplate beans.

Scanning of @Mapper interfaces and registering them as beans.

Internally, the starter’s mybatis-spring-boot-autoconfigure module uses Spring’s SPI mechanism to load MybatisAutoConfiguration, which performs the above tasks, including an AutoConfiguredMapperScannerRegistrar that registers mapper bean definitions.

Overall, using the starter dramatically reduces configuration code and improves developer productivity when integrating MyBatis with Spring Boot.

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.

JavaSpring BootMyBatisauto-configurationStarter
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.