MyBatis-Spring 1.0.0-SNAPSHOT - Reference Documentation

The MyBatis Community (MyBatis.org)

Copyright © 2010

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() { public Object doInSqlSession(SqlSession sqlSession) throws SQLException { sqlSession.insert("sample.UserMapper.insertUser", user); sqlSession.insert("sample.UserMapper.insertAccount", user.getId()); return null; } }); }

A SqlSessionDaoTemplate can also be created using a SqlSessionFactory as a constructor argument. In this case there is no need that you DAOs extends SqlSessionDaoSupport.

3

Chapter 4. Injecting Mappers 4.1. Injecting Mappers The most interesting feature of MyBatis 3 is mappers. MyBatis-Spring lets you inject mappers on your service beans. When using mappers there is no need to use MyBatis API (selectOne, selectList, insert, update...) you simply call your mappers as you have always called your DAOs. This is the way you create a mapper:

When using mappers you don't need to set up mapper list on the mybatis-config.xml file because mappers are able to register themselves to MyBatis during startup. If you don't want you mappers to self-register set the addToConfig property to false. You inject mappers directy on your business/service objets in the same way you usually inject DAOs:

With injected mappers your code will have no MyBatis-Spring dependencies and no MyBatis dependencies either.

4

Chapter 5. Using MyBatis API 5.1. Using MyBatis API You can also use directly MyBatis API. In this case you won't have any MyBatis-Spring Dependency and you simply use a injected SqlSessionFactory on your DAOs: public class UserMapperSqlSessionImpl implements UserMapper { private SqlSessionFactory sqlSessionFactory; public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public User getUser(String userId) { SqlSession session = sqlSessionFactory.openSession(); User user = (User) session.selectOne("sample.UserMapper.getUser", userId); session.close(); return user; } }

This scenario also supports transaction but ther will be no exception translation.

5

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

20KB Sizes 6 Downloads 226 Views

Recommend Documents

No documents