I write a web application on java ee. Arranged connection pool
for connection with db mysql
, created classes of entities. Now you need to implement a layer between db and the application. I know that the DAO template is used for this. Can you tell the teapot how to organize it, what is better to read?
|
1 answer
I guess I would do
- interface with methods of working with the database, standard CRUD
- abstract class (AbstractBaseDao) in which I "hid" connections to the database, methods - open (), close ();
- I created a class for implementing all of this, inherited from an abstract class, which would allow us to have ready-made methods for connecting to the database and a link to it and implement the interface with methods.
It is also a good option - the interface methods cannot be endured, but left in an abstract class.
Sometimes it can all be redundant architecture, so you can fit everything into one class.
Advantages of DAO Intefeys - allows you to change the database without affecting the logic of the program.
- Somewhere I saw the generic interface, there were also options with an abstract class. Are there any standard implementations? - uladzimir
- Is it necessary to use factory when implementing dao? - uladzimir
- generic interface - probably it works more like a template, i.e. you have a group of different objects, but they have the same operations with the database, for example, there are pears, apples and TVs and they should all be stored in the database, then you simply create a template-interface, and when you implement it in generics you specify an object, usually the IDE will add implementation methods of the desired type, and in each implementation of the interface you already properly organize the storage of the object. Standard - these are examples on the Internet, but for your small project, you can not bother with this =) - Gorets
- > is it necessary to use factory when implementing dao? If you mean DAO as an object, then no. If the pattern is DAO, then yes. This template implies the presence of a factory object that will provide access to all DAO objects - jmu
- 2
|