Integrating Mybatis with Spring

Spring framework does not support Mybatis internally. Instead, mybatis-spring project supports integration of Mybatis with Spring. This article is based on Spring 4.3.16 and Mybatis 3.4.6. (As of writing this article, Spring 5.1.x still don’t support Mybatis internally)

You can download the sample project here.

Test table for the sample

CREATE TABLE MYBATIS_TEST(
	COL1 VARCHAR2(10) PRIMARY KEY,
	COL2 VARCHAR2(10)
);

Test Mybatis mapper xml

mapper.xml

Required libraries

Required libraries are mybatis-spring, mybatis, spring-context and spring-jdbc. The following is the sample pom.xml.

pom.xml

To integrate Mybatis with Spring, SqlSessionFactoryBean must be declared. After that, there are 2 ways to access Mybatis.

  1. To use mapper interface
  2. To access SqlSession directly

1. Using mapper interface (spring-mapper-context.xml)

To use mapper interface, SqlSessionFactoryBean and MapperFactoryBean must be instantiated. (There is another way to declare mapper bean – scanning mappers.)

spring-mapper-context.xml

Notice that SqlSessionFactoryBean has a mandatory dataSource attribute and optional mapperLocation attribute

Test java app (mapper interface sample)

    private ApplicationContext getSpringContext(){
        String contextFileName = "spring/spring-mapper-context.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {contextFileName});
        return context;
    }

    private void testMapper() throws Exception{
    	ApplicationContext context = getSpringContext();
    	TestMapper testMapper = context.getBean(TestMapper.class);
    	Map paramMap = new HashMap();
    	paramMap.put("COL1", "VALUE1");
    	paramMap.put("COL2", "VALUE2");
    	testMapper.insertData(paramMap);
    	Map resultMap = testMapper.selectData("VALUE1");
    	System.out.println("Query result : " + resultMap.get("COL2"));
    }

2. Accessing SqlSession directly (spring-dao-context.xml)

To access SqlSession directly, SqlSessionFactoryBean and SqlSessionTemplate must be instantiated. (And, also SqlSession must be injected into DAO bean)

spring-dao-context.xml

Above sample shows that testDao is injected sqlSession, which is created with SqlSessionTemplate, which is created with SqlSessionFactoryBean

Sample java app

    private ApplicationContext getSpringContext(){
        String contextFileName = "spring/spring-dao-context.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {contextFileName});
        return context;
    }

    private void testDao() throws Exception{
    	ApplicationContext context = getSpringContext();
    	TestDao testDao = context.getBean(TestDao.class);
    	Map paramMap = new HashMap();
    	paramMap.put("COL1", "VALUE1");
    	paramMap.put("COL2", "VALUE2");
    	testDao.insertData(paramMap);
    	Map resultMap = testDao.selectData("VALUE1");
    	System.out.println("Query result : " + resultMap.get("COL2"));
    }

These 2 ways are not exhaustive ways to integrate Mybatis. But more advanced techniques are mostly from these 2 ways

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.