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