baseDao

* Description: 支持hibernate,ibatis,JdbcTemplate.

 

 

1: baseDao

 

public class BaseDao extends HibernateDaoSupport {
 protected Log log = LogFactory.getLog(getClass());;
 protected HibernateTemplate hibernateTemplate;

 protected SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();

 protected JdbcTemplate jt;
 protected DataSource dataSource;
 protected String dbDialect;

 

 public String getDbDialect() {
  return dbDialect;
 }

 

 public void setDbDialect(String dbDialect) {
  this.dbDialect = dbDialect;
 }
 
 public final void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
  this.sqlMapClientTemplate.setDataSource(dataSource);
  this.jt = new JdbcTemplate(dataSource);
 }

 

 public final DataSource getDataSource() {
  return dataSource;
 }

 

 public final void setSqlMapClient(SqlMapClient sqlMapClient) {
  this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
 }

 

 public final SqlMapClient getSqlMapClient() {
  return this.sqlMapClientTemplate.getSqlMapClient();
 }

 

 public final void setSqlMapClientTemplate(
   SqlMapClientTemplate sqlMapClientTemplate) {
  if (sqlMapClientTemplate == null) {
   throw new IllegalArgumentException(
     "Cannot set sqlMapClientTemplate to null");
  }
  this.sqlMapClientTemplate = sqlMapClientTemplate;
 }

 

 public JdbcTemplate getJdbcTemplate() {
  return jt;
 }

 

 public void setJdbcTemplate(JdbcTemplate jt) {
  this.jt = jt;
 }

 

 public final SqlMapClientTemplate getSqlMapClientTemplate() {
  return sqlMapClientTemplate;
 }


}

  

2:spring配置文件中的配置

 

  

<bean id="baseDao"
        class="com.dao.BaseDao">
    <property name="sessionFactory">
      <ref bean="sessionFactoryFbth" />
    </property>
    <property name="sqlMapClient">
      <ref local="sqlMapClient" />
    </property>
    <property name="dataSource">
      <ref bean="dataSource.FBCM" />
    </property>
    <property name="dbDialect">
      <value>${sql.server.env}</value>
    </property>        
  </bean>

 

   <bean id="sqlMapClient"
        class="com.spring.stcSqlMapClientFactoryBean">
    <property name="configLocation">
      <value>sql-map-config.xml</value>
    </property>
  </bean>

 

 

 <bean id="dataSource.FBCM"
        class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
      <value>${jndi.ora}</value>
    </property>
  </bean>

 

  

 3:如何使用baseDao

 

 

 <bean id="fbcmCeditAppDao"
        class="com.dao.FbcmCeditAppDao"
        parent="baseDao" />

 

 public class FbcmCeditAppDao extends BaseDao {

}

  

 在该类中就可以使用使用HibernateDaoSupport提供的 getHibernateTemplate() 和  getSession()方法进行查询了。

 

你可能感兴趣的:(spring,Hibernate,bean,ibatis,SQL Server)