What are the DAO, DTO and Service layers in Spring Framework?

后端 未结 5 1840
陌清茗
陌清茗 2020-12-12 10:29

I am writing RESTful services using spring and hibernate. I read many resource in internet, but they did not clarify my doubts. Please explain me in details what are DAO

5条回答
  •  情歌与酒
    2020-12-12 11:06

    Enterprise application is divided into tiers for easy maintenance and development. Tiers are dedicated to particular type of tasks like

    • presentation layer (UI)
    • Business layer
    • Data access layer (DAO, DTO)

    Why this design: Let's pick an example you have an application which reads data from db and performs some business logic on it then present it to user. Now if you want to change your DB let say earlier application was running on Oracle now you want to use mysql so if you don't develop it in tiers you will doing changes everywhere in application. But if you implement DAO in application then this can be done easily

    DAO: Data Access Object is design pattern just provide an interface for accessing data to service layer and provide different implementations for different data sources (Databases, File systems)

    Example code:

    public interface DaoService {
        public boolean create(Object record);
        public CustomerTemp findTmp(String id);
        public Customer find(String id);
        public List getAllTmp();
        public List getAll();
        public boolean update(Object record);
        public boolean delete(Object record);   
        public User getUser(String email);
        public boolean addUser(User user);
    }
    

    Service layer using Dao

    @Service("checkerService")
    public class CheckerServiceImpl implements CheckerService{
    
    @Autowired
    @Qualifier("customerService")
    private DaoService daoService;
    

    Now i can provide any implementation of DaoService interface. Service and DTO are also used for separation of concerns.

提交回复
热议问题