View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2015-2026 the original author or authors.
6    */
7   package org.springframework.orm.ibatis.support;
8   
9   import com.ibatis.sqlmap.client.SqlMapClient;
10  
11  import javax.sql.DataSource;
12  
13  import org.springframework.dao.support.DaoSupport;
14  import org.springframework.orm.ibatis.SqlMapClientTemplate;
15  import org.springframework.util.Assert;
16  
17  /**
18   * Convenient super class for iBATIS SqlMapClient data access objects. Requires a SqlMapClient to be set, providing a
19   * SqlMapClientTemplate based on it to subclasses.
20   * <p>
21   * Instead of a plain SqlMapClient, you can also pass a preconfigured SqlMapClientTemplate instance in. This allows you
22   * to share your SqlMapClientTemplate configuration for all your DAOs, for example a custom SQLExceptionTranslator to
23   * use.
24   *
25   * @author Juergen Hoeller
26   *
27   * @since 24.02.2004
28   *
29   * @see #setSqlMapClient
30   * @see #setSqlMapClientTemplate
31   * @see org.springframework.orm.ibatis.SqlMapClientTemplate
32   * @see org.springframework.orm.ibatis.SqlMapClientTemplate#setExceptionTranslator
33   *
34   * @deprecated as of Spring 3.2, in favor of the native Spring support in the Mybatis follow-up project
35   *             (https://mybatis.org/)
36   */
37  @Deprecated
38  public abstract class SqlMapClientDaoSupport extends DaoSupport {
39  
40    private SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
41  
42    private boolean externalTemplate = false;
43  
44    /**
45     * Set the JDBC DataSource to be used by this DAO. Not required: The SqlMapClient might carry a shared DataSource.
46     *
47     * @param dataSource
48     *          the DataSource to use
49     *
50     * @see #setSqlMapClient
51     */
52    public final void setDataSource(DataSource dataSource) {
53      if (!this.externalTemplate) {
54        this.sqlMapClientTemplate.setDataSource(dataSource);
55      }
56    }
57  
58    /**
59     * Return the JDBC DataSource used by this DAO.
60     *
61     * @return the DataSource
62     */
63    public final DataSource getDataSource() {
64      return this.sqlMapClientTemplate.getDataSource();
65    }
66  
67    /**
68     * Set the iBATIS Database Layer SqlMapClient to work with. Either this or a "sqlMapClientTemplate" is required.
69     *
70     * @param sqlMapClient
71     *          the SqlMapClient to use
72     *
73     * @see #setSqlMapClientTemplate
74     */
75    public final void setSqlMapClient(SqlMapClient sqlMapClient) {
76      if (!this.externalTemplate) {
77        this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
78      }
79    }
80  
81    /**
82     * Return the iBATIS Database Layer SqlMapClient that this template works with.
83     *
84     * @return the SqlMapClient
85     */
86    public final SqlMapClient getSqlMapClient() {
87      return this.sqlMapClientTemplate.getSqlMapClient();
88    }
89  
90    /**
91     * Set the SqlMapClientTemplate for this DAO explicitly, as an alternative to specifying a SqlMapClient.
92     *
93     * @param sqlMapClientTemplate
94     *          the SqlMapClientTemplate to use
95     *
96     * @see #setSqlMapClient
97     */
98    public final void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
99      Assert.notNull(sqlMapClientTemplate, "SqlMapClientTemplate must not be null");
100     this.sqlMapClientTemplate = sqlMapClientTemplate;
101     this.externalTemplate = true;
102   }
103 
104   /**
105    * Return the SqlMapClientTemplate for this DAO, pre-initialized with the SqlMapClient or set explicitly.
106    *
107    * @return the SqlMapClientTemplate
108    */
109   public final SqlMapClientTemplate getSqlMapClientTemplate() {
110     return this.sqlMapClientTemplate;
111   }
112 
113   @Override
114   protected final void checkDaoConfig() {
115     if (!this.externalTemplate) {
116       this.sqlMapClientTemplate.afterPropertiesSet();
117     }
118   }
119 
120 }