Hibernate / MyBatis Interview questions
How do you configure a data source in MyBatis?
In a plain, non-Spring MyBatis application, a data source is configured inside the <environments> element of mybatis-config.xml, specifying connection details and which pooling strategy MyBatis should use to manage database connections.
<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>
MyBatis ships with three built-in data source types: UNPOOLED (opens a new connection for every request, simplest but least efficient), POOLED (MyBatis's own basic built-in connection pool), and JNDI (looks up a data source already configured in an application server), and a custom data source implementation can also be plugged in for more advanced needs.
In a Spring or Spring Boot application, this configuration typically moves entirely to Spring: a DataSource bean is configured through Spring Boot's standard application.properties settings (often backed by a production-grade pool like HikariCP, Spring Boot's default), and MyBatis-Spring wires that Spring-managed DataSource into the SqlSessionFactory automatically, rather than MyBatis managing its own connection pool independently.
More Related questions...