我的搜索功能,我有3种搜索
我的问题是是最好的方法来做DAO方法吗?
在我的struts2方法中,我有
public String execute(){ //假设这些从JSP 传入if(searchByAnId.equals(paramSearch)) {清单< DTO> datalist = this.someDao.implementList(theIdParam); } else if(searchByAnOtherParam.equals(paramSearch)) {列表< DTO> datalist = this.someDao.implementAnotherList(param1,param2,param3,param4) // more params } else {列表&DTO> datalist = this.someDao.implementListAll(); } returnsuccess; }我正在阅读设计模式,如Factory方法,Decorator方法,Observer方法,但我不知道哪一个是最适合的(或没有第三方插件的其他任何东西)适合这个?
解决方案我通常倾向于创建一个基本的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); }然后根据我的要求提供一个或多个实现:
//使用JPA public class JpaDAO< T>执行常用dao方法实现DAO< T> { private EntityManager em; public JpaDao(EntityManager em){this.em = em; } //使用JPA的默认实现... } //使用JDBC执行常用dao方法 public class JdbcDAO< T>实现DAO< T> { private Connection conn; public JdbcDao(Connection conn){this.conn = conn; } //使用JDBC的默认实现}现在,假设我有以下人员类:
public class Person implements DomainEntity { private Long id; private String firstName; private String lastName; // getters / setters ... }我首先定义一个通用的PersonDAO接口,如下所示:
public interface PersonDAO实现DAO< Person> { public List< Person> findByFirstName(String name); public List< Person> findByLastName(String name); }请注意,在我上面的具体实体dao界面中,我只包括额外的特定于我的域实体的方法。常见的方法是由超级接口继承,并使用泛型参数化到我的域实体。
现在最后一件事是定义我的实体特定方法的不同实现,如所以:
package mypackage.daos.jpa; public class PersonDAOImpl扩展JpaDAO< Person>实现PersonDAO { //这里我只实现在最后一个接口中定义的实体特定dao方法 //。 }如果我还需要提供一个替代的DAO实现(比如说基于jdbc的JPA),它与创建第二个类(最好在单独的包中)一样简单:
package mypackage.daos.jdbc ; public class PersonDAOImpl扩展JdbcDAO< Person>实现PersonDAO { //再次,我只实现实体特定的DAO方法,因为 //默认已经在超级类中实现... }这是一个很好的事情,你可以在没有调用代码的情况下切换实现,受到影响:
//使用我的dao 的一个服务类public class PersonService { private PersonDAO dao; public PersonService(PersonDAO dao){this.dao = dao} public void doSomethingUseful(){ //在这里使用dao ... } }通常,正确的dao实现(jdbc或jpa)将是在创建服务期间通过构造函数注入。当然,如果你喜欢,你可以只有一个实现(即jpa)。
Currently I am using struts2 Framework for my work's project, and while designing my DAO classes I have a question in my mind to improve on the design patterns.
On my search function, I have 3 kinds of search
My question is, what is the best way to do the DAO method?
in my struts2 method, I'm having
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 was reading on Design Patterns such as Factory methods, Decorator methods, Observer method, but I wasn't sure which one is the most appropriate ones (or anything else without 3rd party plugin) that suits this?
解决方案I usually tend to create a basic dao interface with the definitions of methods common to all my domain entities, such as:
// 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); }And then provide one or more implementations based on my requirements:
// 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 }Now, assuming i have the following person class:
public class Person implements DomainEntity { private Long id; private String firstName; private String lastName; // getters/setters... }I first define a generic PersonDAO interface like so:
public interface PersonDAO implements DAO<Person> { public List<Person> findByFirstName(String name); public List<Person> findByLastName(String name); }Note that in my specific entity dao interface above, i have included only the extra methods which are specific to my domain entity. Common methods are inherited by the super interface and are parameterised to my domain entity using generics.
Now the last remaining thing is to define different implementations of my entity specific methods, like so:
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. }If i also need to provide an alternative DAO implementation (say based on jdbc instead of JPA), its as easy as creating a second class (preferably in a separate package):
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... }The nice thing about this is that you can switch implementations without the calling code to get affected by that:
// 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... } }Typically, the proper dao implementation (jdbc or jpa) would be injected via the constructor during service creation. Of course you can have only one implementation if you like (i.e. jpa).
更多推荐
Java EE DAO / DTO(数据传输对象)设计模式
发布评论