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

Playing with Spring

Archive for July, 2011

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.


Posted in MyBatis, Spring | 4 Comments »