MyBatis Configuration

Example code is available on

Because Grails is based on Spring, we can use mybatis-spring library to integrate MyBatis into Grails.

Step 1

Add mybatis and mybatis-spring dependencies into BuildConfig.groovy.

compile 'org.mybatis:mybatis:3.2.7'
compile 'org.mybatis:mybatis-spring:1.2.2'

Step 2

Define new beans in resources.groovy.

  • sqlSessionFactory configures connection factory using XML files at the specified location
  • mapperScannerConfigurer will scan for mappers based on what is defined by basePackage property
import org.apache.commons.dbcp.BasicDataSource
import org.mybatis.spring.SqlSessionFactoryBean
import org.mybatis.spring.mapper.MapperScannerConfigurer

beans = {

    sqlSessionFactory(SqlSessionFactoryBean) {
        dataSource = ref("dataSource")
        mapperLocations = "classpath:com/app/mappers/**/*.xml"

    mapperScannerConfigurer(MapperScannerConfigurer) {
        basePackage = ""

results matching ""

    No results matching ""