KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ibatis > SqlMapTemplate


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;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.sql.DataSource JavaDoc;
25
26 import com.ibatis.db.sqlmap.MappedStatement;
27 import com.ibatis.db.sqlmap.RowHandler;
28 import com.ibatis.db.sqlmap.SqlMap;
29
30 import org.springframework.dao.DataAccessException;
31 import org.springframework.jdbc.datasource.DataSourceUtils;
32 import org.springframework.jdbc.support.JdbcAccessor;
33 import org.springframework.util.Assert;
34
35 /**
36  * Helper class that simplifies data access via the MappedStatement API of iBATIS
37  * SQL Maps, and converts checked SQLExceptions into unchecked DataAccessExceptions,
38  * following the <code>org.springframework.dao</code> exception hierarchy.
39  * Uses the same SQLExceptionTranslator mechanism as JdbcTemplate.
40  *
41  * <p>The main method is execute, taking the name of a mapped statement defined
42  * in the iBATIS SqlMap config file and a callback that implements a data access
43  * action on the specified statement.
44  *
45  * <p>This class provides numerous convenience methods that mirror MappedStatement's
46  * executeXXX methods. See the MappedStatement javadocs for details on those methods.
47  *
48  * <p>NOTE: The SqlMap/MappedStatement API is the one to use with iBATIS SQL Maps 1.x.
49  * The SqlMapClient/SqlMapSession API is only available with SQL Maps 2.
50  *
51  * @author Juergen Hoeller
52  * @since 28.11.2003
53  * @see #execute
54  * @see #setSqlMap
55  * @see #setDataSource
56  * @see #setExceptionTranslator
57  * @see com.ibatis.db.sqlmap.MappedStatement
58  */

