Hibernate / MyBatis Interview questions
How do you integrate MyBatis with Spring Boot?
Integrating MyBatis with Spring Boot typically means adding the mybatis-spring-boot-starter dependency, which brings in MyBatis, MyBatis-Spring, and Spring Boot auto-configuration that wires up a SqlSessionFactory and mapper scanning automatically based on sensible defaults and application properties.
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>4.0.1</version> </dependency>
@Mapper public interface UserMapper { @Select("SELECT * FROM users WHERE id = #{id}") User selectUserById(int id); }
With the starter on the classpath and a standard Spring Boot DataSource configured (via application.properties), MyBatis-Spring-Boot-Starter automatically builds a SqlSessionFactory from that data source, and interfaces annotated with @Mapper are automatically discovered and registered as Spring beans without needing manual mapper registration in XML.
Mapper XML files, if used instead of or alongside annotations, are typically placed under src/main/resources in a location configurable via the mybatis.mapper-locations property, and Spring Boot's auto-configuration picks them up automatically as long as their namespace correctly matches the corresponding @Mapper-annotated interface.
More Related questions...