Saturday, May 19, 2012

Spring MVC + Spring Hibernate for basic CRUD operations

Most of java developers are using Spring framework for flexible designs.Specially for web based projects where we apply MVC design pattern. So in this post I am going to explain how to use spring framework to achieve that requirement.

I will explain it with the web.xml file which is easier to understand.

   
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
        1
      

    
        dispatcher
        *.do
    

    
        org.springframework.web.context.ContextLoaderListener
    
    
        contextConfigLocation
        classpath:/spring/applicationContext.xml
    
Here you can see I it triggers a dispatcher servlet when the request consist of .do extension. Following is the dispatcher file.
  
 
    
    

    
    
 
It defines the package where the spring should search for controllers. And also you can see it consists of the view resolver which uses by the controllers to resolve the views from the returned ModelAndViews.

And if you have a closer look in to the web.xml file you can see it has load the context configuration which acts as an ioc container of spring framework.

 
    
    
    

    
          

    
    

    
        
        
            
                ${hibernate.dialect}
                ${hibernate.show_sql}
            
        
        
    


    
    

    
DataSource properties are loaded from config.properties files in the resources directory. Then what you have to do is implement domain and its dao class. Here I have mentioned only the dao class. You can find domain class in the svn project.

 
import java.util.List;

import com.exilesoft.contactmanager.domain.Contact;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class ContactsDAO
{
    @Autowired
    private SessionFactory sessionFactory;

    public Contact getById(int id)
    {
        return (Contact) sessionFactory.getCurrentSession().get(Contact.class, id);
    }

    @SuppressWarnings("unchecked")
    public List searchContacts(String name)
    {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Contact.class);
        criteria.add(Restrictions.ilike("name", name+"%"));
        return criteria.list();
    }

    @SuppressWarnings("unchecked")
    public List getAllContacts()
    {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Contact.class);
        return criteria.list();
    }

    public int save(Contact contact)
    {
        return (Integer) sessionFactory.getCurrentSession().save(contact);
    }

    public void update(Contact contact)
    {
        sessionFactory.getCurrentSession().merge(contact);
    }

    public void delete(int id)
    {
        Contact c = getById(id);
        sessionFactory.getCurrentSession().delete(c);
    }
}
Then what you have to do is call those methods from the controller class. I will left the controller code and the jsp codes since it is easy to understand.
You can find the code in following repository.

http://es-code-snippets.googlecode.com/svn/trunk/contactmanager/

No comments:

Post a Comment