chx 学习jForum笔记四
2010.12.6 接上午。在net.jforum.repository/ForumRepository.java中
??? public synchronized static void start(final ForumDAO forumDAO, final CategoryDAO categoryDAO, final ConfigDAO configModel)
??? {
??? ??? instance = new ForumRepository();
??? ???
??? ??? if (cache.get(FQN, LOADED) == null) {? //未读入缓存
??? ??? ??? instance.loadCategories(categoryDAO); //取分类
??? ??? ??? instance.loadForums(forumDAO);?????? //取板块
??? ??? ??? instance.loadMostUsersEverOnline(configModel); //取最高在线人数
??? ??? ??? instance.loadUsersInfo();? //取用户信息
??? ??? ???
??? ??? ??? final Integer totalMessages = (Integer)cache.get(FQN, TOTAL_MESSAGES); //取缓存信息数
??? ??? ???
??? ??? ??? if (totalMessages == null) {
??? ??? ??? ??? cache.add(FQN, TOTAL_MESSAGES, Integer.valueOf(0)); //设置缓存信息数为零
??? ??? ??? }
??? ??? ???
??? ??? ??? cache.add(FQN, LOADED, "1"); //已读入缓存标记
??? ??? }
??? }
?
还剩下loadUserInfo()没分析了。
?
??? private void loadUsersInfo() //取用户信息
??? {?? //直接从数据表读取并写入缓存
??? ??? UserDAO udao = DataAccessDriver.getInstance().newUserDAO();
??? ??? cache.add(FQN, LAST_USER, udao.getLastUserInfo());
??? ??? cache.add(FQN, TOTAL_USERS, Integer.valueOf(udao.getTotalUsers()));
??? }
?
在net.jforum.dao.generic/GenericUserDAO.java中
?
??? public User getLastUserInfo()
??? {? //取最后注册的用户姓名与ID
??? ??? PreparedStatement p = null;
??? ??? ResultSet rs = null;
??? ??? try {
??? ??? ??? User user = new User();
??? ??? ??? p = JForumExecutionContext.getConnection().prepareStatement(
??? ??? ??? ??? ??? SystemGlobals.getSql("UserModel.lastUserRegistered")); //取SQL语句
??? ??? ??? rs = p.executeQuery();
??? ??? ??? rs.next();
??? ??? ??? user.setUsername(rs.getString("username"));? //用户名
??? ??? ??? user.setId(rs.getInt("user_id"));? //用户ID
??? ??? ??? return user;
??? ??? }
??? ??? catch (SQLException e) {
??? ??? ??? throw new DatabaseException(e);
??? ??? }
??? ??? finally {
??? ??? ??? DbUtils.close(rs, p);
??? ??? }
??? }
?
??? public int getTotalUsers()
??? {? //取总注册人数
??? ??? PreparedStatement preparedStatement = null;
??? ??? try {
??? ??? ??? preparedStatement = JForumExecutionContext.getConnection().prepareStatement(
??? ??? ??? ??? ??? SystemGlobals.getSql("UserModel.totalUsers"));? //取SQL语句
??? ??? ??? return this.getTotalUsersCommon(preparedStatement);//防异常处理
??? ??? }
??? ??? catch (SQLException e) {
??? ??? ??? throw new DatabaseException(e);
??? ??? }
??? ??? finally {
??? ??? ??? DbUtils.close(preparedStatement);
??? ??? }
??? }
?
??? protected int getTotalUsersCommon(PreparedStatement p) throws SQLException//防异常处理
??? {
??? ??? ResultSet rs = p.executeQuery();
??? ??? int total = 0;
??? ??? if (rs.next()) {
??? ??? ??? total = rs.getInt(1);
??? ??? }
??? ??? rs.close();
??? ??? p.close();
??? ??? return total;
??? }
?
小结:
根据ForumRepository的start,其动作为:取所有分类,取当前用户有权处理的所有板块,取最高在线人数,取最后注册用户信息及注册总人数,取缓存信息数。
?
取所有分类的语句在:“CategoryModel.selectAll”
取所有板块的语句在:“ForumModel.selectAll”
取最后注册的用户姓名与ID语句在:"UserModel.lastUserRegistered"
取总注册人数语句在:"UserModel.totalUsers")