Hibernate / MyBatis Interview questions
What is the mybatis-config.xml file used for?
The mybatis-config.xml file is MyBatis's top-level, global configuration file, holding settings that apply across the entire application — environment/data source configuration, type aliases, plugin registrations, and the list of mapper files or interfaces to load — distinct from the individual mapper XML files that define specific SQL statements.
<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
This file is what a SqlSessionFactoryBuilder reads when constructing a SqlSessionFactory in a plain, non-Spring MyBatis application; in a Spring Boot application using MyBatis-Spring-Boot-Starter, this XML configuration is often replaced or supplemented by Spring Boot's own application.properties/application.yml settings and Java-based configuration, since auto-configuration handles much of what mybatis-config.xml would otherwise define manually.
Common elements configured here beyond the data source include typeAliases (shorthand names for fully-qualified Java class names used in mapper XML), plugins (registering custom interceptors), and global settings like whether lazy loading or the second-level cache is enabled by default.
More Related questions...