Java EE DAO / DTO (Data Transfer Object) Design Patterns

I am currently using the struts2 Framework for my working project, and when developing my DAO classes, I have a question that should improve design patterns.

In my search function I have 3 types of search

  • search with one parameter and another,
  • search with several parameters,
  • search without parameters.

My question is, what is the best way to make a DAO method?

in my struts2 method, I have

public String execute() {

    //assuming these are passed in from JSP
    if ("searchByAnId".equals(paramSearch))
    {
        List<DTO> datalist = this.someDao.implementList(theIdParam);

    } else if("searchByAnOtherParam".equals(paramSearch))
    {
        List<DTO> datalist = this.someDao.implementAnotherList(param1, param2, param3, param4)
        // more params
    } else
    {
        List<DTO> datalist = this.someDao.implementListAll();
    }       

    return "success";
}

I read about design patterns, such as Factory methods, Decorator methods, Observer method, but I was not sure which one is most suitable (or something else without a third-party plugin) that is suitable for this?

+5
source share
1 answer

dao , , :

// marker interface
public interface DomainEntity extends Serializable { }

// common dao methods
public interface DAO<T extends DomainEntity> {
  public T findById(Long id);
  public List<T> findAll();
  public T save(T entity);
  public boolean update(T entity);
  public boolean delete(T entity);
}

:

// implementation of common dao methods using JPA
public class JpaDAO<T> implements DAO<T> {

    private EntityManager em;

    public JpaDao(EntityManager em) { this.em = em; }

   // Default implementations using JPA...
}

// implementation of common dao methods using JDBC
public class JdbcDAO<T> implements DAO<T> {

  private Connection conn;

  public JdbcDao(Connection conn) { this.conn = conn; }

  // Default implementations using JDBC
}

, :

public class Person implements DomainEntity {
  private Long id;
  private String firstName;
  private String lastName;

  // getters/setters...
}

PersonDAO :

public interface PersonDAO implements DAO<Person> {
  public List<Person> findByFirstName(String name);
  public List<Person> findByLastName(String name);
}

, dao , . - .

, , - , :

package mypackage.daos.jpa;

public class PersonDAOImpl extends JpaDAO<Person> implements PersonDAO {
   // here i implement only the entity specific dao methods 
   // defined in the last interface.
}

DAO (, jdbc JPA), , ( ):

package mypackage.daos.jdbc;

public class PersonDAOImpl extends JdbcDAO<Person> implements PersonDAO {
  // again i only implement the entity specific DAO methods since
  // defaults have been implemented in the super class...
}

, , :

// a service class that uses my dao
public class PersonService {

  private PersonDAO dao;

  public PersonService(PersonDAO dao) { this.dao = dao }

  public void doSomethingUseful() {
     // use the dao here...
  }
}

, dao (jdbc jpa) . , , (.. Jpa).

+6

All Articles