Configuring MyBatis SqlSessionFactory and
MapperScannerConfigurer
The core concept of MyBatis surrounds the SqlSession interface (under the package
org.apache.ibatis.session), which was obtained from the SqlSessionFactory interface (under the
package org.apache.ibatis.session). Both the SqlSession and SqlSessionFactory interfaces belong to
the mybatis-3.0.6 library. To configure the factory in Spring, we use the SqlSessionFactoryBean class
(under the package org.mybatis.spring), which belongs to the mybatis-spring module (mybatis-
spring-1.0.2 library).
Another important concept in MyBatis is the mapper interfaces, which are simple Java interface
classes, that will be processed by MyBatis for mapping configuration between SQL queries and domain
object properties. The mapping can be defined in either XML files (having the same name as the
interface class) or annotations within the mapper interface. The mybatis-spring module provides
another class, called MapperScannerConfigurer (under the package org.mybatis.spring.mapper), which
supports a convenient way to instruct MyBatis to scan for mapper interface classes and register them as
MapperFactoryBean<T> (under the package org.mybatis.spring.mapper and belonging to the mybatis-
spring module), which enables injection of MyBatis mapper interfaces into other Spring beans.
The mapper interfaces will be discussed in detail later, and you will see how it can help simplify the
development of database operations without the need to interact with the SqlSession interface directly.
Listing 11-4 shows the Spring application context configuration (app-context.xml).
Listing 11-4. Spring Configuration for MyBatis
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:test-data.sql" />
</jdbc:embedded-database>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven />
<context:component-scan
base-package="com.apress.prospring3.ch11.service.mybatis" />
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home