O'Reilly logo

Java Persistence with MyBatis 3 by K. Siva Prasad Reddy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with SqlSession

Once the SqlSessionFactory bean is configured, we need to configure the SqlSessionTemplate bean, which is a thread-safe Spring bean from which we can obtain the thread-safe SqlSession objects. Because SqlSessionTemplate provides the thread-safe SqlSession objects, you can share the same SqlSessionTemplate instance with multiple Spring beans. Conceptually SqlSessionTemplate is similar to JdbcTemplate of the Spring DAO module.

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

Now we can inject the SqlSession bean into any Spring bean and use the SqlSession object to invoke the mapped SQL statements.

public class StudentDaoImpl implements StudentDao ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required