Saturday, May 29, 2010

Part IV : A Simple and Smart Result Mapper

In my last post, I have shown how the dao support class changed to convert list of maps to list of domain objects. Now the DAO implementation class must also change to use these new methods. Here is the modified DAO class.
Listing – UserDaoImpl.java
package net.sf.dms.security.dao.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sf.dms.security.dao.api.UserDao;
import net.sf.dms.security.domain.User;
import net.sf.spring.dao.AbstractBaseDaoSupport;

/**
* @author dhrubo
* 
*/
public class UserDaoImpl extends AbstractBaseDaoSupport implements UserDao {

private Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
@Override
public List<User> listUsers() {
return (List<User>)this.queryForList("listUsers", User.class);
}

@Override
public void save(User user) {
this.insert("saveUser", user.getEmail(),
user.getPassword(), user.getFirstName(), user.getLastName());
}

@Override
public void update(User user) {

}
@Override
public User findUserByUserName(String username) {
logger.debug("Loading user details as part of authentication");
return (User)this.queryForObject("findUserByUserName", User.class, username);  
}

@Override
public List<User> findUsersStartingWith(String nameStartsWith) {
return (List<User>)this.queryForList("findUsersStartingWith", User.class, nameStartsWith + "%");  
}
}


Now you can see clearly that the list or finder queries have been reduced to just 1 line. I guess this code reduction is same as in any ORM or datamapper. Now let us see how the SQL has changed. I have tested this code on Postgresql 8.3 and should work with any sensible database available.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="secSqlMap" class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map>
<entry key="listUsers">
<value>
<![CDATA[
SELECT first_name "firstName",last_name "lastName",email "userCode",user_id "userId" FROM t_user

]]>

</value>

</entry>

<entry key="saveUser">
<value>
<![CDATA[
INSERT INTO t_user(user_id, email, password, first_name,last_name) VALUES (nextval('t_user_seq'), ?, ?, ?, ?)

]]>

</value>

</entry>

<entry key="findUserByUserName">
<value>
<![CDATA[
SELECT first_name "firstName",last_name "latName",email "userCode",user_id "userId" FROM t_user WHERE email = ?

]]>

</value>

</entry>

<entry key="findUsersStartingWith">
<value>
<![CDATA[
SELECT first_name "firstName",last_name "latName",email "userCode",user_id "userId" FROM t_user 
WHERE first_name LIKE ?;

]]>

</value>

</entry>

</map>


</property>
</bean>

</beans>

Now you can see my source of meta data and how I use them to run this simple data mapper and move towards my goal of lightweight persistence and database independence.

No comments:

Post a Comment