Prev Next

Hibernate / MyBatis Interview questions

Explain the execution flow of MyBatis-Spring-Boot-Starter's auto-configuration?

When mybatis-spring-boot-starter is on the classpath of a Spring Boot application, its auto-configuration class activates automatically (conditioned on a DataSource bean being present) and wires up the SqlSessionFactory, mapper scanning, and related beans without requiring explicit XML or Java configuration for the common case.

flowchart TD A[Application starts, mybatis-spring-boot-starter on classpath] --> B{DataSource bean present?} B -- No --> C[MyBatis auto-configuration does not activate] B -- Yes --> D[MyBatis auto-configuration activates] D --> E[SqlSessionFactory bean created from DataSource + mybatis.* properties] E --> F[SqlSessionTemplate bean created, wrapping the factory] F --> G[Classpath scanned for @Mapper-annotated interfaces] G --> H[Each found interface registered as a Spring bean via MapperFactoryBean] H --> I[Mapper XML files loaded per mybatis.mapper-locations, if configured] I --> J[Application beans can now @Autowired inject mapper interfaces directly]

The auto-configuration reads mybatis.* properties from application.properties/application.yml (like mybatis.mapper-locations and mybatis.configuration.* settings) to customize the generated SqlSessionFactory, and it scans the classpath for interfaces annotated with @Mapper, registering each one as a Spring bean via MapperFactoryBean so they can be injected anywhere in the application using standard @Autowired.

Because this entire process is conditional (Spring Boot's auto-configuration only activates when its preconditions, like a present DataSource bean, are actually met), a developer can still fall back to fully manual, explicit configuration — defining their own SqlSessionFactory bean, for instance — and Spring Boot's auto-configuration will back off automatically rather than conflicting with an explicitly user-defined bean of the same type.

What must be present for MyBatis's Spring Boot auto-configuration to activate?
What happens if a developer defines their own SqlSessionFactory bean explicitly?

More Related questions...

What is MyBatis? What is the purpose of MyBatis? What are the key features of MyBatis? What is the difference between MyBatis and Hibernate? What is a SqlSessionFactory in MyBatis? What is a SqlSession in MyBatis? What is a Mapper interface in MyBatis? What is the mybatis-config.xml file used for? What is a Mapper XML file? How do you write a basic SELECT statement in MyBatis? What is parameter binding in MyBatis? What is result mapping? What are the supported statement types in MyBatis? What is the difference between #{} and ${} in MyBatis? What is a resultMap? How do you configure a data source in MyBatis? What is MyBatis-Spring? How do you integrate MyBatis with Spring Boot? What is dynamic SQL in MyBatis? List the dynamic SQL elements available in MyBatis? What is the difference between MyBatis and JPA/Hibernate in terms of philosophy? Why is #{} preferred over ${} for parameter binding? How does MyBatis prevent SQL injection? What is the difference between resultType and resultMap? What is the @Param annotation used for in MyBatis? Explain how MyBatis handles one-to-many mappings? Explain how MyBatis handles many-to-one mappings? What is the difference between association and collection in resultMap? What is lazy loading in MyBatis, and how do you configure it? Explain the lifecycle of a SqlSession? What is the difference between SqlSessionFactory and SqlSessionFactoryBuilder? How does MyBatis's first-level cache work? How does MyBatis's second-level cache work? What is the difference between first-level and second-level cache? Explain how to implement a custom TypeHandler in MyBatis? What is the purpose of the if, choose, when, otherwise dynamic SQL elements? Explain the internal working of the foreach element for batch operations? How do you handle batch inserts/updates in MyBatis? What is the Mapper proxy pattern, and how does MyBatis use it internally? Explain the execution flow of a MyBatis query from mapper call to result? What is the difference between MyBatis annotations and XML mapping? How do you use MyBatis with multiple data sources? Explain the role of the Executor in MyBatis's internal architecture, including the SIMPLE, REUSE, and BATCH executor types? How does MyBatis handle transactions, and how does it integrate with Spring's transaction management? What is a plugin/interceptor in MyBatis, and how do you write one? Explain how pagination is typically implemented in MyBatis? What is the N+1 select problem, and how does it relate to MyBatis? How do you troubleshoot a MyBatis mapping exception? Explain the internal working of MyBatis's dynamic SQL parsing? Explain the execution flow of MyBatis-Spring-Boot-Starter's auto-configuration?
Show more question and Answers...


Comments & Discussions