<!-- Define the SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage"
value="com.apress.prospring3.ch11.domain" />
</bean>
<!-- Scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.apress.prospring3.ch11.persistence" />
</bean>
</beans>
Most of the beans should be familiar to you; let's focus our attention on the beans
sqlSessionFactory and org.mybatis.spring.mapper.MapperScannerConfigurer.
The sqlSessionFactory is implemented by the mybatis-spring provided class
org.mybatis.spring.SqlSessionFactoryBean. As you might expect, the factory requires the data source
bean. Also, the property typeAliasesPackage defines the packages storing the domain objects that
MyBatis should look for when perform mapping from SQL resultset into POJOs. It's similar to the
mapped entity classes in Hibernate and JPA.
Note that, in the configuration, the same instance of the dataSource bean is being injected into both
Spring's transactionManager bean and MyBatis's sqlSessionFactory bean. This is because the
SqlSessionFactoryBean class is designed specifically to leverage the existing
DataSourceTransactionManager in Spring. Once a Spring transaction manager is configured, you can
configure transactions in Spring as you normally would. Both @Transactional annotations and AOP-style
configurations are supported. The mybatis-spring module will transparently manage transactions once
they are set up. You just need to ensure that the same instance of dataSource was injected into both the
transactionManager and SqlSessionFactory beans.
The class org.mybatis.spring.mapper.MapperScannerConfigurer is also provided by the mybatis-
spring module. The class accepts the package name, from which MyBatis will scan for mapper interfaces.
Also, the MapperScannerConfigurer bean also needs an instance of SqlSessionFactory to be injected. If
there is only one SqlSessionFactory bean in the ApplicationContext, it will be autowired into the
MapperScannerConfigurer bean. However, if your application contains multiple SqlSessionFactory beans
(for example, in a multiple data source application, you have sqlSessionFactoryA bean for data source A
and another sqlSessionFactoryB bean for data source B), then you will need to explicitly pass the
reference to the corresponding SqlSessionFactory bean into each MapperScannerConfigurer bean.
Having the configuration in place, we can proceed to the next step, which is to define the SQL
mapping in MyBatis between queries and the POJO model.
SQL Mapping in MyBatis
When implementing data access logic using MyBatis, the most important and time-consuming step is to
define the mappings between the queries for various database operations and the corresponding
domain objects. The mapping is closely related to what operations are required and their underlying
queries. So, for the contact information service, let's define the ContactService interface that we will
implement first. Listing 11-5 shows the interface.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home