1 16 17 package org.springframework.orm.ibatis; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.sql.DataSource ; 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 59 public class SqlMapTemplate extends JdbcAccessor implements SqlMapOperations { 60 61 private SqlMap sqlMap; 62 63 64 67 public SqlMapTemplate() { 68 } 69 70 75 public SqlMapTemplate(DataSource dataSource, SqlMap sqlMap) { 76 setDataSource(dataSource); 77 setSqlMap(sqlMap); 78 afterPropertiesSet(); 79 } 80 81 84 public void setSqlMap(SqlMap sqlMap) { 85 this.sqlMap = sqlMap; 86 } 87 88 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 ("sqlMap is required"); 99 } 100 } 101 102 103 110 public Object execute(String statementName, SqlMapCallback action) throws DataAccessException { 111 Assert.notNull(this.sqlMap, "No SqlMap specified"); 112 MappedStatement stmt = this.sqlMap.getMappedStatement(statementName); 113 Connection con = DataSourceUtils.getConnection(getDataSource()); 114 try { 115 return action.doInMappedStatement(stmt, con); 116 } 117 catch (SQLException ex) { 118 throw getExceptionTranslator().translate("SqlMap operation", null, ex); 119 } 120 finally { 121 DataSourceUtils.releaseConnection(con, getDataSource()); 122 } 123 } 124 125 133 public List executeWithListResult(String statementName, SqlMapCallback action) 134 throws DataAccessException { 135 return (List ) execute(statementName, action); 136 } 137 138 146 public Map executeWithMapResult(String statementName, SqlMapCallback action) 147 throws DataAccessException { 148 return (Map ) execute(statementName, action); 149 } 150 151 152 public Object executeQueryForObject(String statementName, final Object parameterObject) 153 throws DataAccessException { 154 return execute(statementName, new SqlMapCallback() { 155 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 156 return stmt.executeQueryForObject(con, parameterObject); 157 } 158 }); 159 } 160 161 public Object executeQueryForObject( 162 String statementName, final Object parameterObject, final Object resultObject) 163 throws DataAccessException { 164 return execute(statementName, new SqlMapCallback() { 165 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 166 return stmt.executeQueryForObject(con, parameterObject, resultObject); 167 } 168 }); 169 } 170 171 public List executeQueryForList(String statementName, final Object parameterObject) 172 throws DataAccessException { 173 return executeWithListResult(statementName, new SqlMapCallback() { 174 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 175 return stmt.executeQueryForList(con, parameterObject); 176 } 177 }); 178 } 179 180 public List executeQueryForList( 181 String statementName, final Object parameterObject, final int skipResults, final int maxResults) 182 throws DataAccessException { 183 return executeWithListResult(statementName, new SqlMapCallback() { 184 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 185 return stmt.executeQueryForList(con, parameterObject, skipResults, maxResults); 186 } 187 }); 188 } 189 190 public Map executeQueryForMap( 191 String statementName, final Object parameterObject, final String keyProperty) 192 throws DataAccessException { 193 return executeWithMapResult(statementName, new SqlMapCallback() { 194 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 195 return stmt.executeQueryForMap(con, parameterObject, keyProperty); 196 } 197 }); 198 } 199 200 public Map executeQueryForMap( 201 String statementName, final Object parameterObject, final String keyProperty, final String valueProperty) 202 throws DataAccessException { 203 return executeWithMapResult(statementName, new SqlMapCallback() { 204 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 205 return stmt.executeQueryForMap(con, parameterObject, keyProperty, valueProperty); 206 } 207 }); 208 } 209 210 public void executeQueryWithRowHandler( 211 String statementName, final Object parameterObject, final RowHandler rowHandler) 212 throws DataAccessException { 213 execute(statementName, new SqlMapCallback() { 214 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 215 stmt.executeQueryWithRowHandler(con, parameterObject, rowHandler); 216 return null; 217 } 218 }); 219 } 220 221 public int executeUpdate(String statementName, final Object parameterObject) 222 throws DataAccessException { 223 Integer result = (Integer ) execute(statementName, new SqlMapCallback() { 224 public Object doInMappedStatement(MappedStatement stmt, Connection con) throws SQLException { 225 return new Integer (stmt.executeUpdate(con, parameterObject)); 226 } 227 }); 228 return result.intValue(); 229 } 230 231 } 232 | Popular Tags |