ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

Integrating Spring 3 and MyBatis 3 using the Spring JDBC namespace

Posted by ice09 on July 25, 2011

Sources to this blog post are available here. These can be used with Maven 3 and are best suitable for the SpringSource Tool Suite 2.7.1 (just import the unzipped project as “Existing Maven project”)

There are several tutorials about the integration of MyBatis 3 in Spring 3 applications, which is kind of special. Even more tutorials are available about the usage of the Spring-JDBC-namespace, mostly together with an embedded database for for unit tests.

This post will show how to combined these mechanisms using the SpringSource Tool Suite 2.7.1.
There is an explanation of the project artifacts first. Afterwards, a step-by-step guide shows the main steps for the creation of a skeleton of this sample project in the SpringSource Tool Suite.

The project structure

The unit test

public class IntegrationTests {
	private UserService service;

	public void testGetFirstUser() throws Exception {
		assertEquals("Customer1", service.getFirstUser());
	public void testFindUserByName() throws Exception {
		assertEquals("0", service.findUserByName("Customer1"));
		assertEquals("1", service.findUserByName("Customer2"));

The corresponding configuration file IntegrationTests-context.xml

	<import resource="classpath:/META-INF/spring/app-context.xml"/> 

	<jdbc:embedded-database id="dataSource">
		<jdbc:script location="classpath:/de/ice09/blog/init-sql/schema.sql" />
		<jdbc:script location="classpath:/de/ice09/blog/init-sql/data.sql" />

The referenced DDL-SQL for the (embedded test) database (hsqldb)

drop table customer if exists;
create table customer (id integer identity primary key, name varchar(50) not null );

The referenced SQL for initialization of the (embedded) database

insert into customer (name) values ('Customer1');
insert into customer (name) values ('Customer2');

Referenced app-context.xml – the main configuration file

	<import resource="classpath:/META-INF/spring/dao-context.xml" />
	<context:component-scan base-package="" />

The included dao-context.xml infrastructure and DAO configuration file

	<!-- infrastructure -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
	<!-- MyBatis Mapper -->	
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />

The service interface

public interface UserService {
	String getFirstUser();
	String findUserByName(String name);

The UserService implementation with injected DAO-Mapper

public class UserServiceImpl implements UserService {
	private UserMapper userMapper;
	public String getFirstUser() {
		List<User> users = userMapper.getUsers();
		return users.get(0).getLastName();	
	public String findUserByName(String name) {
		return userMapper.findUserByName(name).getId();

The mapper with two possible configuration, the getUsers annotated config and the findUsersByName configuration in XML (see below)

public interface UserMapper {
	@Select("SELECT id, name as lastname FROM customer")
	List<User> getUsers();
	User findUserByName(String name);

The sample XML configuration as an alternative for the annotation configuration (for findUserByName)

<mapper namespace="">
	<resultMap id="userResultMap" type="">
	    <id property="id" column="id" />
	    <result property="lastname" column="name"/>
	<select id="findUserByName" parameterType="string" resultMap="userResultMap"> 
    	    select id, name from customer where name = #{value}

Creating the Project – Step by Step

Create a new Spring Template Project in STS

Choose Spring Utility Template

Add namespace to Spring XML configuration

If this blog post was useful to you, you can tip me. Maybe you do not want to tip, but please inform yourself about Bitcoin and applications of Bitcoin like Youttipit.


4 Responses to “Integrating Spring 3 and MyBatis 3 using the Spring JDBC namespace”

  1. Hello, I have been struggling with getting the Spring3/MyBatis3 to work. I get a NullPointerException when I try to access the userMapper in the Service above, Please let me know if I can send you my project…the codebase is small since I just want the basics to work first.

    • ice09 said


      you can download the sources to this blog post, this is a working Eclipse/STS project for this post which you can just import as a maven project. You can then easily compare your project to the project and the screenshots in this post, should be easy to find the error.
      I assume you forgot the registration of the userMapper bean (cmp. dao-context.xml) or the @Autowired annotation.



  2. Nithi said

    Hello,I get no logs when I try to access the userMapper in the DAO, Please let me know if I can send you my project.

    • ice09 said

      Hi Nithi,

      sorry, I cannot examine your project.
      We can try to solve this this way, did you download the sample project? Did it work without modifications?



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: