Where The Streets Have No Name

SQL strings out of Interface 본문

Developement/Java

SQL strings out of Interface

highheat 2007. 11. 29. 10:20
Step 1 : Mofify Spring-Config.xml

<import resource="Spring-DAO-SQL-Config.xml"/>

<bean id="EMP_DAO" class="com.bea.dev2dev.dao.EmployeeDAOImpl">
<property name="dataSource">
<ref bean="FIREBIRD_DATASOURCE"></ref>
</property>
<property name="sqlBean">
<ref bean="SQL_REPOSITORY"></ref>
</property>
</bean>

Step 2 : Include the Spring-DAO-SQL-Config.xml

<beans>
<!-- Configure SQL Cache Bean -->

<bean id="SQL_REPOSITORY" class="com.bea.dev2dev.dao.helper.SQLCacheBean">
<property name="sqlCache">
<props>
<prop key="FIND_BY_SAL_RNG">
<![CDATA[
SELECT
EMP_NAME,EMP_NO,SALARY
FROM
EMP
WHERE
SALARY >= :MIN_SALARY AND SALARY <=:MAX_SALARY
]]>
</prop>

<prop key="FIND_BY_EMP_PK">
SELECT
*
FROM
EMP
WHERE
EMP_NO = :EMP_NO
</prop>
</props>
</property>
</bean>
</beans>

Step 3 : Introduce AbsBaseDAO.java
package com.bea.dev2dev.dao.helper;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

/**
*
* @author Dhrubo
*/
public class AbsBaseDAO extends NamedParameterJdbcDaoSupport{

private SQLCacheBean sqlCache;

public void setSqlBean(SQLCacheBean sqlCache)
{
this.sqlCache = sqlCache;
}

public String getSQL(String key)
{
return sqlCache.getSQL(key);
}
}

Step 4 : Modify the DAO Implementation class it now extends the AbsBaseDAO
public class EmployeeDAOImpl extends AbsBaseDAO implements IEmployeeDAO{

public List findBySalaryRange(Map salaryMap){

NamedParameterJdbcTemplate daoTmplt = getNamedParameterJdbcTemplate();
return daoTmplt.query(getSQL("FIND_BY_SAL_RNG"),salaryMap,new EmployeeTOMapper());
}
}

Step 5 : Remove the SQL string from the IEmployeeDAO interface