1. /*
  2. * Copyright 2002-2004 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. package org.springframework.orm.ibatis.support;
  17. import javax.sql.DataSource;
  18. import com.ibatis.db.sqlmap.SqlMap;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.springframework.orm.ibatis.SqlMapTemplate;
  22. /**
  23. * Convenient super class for iBATIS SqlMap data access objects.
  24. * Requires a DataSource to be set, providing a SqlMapTemplate
  25. * based on it to subclasses.
  26. * @author Juergen Hoeller
  27. * @since 29.11.2003
  28. * @see org.springframework.orm.ibatis.SqlMapTemplate
  29. */
  30. public class SqlMapDaoSupport {
  31. protected final Log logger = LogFactory.getLog(getClass());
  32. private SqlMapTemplate sqlMapTemplate = new SqlMapTemplate();
  33. /**
  34. * Set the JDBC DataSource to be used by this DAO.
  35. */
  36. public final void setDataSource(DataSource dataSource) {
  37. this.sqlMapTemplate.setDataSource(dataSource);
  38. }
  39. /**
  40. * Return the JDBC DataSource used by this DAO.
  41. */
  42. public final DataSource getDataSource() {
  43. return sqlMapTemplate.getDataSource();
  44. }
  45. /**
  46. * Set the iBATIS Database Layer SqlMap to work with.
  47. */
  48. public final void setSqlMap(SqlMap sqlMap) {
  49. this.sqlMapTemplate.setSqlMap(sqlMap);
  50. }
  51. /**
  52. * Return the iBATIS Database Layer SqlMap that this template works with.
  53. */
  54. public final SqlMap getSqlMap() {
  55. return this.sqlMapTemplate.getSqlMap();
  56. }
  57. /**
  58. * Set the JdbcTemplate for this DAO explicitly,
  59. * as an alternative to specifying a DataSource.
  60. */
  61. public final void setSqlMapTemplate(SqlMapTemplate sqlMapTemplate) {
  62. this.sqlMapTemplate = sqlMapTemplate;
  63. }
  64. /**
  65. * Return the JdbcTemplate for this DAO,
  66. * pre-initialized with the DataSource or set explicitly.
  67. */
  68. public final SqlMapTemplate getSqlMapTemplate() {
  69. return sqlMapTemplate;
  70. }
  71. public final void afterPropertiesSet() throws Exception {
  72. this.sqlMapTemplate.afterPropertiesSet();
  73. initDao();
  74. }
  75. /**
  76. * Subclasses can override this for custom initialization behavior.
  77. * Gets called after population of this instance's bean properties.
  78. * @throws Exception if initialization fails
  79. */
  80. protected void initDao() throws Exception {
  81. }
  82. }