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

SqlSessionDaoSupport is a support class that helps building DAOs with a ... You inject mappers directy on your business/service objets in the same way you ...

20KB Sizes 0 Downloads 224 Views

Recommend Documents

Go Quick Reference Go Quick Reference Go Quick Reference - GitHub
Structure - Package package mylib func CallMeFromOutside. Format verbs. Simpler than Cās. MOAR TABLE package anothermain import (. "fmt". ) func main() {.

Reference Policies
instruction/teaching model of the library and the Information Studies program, members ... Locating books and periodicals in the online catalog ... Paging system.

Physics Reference Tables
NCDPI Reference Tables for Physics (October 2006). Page 2. Mechanics. Energy x v t. Δ= Δ. = + f i x x vt. 2. 1 x x. 2 f i i vt at. = +. +. Δ= Δ v a t. 2. 2. 2 x.

TENANCY REFERENCE REQUEST
Rent Amount Paid Per Week. $ per week/calender month. Was the Tenant listed as a lessee? YES. NO. Did you / your Agency terminate the tenancy? YES. NO.

JavaScript Reference
can be composed of letters, numerals, or the underscore character. • cannot contain blank ..... Returns the Unicode or ASCII decimal value of the character at ...

NetBSD reference card - GitHub
To monitor various informations of your NetBSD box you ... ifconfig_if assigns an IP or other on that network in- ... pkg_admin fetch-pkg-vulnerabilities download.

JavaScript Reference
fontsize("size") Changes the size of a string using font sizes 1 (smallest). - 7 (largest). ... Returns the Unicode or ASCII decimal value of the character at position ...

Reference Tables.pdf
Table 14: Organic Functional Groups. Class of Compound General Formula Example Molecule. alcohol R — OH. aldehyde. R — C — H. amide. R — C — NH. amine R — NH. carboxylic acid. R — C — OH. ester. R — C — O — R'. ether R — O —

Reference Manual - GitHub
for the simulation of the electron cloud buildup in particle accelerators. 1 Input files .... points of the longitudinal beam profile of sec- ondary beams.

Oolite Reference Sheet - GitHub
will shut down, requiring a cool-down period before it ... 10 Fuel Scoop ... V2 & Creative Commons License: BY - NC - SA 3.0 Oolite Website: http:/www. ..... A discontinued fighter design finding a new life in the professional racing circuit.

reference glutton.pdf
Page 1 of 1. Page 1 of 1. reference glutton.pdf. reference glutton.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying reference glutton.pdf. Page 1 of 1.

Reference by Description
Jan 21, 2016 - CL] 21 Jan 2016 .... and which nodes have arcs going to this node (shared domain knowledge). ... Sally and Dave's shared view of the domain: ...... order formula with a single free variable (corresponding to the node being ...

LIKWID | quick reference - GitHub
likwid-memsweeper Sweep memory of NUMA domains and evict cache lines from the last level cache likwid-setFrequencies Control the CPU frequency and ...

J1a SwapForth Reference - GitHub
application. After installing the icestorm tools, you can .... The SwapForth shell is a Python program that runs on the host PC. It has a number of advantages over ...

GABotS Reference Manual - GitHub
Apr 9, 2002 - MainWindow (Main widget for the GABots app). 23. Random ..... Main class for simple Genetic Algorithm used in the program. ز ذظ .

Reference Electrodes - Gamry Instruments
This Application Note presumes that you have a basic understanding of potentiostat operation. If you are not that knowledgeable concerning electrochemical ...

GLFW Reference Manual
Sep 3, 2010 - Page 6/64. Notes. This function may take several seconds to complete on some systems, while on other systems it may take only a fraction of a ...Missing:

reference ravo.pdf
Page 1 of 1. Page 1 of 1. reference ravo.pdf. reference ravo.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying reference ravo.pdf. Page 1 of 1.

Reference by Description
Jan 21, 2016 - Lack of shared unique names often complicates entity reference. Shared knowledge can be ... ever, nearly all symbols in use are ambiguous and could refer to multiple entities. The ..... often observed in social network graphs.

Reference-watermark.pdf
3640–3610 (s, sh) O–H stretch, free hydroxyl alcohols, phenols. 3500–3200 (s,b) O–H stretch, H–bonded alcohols, phenols. 3400–3250 (m) N–H stretch 1 ̊, ...