Spring + iBatis // Part 5 // The DAO
We declared a DAO in our applicationContext.xml file. It’s time to create that DAO now by starting with an interface declaration. I called this interface MyDAO.java.
package com.example;
import java.util.List;
public interface MyDAO {
FilePickup selectFilepickupInstruction(long id);
List<FilePickup> selectFilepickupInstructions();
}
Then we need to create the class that will implement this interface. The name of this class should match that specified in the applicationContext.xml. In this example it’s MyDAOImpl.java.
package com.example; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; public class MyDAOImpl extends SqlMapClientDaoSupport implements MyDAO { public MyDAOImpl() { } public FilePickup selectFilepickupInstruction(long id) { return (FilePickup)getSqlMapClientTemplate().queryForObject( "selectFilepickupInstruction",id); } public List<FilePickup> selectFilepickupInstructions() { return getSqlMapClientTemplate().queryForList( "selectFilepickupInstructions",null); } }
Note that a java.util.list is used to handle results from a sql statement that returns multiple rows. The strings used in the queryForList() and queryForObject() methods are those names declared in the mapping stage.
Advertisement
