How Spring Boot Injects `SqlSessionTemplate` for `@Mapper` Interfaces
In Spring Boot with MyBatis, the @Mapper
annotation is used to mark an interface as a MyBatis Mapper. Spring Boot automatically injects SqlSessionTemplate
behind the scenes to enable SQL execution for these mapper interfaces.
How Spring Boot Injects SqlSessionTemplate
for @Mapper
Interfaces
Spring Boot and MyBatis integrate through MyBatis-Spring-Boot-Starter
, which:
- Scans for
@Mapper
interfaces and registers them as Spring beans. - Automatically configures a
SqlSessionFactory
andSqlSessionTemplate
. - Injects
SqlSessionTemplate
behind the scenes into each@Mapper
interface.
Example Setup
1. Add Dependencies
If using Maven, add:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2. Define the MyBatis @Mapper
Interface
@Mapper
public interface UserMapper {
@Select("SELECT * FROM users WHERE id = #{id}")
User findById(@Param("id") Long id);
}
3. Configure MyBatis in application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mappers/*.xml
4. Enable @MapperScan
in the @SpringBootApplication
@SpringBootApplication
@MapperScan("com.example.mappers") // Automatically scans @Mapper interfaces
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
5. Use the UserMapper
in a Service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Long id) {
return userMapper.findById(id);
}
}
How SqlSessionTemplate
Works Behind the Scenes
- Spring Boot auto-configures
SqlSessionFactory
andSqlSessionTemplate
. - MyBatis-Spring framework generates a dynamic proxy for
UserMapper
and internally usesSqlSessionTemplate
. - No need to manually inject
SqlSessionTemplate
. It is injected by Spring Boot throughSqlSessionFactoryBean
.
Manually Defining SqlSessionTemplate
(If Needed)
If you want explicit control over SqlSessionTemplate
, you can configure it manually:
@Configuration
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
Summary
✅ Spring Boot automatically injects SqlSessionTemplate
into @Mapper
interfaces.
✅ No need to manually define SqlSessionTemplate
unless you have custom configurations.
✅ @MapperScan
allows Spring Boot to detect and register @Mapper
interfaces automatically.
✅ UserMapper
gets proxied using SqlSessionTemplate
, handling database interactions seamlessly.