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.common.util.PaginatedList; 27 import com.ibatis.sqlmap.client.SqlMapClient; 28 import com.ibatis.sqlmap.client.SqlMapExecutor; 29 import com.ibatis.sqlmap.client.SqlMapSession; 30 import com.ibatis.sqlmap.client.event.RowHandler; 31 import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; 32 33 import org.springframework.dao.DataAccessException; 34 import org.springframework.dao.InvalidDataAccessApiUsageException; 35 import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException; 36 import org.springframework.jdbc.datasource.DataSourceUtils; 37 import org.springframework.jdbc.support.JdbcAccessor; 38 import org.springframework.util.Assert; 39 40 86 public class SqlMapClientTemplate extends JdbcAccessor implements SqlMapClientOperations { 87 88 private SqlMapClient sqlMapClient; 89 90 private boolean lazyLoadingAvailable = true; 91 92 93 96 public SqlMapClientTemplate() { 97 } 98 99 103 public SqlMapClientTemplate(SqlMapClient sqlMapClient) { 104 setSqlMapClient(sqlMapClient); 105 afterPropertiesSet(); 106 } 107 108 113 public SqlMapClientTemplate(DataSource dataSource, SqlMapClient sqlMapClient) { 114 setDataSource(dataSource); 115 setSqlMapClient(sqlMapClient); 116 afterPropertiesSet(); 117 } 118 119 122 public void setSqlMapClient(SqlMapClient sqlMapClient) { 123 this.sqlMapClient = sqlMapClient; 124 } 125 126 129 public SqlMapClient getSqlMapClient() { 130 return sqlMapClient; 131 } 132 133 137 public DataSource getDataSource() { 138 DataSource ds = super.getDataSource(); 139 return (ds != null ? ds : this.sqlMapClient.getDataSource()); 140 } 141 142 public void afterPropertiesSet() { 143 if (this.sqlMapClient == null) { 144 throw new IllegalArgumentException ("sqlMapClient is required"); 145 } 146 if (this.sqlMapClient instanceof ExtendedSqlMapClient) { 147 this.lazyLoadingAvailable = (((ExtendedSqlMapClient) this.sqlMapClient).getDelegate().getTxManager() != null); 150 } 151 super.afterPropertiesSet(); 152 } 153 154 155 161 public Object execute(SqlMapClientCallback action) throws DataAccessException { 162 Assert.notNull(action, "Callback object must not be null"); 163 Assert.notNull(this.sqlMapClient, "No SqlMapClient specified"); 164 165 172 SqlMapSession session = this.sqlMapClient.openSession(); 173 Connection ibatisCon = null; 174 try { 175 if (logger.isDebugEnabled()) { 176 logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation"); 177 } 178 Connection springCon = null; 179 try { 180 ibatisCon = session.getCurrentConnection(); 181 if (ibatisCon == null) { 182 springCon = DataSourceUtils.getConnection(getDataSource()); 183 session.setUserConnection(springCon); 184 if (logger.isDebugEnabled()) { 185 logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation"); 186 } 187 } 188 else { 189 if (logger.isDebugEnabled()) { 190 logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation"); 191 } 192 } 193 return action.doInSqlMapClient(session); 194 } 195 catch (SQLException ex) { 196 throw getExceptionTranslator().translate("SqlMapClient operation", null, ex); 197 } 198 finally { 199 DataSourceUtils.releaseConnection(springCon, getDataSource()); 200 } 201 } 202 finally { 203 if (ibatisCon == null) { 206 session.close(); 207 } 208 } 209 } 210 211 218 public List executeWithListResult(SqlMapClientCallback action) throws DataAccessException { 219 return (List ) execute(action); 220 } 221 222 229 public Map executeWithMapResult(SqlMapClientCallback action) throws DataAccessException { 230 return (Map ) execute(action); 231 } 232 233 234 public Object queryForObject(String statementName) throws DataAccessException { 235 return queryForObject(statementName, null); 236 } 237 238 public Object queryForObject(final String statementName, final Object parameterObject) 239 throws DataAccessException { 240 241 return execute(new SqlMapClientCallback() { 242 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 243 return executor.queryForObject(statementName, parameterObject); 244 } 245 }); 246 } 247 248 public Object queryForObject( 249 final String statementName, final Object parameterObject, final Object resultObject) 250 throws DataAccessException { 251 252 return execute(new SqlMapClientCallback() { 253 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 254 return executor.queryForObject(statementName, parameterObject, resultObject); 255 } 256 }); 257 } 258 259 public List queryForList(String statementName) throws DataAccessException { 260 return queryForList(statementName, null); 261 } 262 263 public List queryForList(final String statementName, final Object parameterObject) 264 throws DataAccessException { 265 266 return executeWithListResult(new SqlMapClientCallback() { 267 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 268 return executor.queryForList(statementName, parameterObject); 269 } 270 }); 271 } 272 273 public List queryForList(String statementName, int skipResults, int maxResults) 274 throws DataAccessException { 275 276 return queryForList(statementName, null, skipResults, maxResults); 277 } 278 279 public List queryForList( 280 final String statementName, final Object parameterObject, final int skipResults, final int maxResults) 281 throws DataAccessException { 282 283 return executeWithListResult(new SqlMapClientCallback() { 284 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 285 return executor.queryForList(statementName, parameterObject, skipResults, maxResults); 286 } 287 }); 288 } 289 290 public void queryWithRowHandler(String statementName, RowHandler rowHandler) 291 throws DataAccessException { 292 293 queryWithRowHandler(statementName, null, rowHandler); 294 } 295 296 public void queryWithRowHandler( 297 final String statementName, final Object parameterObject, final RowHandler rowHandler) 298 throws DataAccessException { 299 300 execute(new SqlMapClientCallback() { 301 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 302 executor.queryWithRowHandler(statementName, parameterObject, rowHandler); 303 return null; 304 } 305 }); 306 } 307 308 311 public PaginatedList queryForPaginatedList(String statementName, int pageSize) 312 throws DataAccessException { 313 314 return queryForPaginatedList(statementName, null, pageSize); 315 } 316 317 320 public PaginatedList queryForPaginatedList( 321 final String statementName, final Object parameterObject, final int pageSize) 322 throws DataAccessException { 323 324 if (!this.lazyLoadingAvailable) { 326 throw new InvalidDataAccessApiUsageException( 327 "SqlMapClient needs to have DataSource to allow for lazy loading" + 328 " - specify SqlMapClientFactoryBean's 'dataSource' property"); 329 } 330 331 return (PaginatedList) execute(new SqlMapClientCallback() { 332 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 333 return executor.queryForPaginatedList(statementName, parameterObject, pageSize); 334 } 335 }); 336 } 337 338 public Map queryForMap( 339 final String statementName, final Object parameterObject, final String keyProperty) 340 throws DataAccessException { 341 342 return executeWithMapResult(new SqlMapClientCallback() { 343 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 344 return executor.queryForMap(statementName, parameterObject, keyProperty); 345 } 346 }); 347 } 348 349 public Map queryForMap( 350 final String statementName, final Object parameterObject, final String keyProperty, final String valueProperty) 351 throws DataAccessException { 352 353 return executeWithMapResult(new SqlMapClientCallback() { 354 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 355 return executor.queryForMap(statementName, parameterObject, keyProperty, valueProperty); 356 } 357 }); 358 } 359 360 public Object insert(String statementName) throws DataAccessException { 361 return insert(statementName, null); 362 } 363 364 public Object insert(final String statementName, final Object parameterObject) 365 throws DataAccessException { 366 367 return execute(new SqlMapClientCallback() { 368 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 369 return executor.insert(statementName, parameterObject); 370 } 371 }); 372 } 373 374 public int update(String statementName) throws DataAccessException { 375 return update(statementName, null); 376 } 377 378 public int update(final String statementName, final Object parameterObject) 379 throws DataAccessException { 380 381 Integer result = (Integer ) execute(new SqlMapClientCallback() { 382 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 383 return new Integer (executor.update(statementName, parameterObject)); 384 } 385 }); 386 return result.intValue(); 387 } 388 389 public void update(String statementName, Object parameterObject, int requiredRowsAffected) 390 throws DataAccessException { 391 392 int actualRowsAffected = update(statementName, parameterObject); 393 if (actualRowsAffected != requiredRowsAffected) { 394 throw new JdbcUpdateAffectedIncorrectNumberOfRowsException( 395 statementName, requiredRowsAffected, actualRowsAffected); 396 } 397 } 398 399 public int delete(String statementName) throws DataAccessException { 400 return delete(statementName, null); 401 } 402 403 public int delete(final String statementName, final Object parameterObject) 404 throws DataAccessException { 405 406 Integer result = (Integer ) execute(new SqlMapClientCallback() { 407 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 408 return new Integer (executor.delete(statementName, parameterObject)); 409 } 410 }); 411 return result.intValue(); 412 } 413 414 public void delete(String statementName, Object parameterObject, int requiredRowsAffected) 415 throws DataAccessException { 416 417 int actualRowsAffected = delete(statementName, parameterObject); 418 if (actualRowsAffected != requiredRowsAffected) { 419 throw new JdbcUpdateAffectedIncorrectNumberOfRowsException( 420 statementName, requiredRowsAffected, actualRowsAffected); 421 } 422 } 423 424 } 425 | Popular Tags |