content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.starter.mapper;
  
  import org.apache.ibatis.executor.ErrorContext;
  import org.apache.ibatis.session.Configuration;
  import org.miaixz.bus.core.lang.Assert;
  import org.miaixz.bus.logger.Logger;
  import org.mybatis.spring.support.SqlSessionDaoSupport;
  import org.springframework.beans.factory.FactoryBean;
  
  /**
   * 支持注入MyBatis映射器接口的BeanFactory， 通过sqlessionFactory或者预先配置的sqlessionTemplate来设置
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
  
      private Class<T> mapperInterface;
  
      public MapperFactoryBean() {
  
      }
  
      public MapperFactoryBean(Class<T> mapperInterface) {
          this.mapperInterface = mapperInterface;
      }
  
      @Override
      protected void checkDaoConfig() {
          super.checkDaoConfig();
  
          Assert.notNull(this.mapperInterface, "Property 'mapperInterface' is required");
  
          Configuration configuration = getSqlSession().getConfiguration();
          if (!configuration.hasMapper(this.mapperInterface)) {
              try {
                  configuration.addMapper(this.mapperInterface);
              } catch (Exception e) {
                  Logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
                  throw new IllegalArgumentException(e);
              } finally {
                  ErrorContext.instance().reset();
              }
          }
      }
  
      @Override
      public T getObject() {
          return getSqlSession().getMapper(this.mapperInterface);
      }
  
      @Override
      public Class<T> getObjectType() {
          return this.mapperInterface;
      }
  
      @Override
      public boolean isSingleton() {
          return true;
      }
  
      /**
       * 返回MyBatis mapper的mapper接口
       *
       * @return the object
       */
      public Class<T> getMapperInterface() {
          return mapperInterface;
      }
  
      /**
       * 设置MyBatis mapper的mapper接口
       *
       * @param mapperInterface 接口
       */
      public void setMapperInterface(Class<T> mapperInterface) {
          this.mapperInterface = mapperInterface;
      }
  
  }
  