|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> |
| 3 | +<!-- |
| 4 | + Copyright 2010 The myBatis Team |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +--> |
| 18 | + |
| 19 | +<!-- |
| 20 | + version: $Id$ |
| 21 | +--> |
| 22 | +<chapter id="mappers"> |
| 23 | + <title>Injecting Mappers</title> |
| 24 | + |
| 25 | + <section id="mappers"> |
| 26 | + <title>Injecting Mappers</title> |
| 27 | + <para> |
| 28 | + The most interesting feature of MyBatis 3 is mappers. MyBatis-Spring lets you inject mappers on your service |
| 29 | + beans. When using mappers there is no need to use MyBatis API (<literal>selectOne</literal>, |
| 30 | + <literal>selectList</literal>, <literal>insert</literal>, <literal>update</literal>...) you simply call your |
| 31 | + mappers as you have always called your DAOs. |
| 32 | + </para> |
| 33 | + <para> |
| 34 | + This is the way you create a mapper: |
| 35 | + <programlisting language="xml"><![CDATA[<bean id="userMapper3" class="org.mybatis.spring.MapperFactoryBean"> |
| 36 | + <property name="sqlSessionFactory" ref="sqlSessionFactory" /> |
| 37 | + <property name="mapperInterface" value="sample.UserMapper" /> |
| 38 | +</bean>]]></programlisting> |
| 39 | + When using mappers you don't need to set up mapper list on the <literal>mybatis-config.xml</literal> file |
| 40 | + because mappers are able to register themselves to MyBatis during startup. If you don't want you mappers to |
| 41 | + self-register set the <literal>addToConfig</literal> property to <literal>false</literal>. |
| 42 | + </para> |
| 43 | + |
| 44 | + <para> |
| 45 | + You inject mappers directy on your business/service objets in the same way you usually inject DAOs: |
| 46 | + <programlisting language="xml"><![CDATA[<bean id="fooService3" class="sample.FooServiceImpl"> |
| 47 | + <property name="userMapper" ref="userMapper3" /> |
| 48 | +</bean>]]></programlisting> |
| 49 | + With injected mappers your code will have no MyBatis-Spring dependencies and no MyBatis dependencies either. |
| 50 | + </para> |
| 51 | + </section> |
| 52 | + |
| 53 | +</chapter> |
0 commit comments