⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 详细分析二.txt

📁 开源论坛实现
💻 TXT
📖 第 1 页 / 共 5 页
字号:
    </id>
    <property column="RoleName" length="255" name="roleName" not-null="true" type="string"/>
    <property column="TypeID" length="2" name="typeID" not-null="true" type="int"/>
    <property column="Permissions" name="permissions" type="com.laoer.bbscs.ext.hibernate.SplitList"/>//用了自定义类型!
  </class>
</hibernate-mapping>
我们看下数据库bbscs_role中的记录,可见它也基本上是一本固定的配置表而已:
1  ???(乱码)     1      MEMO(401,402,403,404,405,406,407,408,409,410,411,412,413,.....)
4  ???(乱码)     0      MEMO(126,127,128,129,130)
我们看方法:
public Role saveRole(Role role) throws BbscsException;
public Role findRoleByID(int id);
public List findRolesAll();
public List findRolesByTypeID(int typeID);//根据tpyeID取得Role列表
public List findRolesInIDs(List ids);//根据指定IDs取得Role对象列表
public void removeRole(Role role) throws BbscsException;
进入其实现层,首先也用了userPermissionCache,大部分方法仍有DAO处理:
  public Role saveRole(Role role) throws BbscsException {
    try {
      role = this.getRoleDAO().saveRole(role);
      this.clearPermissionCache();
      return role;
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
  }
  private void clearPermissionCache() {   //私有方法
    if (Constant.USE_PERMISSION_CACHE) {//public static final boolean USE_PERMISSION_CACHE = true;
      this.getUserPermissionCache().removeAll();
    }
  }
对于DAO及其实现层我们不分析下去.

好,我们到了SessionService,这个接口中并没有import任何service,bean(当然也没有.hbm.xml),应该是完全用于session的操作的吧.我们直接看方法:
	public void saveSession(String id, String key, Object value);
	public void saveSession(String id, Map session);
	public Map getSession(String id);
	public Object getSession(String id, String key);
	public void removeSession(String id);
我们在com.laoer.bbscs.serivce.config包中也发现了还有SessionConfig没有介绍之,其实它也是个用于得到目录的配置服务类,先注入safePath后:
	public String getSessionPath(String id) {
		StringBuffer sb = new StringBuffer();
		sb.append(this.getSafePath());
		if (!this.getSafePath().endsWith("/")) {
			sb.append("/");
		}
		sb.append("session/");
		sb.append(BBSCSUtil.getStringHashCode(id) % 100);
		sb.append("/");
		String tmp = "";
		if (id.length() > 5) {
			tmp = id.substring(0, 5);
		} else {
			tmp = id;
		}
		sb.append(BBSCSUtil.getStringHashCode(tmp) % 100);
		sb.append("/");
		sb.append(id);
		sb.append("/");
		File ft = new File(sb.toString());
		if (!ft.exists()) {
			ft.mkdirs();
		}
		return sb.toString(); //safe/session/34/23/2342342/
	}
其中用到了com.laoer.bbscs.comm.*:
public static int getStringHashCode(String txt) {
		int t = 0;
		if (txt != null) {
			char[] chars = txt.toCharArray();
			for (int i = 0; i < chars.length; i++) {
				t = t + (int) chars[i];
			}
		}
		return t;
	}
至于SessionService和SessionConfig的具体用处可能是在web层使用吧!有待考虑之.

我们来到SubscibeService(帖子订阅服务):(我们不分析关于拆表的任何代码,有关请看Friend相关类)	
        private String id;
	private String userID; //用户ID
	private String userName;
        private String nickName;
	private String postID;//帽子ID
	private String postTitle;
	private long boardID;//版区ID
	private int emailinform;//Email发送标志
	private int msginform;//留言发送标志
	private String userEmail;
	private Date createTime;
	private String userLocale;//用户Locale信息
<hibernate-mapping package="com.laoer.bbscs.bean">
  <class name="Subscibe" table="bbscs_subscibe">
    <id name="id" column="ID" type="string" unsaved-value="null">
      <generator class="uuid"/>  //UUID类型!!
    </id>
    <property column="UserID" length="40" name="userID" not-null="true" type="string"/>
    <property column="UserName" length="20" name="userName" not-null="true" type="string"/>
    <property column="NickName" length="60" name="nickName" not-null="true" type="string"/>
    <property column="PostID" length="60" name="postID" not-null="true" type="string"/>
    <property column="PostTitle" length="150" name="postTitle" type="string"/>
    <property column="BoardID" length="13" name="boardID" not-null="true" type="long"/>
    <property column="Emailinform" length="1" name="emailinform" type="int"/>
    <property column="Msginform" length="1" name="msginform" type="int"/>
    <property column="UserEmail" length="255" name="userEmail" type="string"/>
    <property column="CreateTime" name="createTime" not-null="true" type="timestamp"/>//时间截
    <property column="UserLocale" length="20" name="userLocale" type="string"/>
  </class>
</hibernate-mapping>
好了,我们看SubscibeService中的服务公开方法:
public Subscibe saveSubscibe(Subscibe subscibe) throws BbscsException;
public Subscibe findSubscibeByID(String id, String userID, long bid);
public Subscibe findSubscibeByPostID(String postID, String userID, long bid);
public List findSubscibesSend(String postID, long bid);
public long getSubscibeNumByUserID(String userID, long bid);
public PageList findSubscibesByUserID(String userID, long bid, Pages pages);
public void removeSubscibe(String id, String userID, long bid) throws BbscsException;
public void removeSubscibe(Subscibe subscibe) throws BbscsException;
而其实现基本由DAO完成!
  private static final String LOAD_BY_ID = "from Subscibe where id = ? and userID = ?";
  private static final String LOAD_BY_POSTID = "from Subscibe where postID = ? and userID = ?";
  private static final String LOADS_SEND = "from Subscibe where postID = ?";
  private static final String GET_NUM_BY_USER =
      "select count(*) from Subscibe where userID = ? and boardID = ?";
  private static final String LOADS_BY_USER = "from Subscibe where userID = ? and boardID = ? order by createTime desc";
  private static final String REMOVE_BY_ID = "delete from Subscibe where id = ? and userID = ?";
如下为根据ID查找订阅信息的,其中bid没有用上!
  public Subscibe findSubscibeByID(String id, String userID, long bid) {
    Object[] o = {id, userID};
    List l = this.getHibernateTemplate().find(LOAD_BY_ID, o);
    if (l == null || l.isEmpty()) {
      return null;
    }
    else {
      return (Subscibe) l.get(0);
    }
  }

下面我们继续分析SysNumStatService,先看其Bean:(系统统计)
	private String id;
	private String recDate; //统计时间
	private long num0;//注册人数
	private long numInc0;//注册人数增加数
	private long num1;//主题数
	private long numInc1;//主题增加数
	private long num2;//帖子总数
	private long numInc2;//帖子总数增加数
	private long num3;
	private long numInc3;
	private long num4;
	private long numInc4;
	private long num5;
	private long numInc5;
	private long createTime;
我们看其方法:
public SysNumStat saveSysNumStat(SysNumStat sns) throws BbscsException;
public SysNumStat saveSysNumStat(String recDate, SysNumStat sns) throws BbscsException;
public SysNumStat findSysNumStatByRecDate(String recDate);
public long getSysNumStatNum();
public PageList findSysNumStats(Pages pages);//pages由web层的action填充!
它由DAO代理完成,对于DAO自行分析...

接下来是SysStatService,以前讲过,这里在复习一次吧.它是个抽象类,并没有什么bean支撑(不需要与数据库打交道),直接写在自己的私有成员,还有公开的setter/getter方法中!
  private long onlineNum = 0;
  private long appearTime = 0;
  private String appearTimeStr = "";
  private long allUserNum = 0;
  private String lastRegUser = "";
  private long postMainNum = 0;
  private long postNum = 0;
子类需实现的方法有:
  public abstract void load();
  public abstract void saveOnline(long nowonlinenum);
  public abstract void saveAllUserNum(long allusernum, String lastreguser);
  public abstract void savePostNum(long main, long all);
对于具体的实现请看前面的分析!

我们看UserGroupService.其bean(用户组)如下:
	private Integer id;
	private String groupName;//用户组名称
	private String groupDesc;
	private int typeID;//类型
	private Set roles = new HashSet();
其UserGroup-mysql.hbm.xml:
<hibernate-mapping package="com.laoer.bbscs.bean">
  <class name="UserGroup" table="bbscs_usergroup">
    <id name="id" column="ID" type="int" unsaved-value="null">
      <generator class="identity"/>
    </id>
    <property column="GroupName" length="255" name="groupName" not-null="true" type="string"/>
    <property column="GroupDesc" length="255" name="groupDesc" type="string"/>
    <property column="TypeID" length="2" name="typeID" not-null="true" type="int"/>
    <set name="roles" table="bbscs_grouprole" cascade="save-update" lazy="true" fetch="select">
      <key column="GroupID"/>
      <many-to-many column="RoleID" class="Role"/> //set 多对多关系
    </set>
  </class>
</hibernate-mapping>
让我们看看起初加入数据库的数据(注意到没有roles字段):
  (1,'游客/未注册用户','',0),
  (2,'注册用户','',0),
  (3,'待验证用户','',0),
  (4,'超级版主','',0),
  (5,'系统管理员','',0),
  (6,'封禁用户','',0)
我们接下来看服务类的提供的对外方法:
public UserGroup saveUserGroup(UserGroup ug) throws BbscsException;
public UserGroup updateUserGroup(UserGroup ug) throws BbscsException;
public UserGroup findUserGroupByID(int id);
public List findUserGroupsAll();
public List findUserGroupInIDs(List ids);
public void removeUserGroup(UserGroup ug) throws BbscsException;
在其实现中,首先注入了userGroupDAO,boardDAO,boardPermissionDAO,userPermissionCache,这四个方法我们都用过:
public UserGroup saveUserGroup(UserGroup ug) throws BbscsException {
    try {
      ug = this.getUserGroupDAO().saveUserGroup(ug);
下面是对版区权限的附加操作(若没有这个用户组的话)
      List bplist = this.getBoardPermissionDAO().findBoardPermissionsByGid(ug.getId().intValue()); //取得版区权限列表
      if (bplist.size() == 0) { //没有版区权限列表,说明是新的用户组,增加版区权限记录
        List blist = this.getBoardDAO().findBoardsAll(); //获得所有的版区
        Board b;
        for (int i = 0; i < blist.size(); i++) {
          b = (Board) blist.get(i);
          BoardPermission bp = new BoardPermission();
          bp.setBoardID(b.getId().longValue());
          bp.setGroupID(ug.getId().intValue());
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_1);//自定义权限列表!
          this.getBoardPermissionDAO().saveBoardPermission(bp);
        }
      }
      this.clearPermissionCache();
      return ug;
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }  private void clearPermissionCache() {
    if (Constant.USE_PERMISSION_CACHE) {
      this.getUserPermissionCache().removeAll();
    }
  }
  }
