Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
1. Introduction .................................................................................................................................. 1 1.1. Why mybatis-spring - Motivation ........................................................................................ 1 1.2. Requirements ..................................................................................................................... 1 1.3. Acknowledgements ............................................................................................................ 1 2. Bootstrap ...................................................................................................................................... 2 2.1. Introduction ....................................................................................................................... 2 2.2. Setting up a SqlSessionFactory ........................................................................................... 2 3. Using SqlSessionDaoTemplate and SqlSessionDaoSupport ............................................................. 3 3.1. SqlSessionDaoSupport ....................................................................................................... 3 3.2. .......................................................................................................................................... 3 4. Injecting Mappers ......................................................................................................................... 4 4.1. Injecting Mappers .............................................................................................................. 4 5. Using MyBatis API ....................................................................................................................... 5 5.1. Using MyBatis API ............................................................................................................ 5
ii
Chapter 1. Introduction 1.1. Why mybatis-spring - Motivation Every Spring user was impatiently waiting for the 3.X release, but unfortunately once released, MyBatis users where terribly disappointed: their preferred SQL Mapping Framework was no more supported. After many request of including the integration and a good number of patches submitted on the Spring Jira issue, once the issue was accepted but announced to be delaiyed, the MyBatis community thought it was time to reunite the interested people and contributors and start the Spring integration made by the community itself. So, this small library intends to create the missing perfect glue between the two popular frameworks, reducing the boilerplate and redundant code that users have to write to configure and use MyBatis into a Spring 3.X context.
1.2. Requirements Before starting reading the manual, it is very important you're familiar with both MyBatis and Spring framework and therminology, otherwise it would be very difficult to understand the described context. Like MyBatis, mybatis-spring requires Java 5 or higher.
1.3. Acknowledgements A special thanks goes to all the special people who made this project a reality, above all (in alphabetical order) Andrius Juozapaitis, Eduardo Macarron, Giovanni Cuccu, Hunter Presnall, Putthibong Boonbong and Raj Nagappan. Without them, that project wouldn't exist.
1
Chapter 2. Bootstrap 2.1. Introduction MyBatis-Spring integration helps you to integrate your code seamlessly with Spring. Spring will load and create necessary MyBatis classes for you. It will also inject working Mappers/Daos directly on your service beans.
2.2. Setting up a SqlSessionFactory As you already know, to use MyBatis you need to build a SqlSessionFactory from XML files. MyBatis-Spring will build a SqlSessionFactory for you during Spring startup. The XML snippet below shows the configuration needed to build a SqlSessionFactoryBean:
Where mybatis-config.xml is the main configuration file for MyBatis. Follows below a mybatis-config.xml sample, but please refeer to the MyBatis reference manual to know more details about it:
Usually the main config file holds general configuration options and the mappers list. The mappers list is optional if you are using injected mappers.
2
Chapter 3. Using SqlSessionDaoTemplate and SqlSessionDaoSupport 3.1. SqlSessionDaoSupport SqlSessionDaoSupport is a support class that helps building DAOs with a very simple API, like in the sample below: public class UserMapperTemplateImpl extends SqlSessionDaoSupport implements UserMapper { public User getUser(String userId) { return (User) getSqlSessionTemplate().selectOne("sample.UserMapper.getUser", userId); } }
As the example shows instead of using a SqlSession you just use SqlSessionDaoTemplate to execute MyBatis methods (selectOne, selectList...)
3.2. is able to create a new SqlSession or get the active SqlSession from current transaction. It also translates exceptions to Spring's genericDataAccessException hierarchy. SqlSessionDaoTemplate
The SqlSessionDaoTemplate offers a generic method, taking a custom SqlSessionCallback as argument so that you can execute more than one method over a SqlSession: public void inserUser(final User user) { getSqlSessionTemplate().execute(new SqlSessionCallback
MyBatis-Spring 1.0.0-SNAPSHOT - Reference ... -
Every Spring user was impatiently waiting for the 3.X release, but unfortunately once released, MyBatis users where terribly disappointed: their preferred SQL Mapping Framework was no more supported. After many request of including the integration and a good number of patches submitted on the Spring Jira issue, once ...