JForum数据持久化

前端之家收集整理的这篇文章主要介绍了JForum数据持久化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JForum“Support for several popular databases,including MysqL,Postgresql,Oracle and HsqlDB”。

JForum系统中用于持久化的实体类被放在net.jforum.entities包下,例如:User、UserSession、Post等。关于实体类的DAO操作被定义在多个接口中,例如:关于User的DAO操作被定义在net.jforum.dao.UserDAO接口中。针对不同的数据库,有不同的UserDAO实现类。

如上图,GenericUserDAO是JForum的默认实现,对应的数据库MysqL。GenericUserDAO、HsqldbUserDAO、OracleUserDAO和sqlServerUserDAO类都有public的无参数构造函数。JForum不是根据“配置文件 + if else”的方式决定初始化哪个UserDAO,而是新建了一个抽象类DataAccessDriver。

Java代码
  1. publicabstractclassDataAccessDriver
  2. {
  3. privatestaticDataAccessDriverdriver;
  4. protectedDataAccessDriver(){}
  5. /**
  6. *Startstheengine.
  7. *Thismethodshouldbecalledwhenthesystem
  8. *isstarting.
  9. *
  10. *@paramimplementationThedao.driverimplementation
  11. */
  12. publicstaticvoidinit(DataAccessDriverimplementation)
  13. {
  14. driver=implementation;
  15. }
  16. /**
  17. *Getsadriverimplementationinstance.
  18. *YouMUSTusethismethodwhenyouwantainstance
  19. *ofavalid<code>DataAccessDriver</code>.Neveraccess
  20. *thedriverimplementationdirectly.
  21. *
  22. *@return<code>DataAccessDriver</code>instance
  23. */
  24. publicstaticDataAccessDrivergetInstance()
  25. {
  26. returndriver;
  27. }
  28. publicabstractForumDAOnewForumDAO();
  29. publicabstractGroupDAOnewGroupDAO();
  30. publicabstractPostDAOnewPostDAO();
  31. publicabstractPollDAOnewPollDAO();
  32. publicabstractRankingDAOnewRankingDAO();
  33. publicabstractTopicDAOnewTopicDAO();
  34. publicabstractUserDAOnewUserDAO();
  35. publicabstractCategoryDAOnewCategoryDAO();
  36. publicabstractTreeGroupDAOnewTreeGroupDAO();
  37. publicabstractSmilieDAOnewSmilieDAO();
  38. publicabstractGroupSecurityDAOnewGroupSecurityDAO();
  39. publicabstractPrivateMessageDAOnewPrivateMessageDAO();
  40. publicabstractUserSessionDAOnewUserSessionDAO();
  41. publicabstractConfigDAOnewConfigDAO();
  42. publicabstractKarmaDAOnewKarmaDAO();
  43. publicabstractBookmarkDAOnewBookmarkDAO();
  44. publicabstractAttachmentDAOnewAttachmentDAO();
  45. publicabstractModerationDAOnewModerationDAO();
  46. publicabstractBannerDAOnewBannerDAO();
  47. publicabstractSummaryDAOnewSummaryDAO();
  48. publicabstractMailIntegrationDAOnewMailIntegrationDAO();
  49. publicabstractApiDAOnewApiDAO();
  50. publicabstractBanlistDAOnewBanlistDAO();
  51. publicabstractModerationLogDAOnewModerationLogDAO();
  52. publicabstractLuceneDAOnewLuceneDAO();
  53. }

init()方法仅在系统启动时被调用一次。JForum系统中有多处使用此类init(),有一点反向注入的味道。

系统启动时,初始化DataAccessDriver:

Java代码
  1. publicstaticvoidloadDaoImplementation()
  2. {
  3. //Startthedao.driverimplementation
  4. Stringdriver=SystemGlobals.getValue(ConfigKeys.DAO_DRIVER);
  5. logger.info("LoadingJDBCdriver"+driver);
  6. try{
  7. Classc=Class.forName(driver);
  8. DataAccessDriverd=(DataAccessDriver)c.newInstance();
  9. DataAccessDriver.init(d);
  10. }
  11. catch(Exceptione){
  12. thrownewForumException(e);
  13. }
  14. }

如果是MysqL,String driver值为:net.jforum.dao.MysqL.MysqLDataAccessDriver

原文链接:https://www.f2er.com/postgresql/196437.html

猜你在找的Postgre SQL相关文章