Java – generic Dao and service layer
•
Java
I will design a Dao layer for my application My focus is that services only call Dao, which is independent of the underlying implementation
public interface GenericSearchDao{ List getAll(); List getByQuery(String query); } public class UserJdbcSearch implements GenericSearchDao{ public List getAll(){ // Get all users; } List getByQuery(String query){ // Get users by query; } } public class UserFileSystemSearch implements GenericSearchDao{ public List getAll(){ // Get all users from file system; } List getByQuery(String query){ // Get users by query[this leads to invalid operation]; } } public userService { private GenericSearchDao dao = new UserFileSystemSearch(); public List getUsers(){ rturn dao.getAll(); } public List getByQuery(String query){ return dao.getByQuery(query); } }
Need help:
What should I do to get rid of the specific implementation of 'getbyquery', because the data storage can be RDBMS, file system, FTP, etc
How should I design my Dao layer?
If anyone says "delete getbyquery () from genericsearchdao", what should I do when I need data specific to business operations: for example, users with roles, users with products, etc
Solution
You may find this method very useful http://www.bejug.org/confluenceBeJUG/display/BeJUG/Generic +DAO+example
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码