我们看下DAO:
public UserGroup saveUserGroup(UserGroup ug);
public UserGroup updateUserGroup(UserGroup ug);
public UserGroup findUserGroupByID(int id);
public List findUserGroupsAll();
public List findUserGroupInIDs(List ids);
public void removeUserGroup(UserGroup ug);
看其中的一个实现:
private static final String LOADS_IN_IDS = "from UserGroup where id in (:ids)";
	public List findUserGroupInIDs(final List ids) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(LOADS_IN_IDS);
				query.setParameterList("ids", ids);
				List list = query.list();
				return list;
			}
		});
	}

下面是UserLevelService:
  private String levelName; //级别名称
  private String id;//ID
  private int levelValue;//该级别所需相应值
  private int levelType;//级别类型
<hibernate-mapping package="com.laoer.bbscs.bean">
  <class name="UserLevel" table="bbscs_userlevel">
    <id name="id" column="ID" type="string" unsaved-value="null">
      <generator class="uuid"/>
    </id>
    <property column="LevelName" length="60" name="levelName" not-null="true" type="string"/>
    <property column="LevelValue" length="11" name="levelValue" not-null="true" type="int"/>
    <property column="LevelType" length="1" name="levelType" not-null="true" type="int"/>
  </class>
</hibernate-mapping>
其初始数据请看数据库设计表!其下有以下方法:
public UserLevel saveUserLevel(UserLevel ul) throws BbscsException;
public UserLevel findUserLevelById(String id);
public List findUserLevelsByType(int type);
public UserLevel getUserLevelByUserValue(int type, int value);
public void removeUserLevel(UserLevel ul) throws BbscsException;
其实现注入了sysListObjCache:
public UserLevel saveUserLevel(UserLevel ul) throws BbscsException {
		try {
			ul = this.getUserLevelDAO().saveUserLevel(ul);
			this.getSysListObjCache().remove("UserLevel-" + ul.getLevelType());
			return ul;
		} catch (Exception ex) {
			logger.error(ex);
			throw new BbscsException(ex);
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -