This chapter will show you in a few steps how to install and setup MyBatis-Spring and how to build a simple transactional application.
To use the MyBatis-Spring module, you just need to include the mybatis-spring-x.x.x.jar file and its dependencies in the classpath.
If you are using Maven just add the following dependency to your pom.xml:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>x.x.x</version> </dependency>
To use MyBatis with Spring you need at least two things defined in the Spring application context: an SqlSessionFactory and at least one mapper interface.
In MyBatis-Spring, an SqlSessionFactoryBean is used to create an SqlSessionFactory . To configure the factory bean, put the following in the Spring XML configuration file:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean>
Notice that the SqlSessionFactory requires a DataSource . This can be any DataSource and should be configured just like any other Spring database connection.
Assume you have a mapper interface defined like the following:
public interface UserMapper { @Select("SELECT * FROM users WHERE id = #{userId}") User getUser(@Param("userId") String userId); }
This interface is added to Spring using a MapperFactoryBean like the following:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
Note that the mapper class specified must be an interface, not an actual implementation class. In this example, annotations are used to specify the SQL, but a MyBatis mapper XML file could also be used.
Once configured, you can inject mappers directly into your business/service objects in the same way you inject any other Spring bean. The MapperFactoryBean handles creating an SqlSession as well as closing it. If there is a Spring transaction in progress, the session will also be committed or rolled back when the transaction completes. Finally, any exceptions will be translated into Spring DataAccessExceptions.
Calling MyBatis data methods is now only one line of code:
public class FooServiceImpl implements FooService { private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public User doSomeBusinessStuff(String userId) { return this.userMapper.getUser(userId); }