59 public class SqlMapTemplate extends JdbcAccessor implements SqlMapOperations {
60
61     private SqlMap sqlMap;
62
63
64     /**
65      * Create a new SqlMapTemplate.
66      */

67     public SqlMapTemplate() {
68     }
69
70     /**
71      * Create a new SqlMapTemplate.
72      * @param dataSource JDBC DataSource to obtain connections from
73      * @param sqlMap iBATIS SqlMap that defines the mapped statements
74      */

75     public SqlMapTemplate(DataSource JavaDoc dataSource, SqlMap sqlMap) {
76         setDataSource(dataSource);
77         setSqlMap(sqlMap);
78         afterPropertiesSet();
79     }
80
81     /**
82      * Set the iBATIS Database Layer SqlMap that defines the mapped statements.
83      */

84     public void setSqlMap(SqlMap sqlMap) {
85         this.sqlMap = sqlMap;
86     }
87
88     /**
89      * Return the iBATIS Database Layer SqlMap that this template works with.
90      */

91     public SqlMap getSqlMap() {
92         return sqlMap;
93     }
94
95     public void afterPropertiesSet() {
96         super.afterPropertiesSet();
97         if (this.sqlMap == null) {
98             throw new IllegalArgumentException JavaDoc("sqlMap is required");
99         }
100     }
101
102
103     /**
104      * Execute the given data access action on the given iBATIS mapped statement.
105      * @param statementName name of the statement mapped in the iBATIS SqlMap config file
106      * @param action callback object that specifies the data access action
107      * @return a result object returned by the action, or null
108      * @throws DataAccessException in case of SQL Maps errors
109      */

110     public Object JavaDoc execute(String JavaDoc statementName, SqlMapCallback action) throws DataAccessException {
111         Assert.notNull(this.sqlMap, "No SqlMap specified");
112         MappedStatement stmt = this.sqlMap.getMappedStatement(statementName);
113         Connection JavaDoc con = DataSourceUtils.getConnection(getDataSource());
114         try {
115             return action.doInMappedStatement(stmt, con);
116         }
117         catch (SQLException JavaDoc ex) {
118             throw getExceptionTranslator().translate("SqlMap operation", null, ex);
119         }
120         finally {
121             DataSourceUtils.releaseConnection(con, getDataSource());
122         }
123     }
124
125     /**
126      * Execute the given data access action on the given mapped statement,
127      * expecting a List result.
128      * @param statementName name of the statement mapped in the iBATIS SqlMap config file
129      * @param action callback object that specifies the data access action
130      * @return a List result
131      * @throws DataAccessException in case of SQL Maps errors
132      */

133     public List JavaDoc executeWithListResult(String JavaDoc statementName, SqlMapCallback action)
134         throws DataAccessException {
135         return (List JavaDoc) execute(statementName, action);
136     }
137
138     /**
139      * Execute the given data access action on the given mapped statement,
140      * expecting a Map result.
141      * @param statementName name of the statement mapped in the iBATIS SqlMap config file
142      * @param action callback object that specifies the data access action
143      * @return a Map result
144      * @throws DataAccessException in case of SQL Maps errors
145      */

146     public Map JavaDoc executeWithMapResult(String JavaDoc statementName, SqlMapCallback action)
147         throws DataAccessException {
148         return (Map JavaDoc) execute(statementName, action);
149     }
150
151
152     public Object JavaDoc executeQueryForObject(String JavaDoc statementName, final Object JavaDoc parameterObject)
153             throws DataAccessException {
154         return execute(statementName, new SqlMapCallback() {
155             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
156                 return stmt.executeQueryForObject(con, parameterObject);
157             }
158         });
159     }
160
161     public Object JavaDoc executeQueryForObject(
162             String JavaDoc statementName, final Object JavaDoc parameterObject, final Object JavaDoc resultObject)
163             throws DataAccessException {
164         return execute(statementName, new SqlMapCallback() {
165             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
166                 return stmt.executeQueryForObject(con, parameterObject, resultObject);
167             }
168         });
169     }
170
171     public List JavaDoc executeQueryForList(String JavaDoc statementName, final Object JavaDoc parameterObject)
172             throws DataAccessException {
173         return executeWithListResult(statementName, new SqlMapCallback() {
174             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
175                 return stmt.executeQueryForList(con, parameterObject);
176             }
177         });
178     }
179
180     public List JavaDoc executeQueryForList(
181             String JavaDoc statementName, final Object JavaDoc parameterObject, final int skipResults, final int maxResults)
182             throws DataAccessException {
183         return executeWithListResult(statementName, new SqlMapCallback() {
184             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
185                 return stmt.executeQueryForList(con, parameterObject, skipResults, maxResults);
186             }
187         });
188     }
189
190     public Map JavaDoc executeQueryForMap(
191             String JavaDoc statementName, final Object JavaDoc parameterObject, final String JavaDoc keyProperty)
192             throws DataAccessException {
193         return executeWithMapResult(statementName, new SqlMapCallback() {
194             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
195                 return stmt.executeQueryForMap(con, parameterObject, keyProperty);
196             }
197         });
198     }
199
200     public Map JavaDoc executeQueryForMap(
201             String JavaDoc statementName, final Object JavaDoc parameterObject, final String JavaDoc keyProperty, final String JavaDoc valueProperty)
202             throws DataAccessException {
203         return executeWithMapResult(statementName, new SqlMapCallback() {
204             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
205                 return stmt.executeQueryForMap(con, parameterObject, keyProperty, valueProperty);
206             }
207         });
208     }
209
210     public void executeQueryWithRowHandler(
211             String JavaDoc statementName, final Object JavaDoc parameterObject, final RowHandler rowHandler)
212             throws DataAccessException {
213         execute(statementName, new SqlMapCallback() {
214             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
215                 stmt.executeQueryWithRowHandler(con, parameterObject, rowHandler);
216                 return null;
217             }
218         });
219     }
220
221     public int executeUpdate(String JavaDoc statementName, final Object JavaDoc parameterObject)
222             throws DataAccessException {
223         Integer JavaDoc result = (Integer JavaDoc) execute(statementName, new SqlMapCallback() {
224             public Object JavaDoc doInMappedStatement(MappedStatement stmt, Connection JavaDoc con) throws SQLException JavaDoc {
225                 return new Integer JavaDoc(stmt.executeUpdate(con, parameterObject));
226             }
227         });
228         return result.intValue();
229     }
230
231 }
232
Popular Tags