MyBatis with Spring Boot
Harry
· 11 Sep 2026
· 7 views
The Starter Handles the Wiring
mybatis-spring-boot-starter auto-configures SqlSessionFactory, a transaction manager and mapper scanning. You only provide a DataSource and mappers.
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.username=root
spring.datasource.password=secret
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.example.app.domainScanning Mappers
@SpringBootApplication
@MapperScan("com.example.app.mapper")
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}With @MapperScan, you can inject any mapper interface directly into a service:
@Service
public class CustomerService {
private final CustomerMapper customerMapper;
public CustomerService(CustomerMapper customerMapper) {
this.customerMapper = customerMapper;
}
@Transactional
public void updateCity(Long id, String city) {
customerMapper.updateCity(id, city);
}
}Transactions
Spring's @Transactional wraps mapper calls in a real JDBC transaction. Because the starter registers MyBatis with Spring's transaction manager, commit and rollback behave exactly as you expect.
Key Points
- The starter auto-configures factory, managers and scanning.
- Point mybatis.mapper-locations at your XML files.
- @MapperScan registers all mapper interfaces as beans.
- @Transactional gives you declarative transactions.