简体   繁体   English

DAO界面,类和工厂

[英]DAO interface, class and factories

So I am currently learning about the Dao in Java. 因此,我目前正在学习Java中的Dao。 I am finding it hard to grasp the concepts for the most part. 我发现很难掌握大部分概念。 I believe the Dao interface is for listing out the methods that are going to help you CRUD the database. 我相信Dao界面用于列出将帮助您CRUD数据库的方法。 As for the Dao concrete class.. that is just a class that does the actual implementation of the Dao interface. 至于Dao具体类,那只是一个实际实现Dao接口的类。 But I do not understand what the Dao Factory purpose is. 但是我不明白道工厂的目的是什么。 Any input would be appreciated. 任何输入将不胜感激。

See: https://www.baeldung.com/java-dao-pattern . 参见: https : //www.baeldung.com/java-dao-pattern The Dao is an interface. 道是一个接口。 To use it, you would have to implement it or better yet include a Jpa provider in your project that already has implemented it. 要使用它,您将不得不实现它,或者更好地在已经实现它的项目中包括一个Jpa提供程序。 The factory allows different implementations of the interface without the application knowing. 工厂允许在应用程序未知的情况下使用不同的接口实现。

For example, you might use an Oracle database today and tomorrow switch to MySql. 例如,您今天可能会使用Oracle数据库,明天可能会使用MySql。 You can just change your driver (a different jar) and some properties. 您可以只更改驱动程序(其他jar)和一些属性。 The factory will then get the proper implementation of the Dao for the configured database. 然后,工厂将为配置的数据库获得正确的Dao实现。 The code to save and find entities from the database doesn't need to know anything changed. 从数据库保存和查找实体的代码不需要知道任何更改。

That said in the wild this doesn't usually go this smoothly for decent size apps. 话虽这么说,但对于体面大小的应用程序而言,这通常并不顺利。 There are a lot of ways to sneak dependencies on a particular technology into an app. 有很多方法可以将对特定技术的依赖偷偷带入应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM