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 mappers

We can configure the Mapper interface as a Spring bean using MapperFactoryBean as follows:

public interface StudentMapper
{
  @Select("select stud_id as studId, name, email, phone from students where stud_id=#{id}")
  Student findStudentById(Integer id);
}

<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mybatis3.mappers.StudentMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>  

Now the StudentMapper bean can be injected into any Spring bean and can invoke the mapped statement methods as follows:

public class StudentService { private StudentMapper studentMapper; public void setStudentMapper (StudentMapperstudentMapper) { this. ...

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