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代码 publicabstractclassDataAccessDriver { privatestaticDataAccessDriverdriver; protectedDataAccessDriver(){} /** *Startstheengine. *ThismethodshouldbecalleDWhenthesystem *isstarting. * *@paramimplementationThedao.driverimplementation */ publicstaticvoIDinit(DataAccessDriverimplementation) { driver=implementation; } /** *Getsadriverimplementationinstance. *YouMUSTusethismethoDWhenyouwantainstance *ofavalID<code>DataAccessDriver</code>.Neveraccess *thedriverimplementationdirectly. * *@return<code>DataAccessDriver</code>instance */ publicstaticDataAccessDrivergetInstance() { returndriver; } publicabstractForumDAOnewForumDAO(); publicabstractGroupDAOnewGroupDAO(); publicabstractPostDAOnewPostDAO(); publicabstractPollDAOnewPollDAO(); publicabstractRankingDAOnewRankingDAO(); publicabstracttopicdaOnewtopicdaO(); publicabstractUserDAOnewUserDAO(); publicabstractcategoryDAOnewcategoryDAO(); publicabstractTreeGroupDAOnewTreeGroupDAO(); publicabstractSmilIEDAOnewSmilIEDAO(); publicabstractGroupSecurityDAOnewGroupSecurityDAO(); publicabstractPrivateMessageDAOnewPrivateMessageDAO(); publicabstractUserSessionDAOnewUserSessionDAO(); publicabstractConfigDAOnewConfigDAO(); publicabstractKarmaDAOnewKarmaDAO(); publicabstractBookmarkDAOnewBookmarkDAO(); publicabstractAttachmentDAOnewAttachmentDAO(); publicabstractModerationDAOnewModerationDAO(); publicabstractBannerDAOnewBannerDAO(); publicabstractSummaryDAOnewSummaryDAO(); publicabstractMailintegrationDAOnewMailintegrationDAO(); publicabstractAPIDAOnewAPIDAO(); publicabstractBanListDAOnewBanListDAO(); publicabstractModerationLogDAOnewModerationLogDAO(); publicabstractLuceneDAOnewLuceneDAO(); }public abstract class DataAccessDriver { private static DataAccessDriver driver; protected DataAccessDriver() {} /** * Starts the engine. * This method should be called when the system * is starting. * * @param implementation The dao.driver implementation */ public static voID init(DataAccessDriver implementation) { driver = implementation; } /** * Gets a driver implementation instance. * You MUST use this method when you want a instance * of a valID <code>DataAccessDriver</code>. Never access * the driver implementation directly. * * @return <code>DataAccessDriver</code> instance */ public static DataAccessDriver getInstance() { return driver; } public abstract ForumDAO newForumDAO(); public abstract GroupDAO newGroupDAO(); public abstract PostDAO newPostDAO(); public abstract PollDAO newPollDAO(); public abstract RankingDAO newRankingDAO(); public abstract topicdaO newtopicdaO(); public abstract UserDAO newUserDAO(); public abstract categoryDAO newcategoryDAO(); public abstract TreeGroupDAO newTreeGroupDAO(); public abstract SmilIEDAO newSmilIEDAO(); public abstract GroupSecurityDAO newGroupSecurityDAO(); public abstract PrivateMessageDAO newPrivateMessageDAO(); public abstract UserSessionDAO newUserSessionDAO(); public abstract ConfigDAO newConfigDAO(); public abstract KarmaDAO newKarmaDAO(); public abstract BookmarkDAO newBookmarkDAO(); public abstract AttachmentDAO newAttachmentDAO(); public abstract ModerationDAO newModerationDAO(); public abstract BannerDAO newBannerDAO(); public abstract SummaryDAO newSummaryDAO(); public abstract MailintegrationDAO newMailintegrationDAO(); public abstract APIDAO newAPIDAO(); public abstract BanListDAO newBanListDAO(); public abstract ModerationLogDAO newModerationLogDAO(); public abstract LuceneDAO newLuceneDAO();}
init()方法仅在系统启动时被调用一次。JForum系统中有多处使用此类init(),有一点反向注入的味道。
系统启动时,初始化DataAccessDriver:
Java代码 publicstaticvoIDloadDaoImplementation() { //Startthedao.driverimplementation Stringdriver=SystemGlobals.getValue(ConfigKeys.DAO_DRIVER); logger.info("LoadingJDBCdriver"+driver); try{ Classc=Class.forname(driver); DataAccessDriverd=(DataAccessDriver)c.newInstance(); DataAccessDriver.init(d); } catch(Exceptione){ thrownewForumException(e); } }public static voID loadDaoImplementation() { // Start the dao.driver implementation String driver = SystemGlobals.getValue(ConfigKeys.DAO_DRIVER); logger.info("Loading JDBC driver " + driver); try { Class c = Class.forname(driver); DataAccessDriver d = (DataAccessDriver)c.newInstance(); DataAccessDriver.init(d); } catch (Exception e) { throw new ForumException(e); } }
如果是MysqL,String driver值为:net.jforum.dao.MysqL.MysqLDataAccessDriver
总结以上是内存溢出为你收集整理的JForum数据持久化全部内容,希望文章能够帮你解决JForum数据持久化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)