KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ibatis > support > SqlMapDaoSupport


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.orm.ibatis.support;
18
19 import javax.sql.DataSource JavaDoc;
20
21 import com.ibatis.db.sqlmap.SqlMap;
22
23 import org.springframework.dao.support.DaoSupport;
24 import org.springframework.orm.ibatis.SqlMapTemplate;
25
26 /**
27  * Convenient super class for iBATIS SqlMap data access objects.
28  * Requires a SqlMap to be set, providing a SqlMapTemplate
29  * based on it to subclasses.
30  *
31  * <p>Instead of a plain SqlMap, you can also pass a preconfigured
32  * SqlMapTemplate instance in. This allows you to share your
33  * SqlMapTemplate configuration for all your DAOs, for example
34  * a custom SQLExceptionTranslator to use.
35  *
36  * @author Juergen Hoeller
37  * @since 29.11.2003
38  * @see #setSqlMap
39  * @see #setSqlMapTemplate
40  * @see org.springframework.orm.ibatis.SqlMapTemplate
41  * @see org.springframework.orm.ibatis.SqlMapTemplate#setExceptionTranslator
42  */

43 public abstract class SqlMapDaoSupport extends DaoSupport {
44
45     private SqlMapTemplate sqlMapTemplate = new SqlMapTemplate();
46
47     private boolean externalTemplate = false;
48
49     /**
50      * Set the JDBC DataSource to be used by this DAO.
51      * Not required: The SqlMap might carry a shared DataSource.
52      * @see #setSqlMap
53      */

54     public final void setDataSource(DataSource JavaDoc dataSource) {
55       this.sqlMapTemplate.setDataSource(dataSource);
56     }
57
58     /**
59      * Return the JDBC DataSource used by this DAO.
60      */

61     public final DataSource JavaDoc getDataSource() {
62         return (this.sqlMapTemplate != null ? this.sqlMapTemplate.getDataSource() : null);
63     }
64
65     /**
66      * Set the iBATIS Database Layer SqlMap to work with.
67      * Either this or a "sqlMapTemplate" is required.
68      * @see #setSqlMapTemplate
69      */

70     public final void setSqlMap(SqlMap sqlMap) {
71         this.sqlMapTemplate.setSqlMap(sqlMap);
72     }
73
74     /**
75      * Return the iBATIS Database Layer SqlMap that this template works with.
76      */

77     public final SqlMap getSqlMap() {
78         return this.sqlMapTemplate.getSqlMap();
79     }
80
81     /**
82      * Set the SqlMapTemplate for this DAO explicitly,
83      * as an alternative to specifying a SqlMap.
84      * @see #setSqlMap
85      */

86     public final void setSqlMapTemplate(SqlMapTemplate sqlMapTemplate) {
87         if (sqlMapTemplate == null) {
88             throw new IllegalArgumentException JavaDoc("Cannot set sqlMapTemplate to null");
89         }
90         this.sqlMapTemplate = sqlMapTemplate;
91         this.externalTemplate = true;
92     }
93
94     /**
95      * Return the SqlMapTemplate for this DAO,
96      * pre-initialized with the SqlMap or set explicitly.
97      */

98     public final SqlMapTemplate getSqlMapTemplate() {
99       return sqlMapTemplate;
100     }
101
102     protected final void checkDaoConfig() {
103         if (!this.externalTemplate) {
104             this.sqlMapTemplate.afterPropertiesSet();
105         }
106     }
107
108 }
109
Popular Tags