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 IntegrationTests.java

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class IntegrationTests {
	
	@Autowired
	private UserService service;

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

The corresponding configuration file IntegrationTests-context.xml

<beans>
	<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" />
	</jdbc:embedded-database>
</beans>

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

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

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

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

The service interface

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

The UserService implementation with injected DAO-Mapper

@Component
public class UserServiceImpl implements UserService {
	
	@Autowired
	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="de.ice09.blog.dao.mybatis.UserMapper">
	<resultMap id="userResultMap" type="de.ice09.blog.domain.User">
	    <id property="id" column="id" />
	    <result property="lastname" column="name"/>
	</resultMap>
 
	<select id="findUserByName" parameterType="string" resultMap="userResultMap"> 
    	    select id, name from customer where name = #{value}
  	</select>
</mapper>

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.

Advertisements

Posted in MyBatis, Spring | 4 Comments »