Coverage Report - org.mybatis.spring.mapper.MapperFactoryBean
 
Classes in this File Line Coverage Branch Coverage Complexity
MapperFactoryBean
76%
20/26
100%
4/4
1.4
 
 1  
 /**
 2  
  *    Copyright 2010-2015 the original author or authors.
 3  
  *
 4  
  *    Licensed under the Apache License, Version 2.0 (the "License");
 5  
  *    you may not use this file except in compliance with the License.
 6  
  *    You may obtain a copy of the License at
 7  
  *
 8  
  *       http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  *    Unless required by applicable law or agreed to in writing, software
 11  
  *    distributed under the License is distributed on an "AS IS" BASIS,
 12  
  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  *    See the License for the specific language governing permissions and
 14  
  *    limitations under the License.
 15  
  */
 16  
 package org.mybatis.spring.mapper;
 17  
 
 18  
 import static org.springframework.util.Assert.notNull;
 19  
 
 20  
 import org.apache.ibatis.executor.ErrorContext;
 21  
 import org.apache.ibatis.session.Configuration;
 22  
 import org.mybatis.spring.SqlSessionTemplate;
 23  
 import org.mybatis.spring.support.SqlSessionDaoSupport;
 24  
 import org.springframework.beans.factory.FactoryBean;
 25  
 
 26  
 /**
 27  
  * BeanFactory that enables injection of MyBatis mapper interfaces. It can be set up with a
 28  
  * SqlSessionFactory or a pre-configured SqlSessionTemplate.
 29  
  * <p>
 30  
  * Sample configuration:
 31  
  *
 32  
  * <pre class="code">
 33  
  * {@code
 34  
  *   <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
 35  
  *     <property name="sqlSessionFactory" ref="sqlSessionFactory" />
 36  
  *   </bean>
 37  
  *
 38  
  *   <bean id="oneMapper" parent="baseMapper">
 39  
  *     <property name="mapperInterface" value="my.package.MyMapperInterface" />
 40  
  *   </bean>
 41  
  *
 42  
  *   <bean id="anotherMapper" parent="baseMapper">
 43  
  *     <property name="mapperInterface" value="my.package.MyAnotherMapperInterface" />
 44  
  *   </bean>
 45  
  * }
 46  
  * </pre>
 47  
  * <p>
 48  
  * Note that this factory can only inject <em>interfaces</em>, not concrete classes.
 49  
  *
 50  
  * @author Eduardo Macarron
 51  
  *
 52  
  * @see SqlSessionTemplate
 53  
  * @version $Id$
 54  
  */
 55  
 public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
 56  
 
 57  
   private Class<T> mapperInterface;
 58  
 
 59  205
   private boolean addToConfig = true;
 60  
 
 61  
 
 62  0
   public MapperFactoryBean(Class<T> mapperInterface) {
 63  0
     this.mapperInterface = mapperInterface;
 64  0
   }
 65  
 
 66  205
   public MapperFactoryBean() {
 67  205
   }
 68  
 
 69  
   /**
 70  
    * {@inheritDoc}
 71  
    */
 72  
   @Override
 73  
   protected void checkDaoConfig() {
 74  161
     super.checkDaoConfig();
 75  
 
 76  161
     notNull(this.mapperInterface, "Property 'mapperInterface' is required");
 77  
 
 78  161
     Configuration configuration = getSqlSession().getConfiguration();
 79  161
     if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
 80  
       try {
 81  120
         configuration.addMapper(this.mapperInterface);
 82  0
       } catch (Exception e) {
 83  0
         logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
 84  0
         throw new IllegalArgumentException(e);
 85  
       } finally {
 86  120
         ErrorContext.instance().reset();
 87  120
       }
 88  
     }
 89  161
   }
 90  
 
 91  
   /**
 92  
    * {@inheritDoc}
 93  
    */
 94  
   @Override
 95  
   public T getObject() throws Exception {
 96  102
     return getSqlSession().getMapper(this.mapperInterface);
 97  
   }
 98  
 
 99  
   /**
 100  
    * {@inheritDoc}
 101  
    */
 102  
   @Override
 103  
   public Class<T> getObjectType() {
 104  1009
     return this.mapperInterface;
 105  
   }
 106  
 
 107  
   /**
 108  
    * {@inheritDoc}
 109  
    */
 110  
   @Override
 111  
   public boolean isSingleton() {
 112  484
     return true;
 113  
   }
 114  
 
 115  
   //------------- mutators --------------
 116  
 
 117  
   /**
 118  
    * Sets the mapper interface of the MyBatis mapper
 119  
    *
 120  
    * @param mapperInterface class of the interface
 121  
    */
 122  
   public void setMapperInterface(Class<T> mapperInterface) {
 123  165
     this.mapperInterface = mapperInterface;
 124  165
   }
 125  
 
 126  
   /**
 127  
    * Return the mapper interface of the MyBatis mapper
 128  
    *
 129  
    * @return class of the interface
 130  
    */
 131  
   public Class<T> getMapperInterface() {
 132  9
     return mapperInterface;
 133  
   }
 134  
 
 135  
   /**
 136  
    * If addToConfig is false the mapper will not be added to MyBatis. This means
 137  
    * it must have been included in mybatis-config.xml.
 138  
    * <p/>
 139  
    * If it is true, the mapper will be added to MyBatis in the case it is not already
 140  
    * registered.
 141  
    * <p/>
 142  
    * By default addToCofig is true.
 143  
    *
 144  
    * @param addToConfig
 145  
    */
 146  
   public void setAddToConfig(boolean addToConfig) {
 147  160
     this.addToConfig = addToConfig;
 148  160
   }
 149  
 
 150  
   /**
 151  
    * Return the flag for addition into MyBatis config.
 152  
    *
 153  
    * @return true if the mapper will be added to MyBatis in the case it is not already
 154  
    * registered.
 155  
    */
 156  
   public boolean isAddToConfig() {
 157  9
     return addToConfig;
 158  
   }
 159  
 }