Skip to main content

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.

Comments

Popular posts from this blog

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2 Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- Tiles --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> <scope>compile</s...

Getting started with Prime faces 2

Prime faces is an amazing JSF framework from Cagatay Civici ( http://cagataycivici.wordpress.com/ ). Its wonderful because it is easy to use, minimal dependencies, has probably the widest set of controls among all JSF frameworks, easy to integrate with Spring (including Spring Security) , Java EE EJBs, and last but not the least mobile UI support. So I decided to give Prime faces a try, before selecting it to use in my projects. Step 1 – Create Maven 2 project As a first step to integrating Prime faces, create a Maven 2 project in Eclipse. You will need to select ‘maven-archetype-webapp’. Step 2 – Add repositories and dependencies in pom.xml I will be using Prime faces 2 with JSF 2 on Tomcat 6. Since the dependencies for Prime Faces and JSF 2 (JSF 2.0.3 is required) are available on different repositories, I will add them to my pom file first. The listing below shows my pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/X...

CKEDITOR 3.x - Simplest Ajax Submit Plugin

  I have assumed that you have downloaded and got started with CKEDITOR. Step 1 – The html file is shown below: <html> <head> <title>Writer</title> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <style> .cke_contents { height: 400px !important; } </style> </head> <body> <form action="sample_posteddata.php" method="post"> <textarea id="editor" > </textarea> <script type="text/javascript"> //<![CDATA[ CKEDITOR.replace( 'editor', { fullPage : true, uiColor : '#9AB8F3', toolbar : 'MyToolbar' }); //]]> </script> </form> </body> </html> Note that the jquery js...