对天乙社区bbscs8实现的详细分析八

此文为转载:http://www.diybl.com/course/1_web/webjs/2007113/82989.html


接下来是FriendFactory,是有个方法public Friend getInstance(String userId);用于实例化Friend对象用!进入Friend Bean:(注意其实现了可序列化接口implements Serializable与BookMark一样,其实后面的Note,Subscibe都一样,bean包下还有Friend0~9个与Friend内容一样的BEAN,它们主要是为了拆表准备的,普通版本用不到)
private String id;
private String userID;
private String userName;
private String friendID;
private String friendName;
private String friendComment;//介绍
private int isBlack;//是否加入黑名单
我们看其Friend.hbm.xml:

 
 

  
  
    
   
   
      
    
    
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   //length=2000?--->对应表中的`FriendComment` text, //说明
    
   
   //`IsBlack` tinyint(1) default ?'

  
  

 
 
看服务层的方法:
public Friend saveFriend(Friend f) throws BbscsException;
public Friend findFriendByID(String id, String ownId);
public Friend findFriendByName(String fname, String ownId);//根据朋友名、自己的ID取得
public long getFriendNum(String ownId, int isBlack);
public List findFriends(String ownId, int isBlack);
public void removeFriend(Friend f) throws BbscsException;
public void removeFriend(String id, String ownId) throws BbscsException;
public void friendIDsToFile(String ownId);//好友列表ID保存至文件
public List fileToFriendIDs(String ownId);
public List findFriendIds(String ownId, int isBlack);
进入service.imp层,首先是
public class FriendFactoryImp
    implements FriendFactory {
public FriendFactoryImp() {
}
   public synchronized Friend getInstance(String userId) {
    return new Friend();//同步方法!!实例化Friend对象
}
}
而另外的FriendsFactoryImp实现,需要注意的是它也实现了FriendFactory接口:
public synchronized Friend getInstance(String userId) {
    try {
      return (Friend) Class.forName(BBSCSUtil.getClassName("Friend", userId)).newInstance();
    }
/**
public static String getClassName(String className, String userID) {
int num = Math.abs(userID.hashCode());
className = Constant.BEANPERFIX + className + (num % 10);
return className; //Friend0~~~~9
}
public static String getClassName(String className, String userID, int modnum) {
int num = Math.abs(userID.hashCode());
className = Constant.BEANPERFIX + className + (num % modnum);
return className;
}//modnum由setModnum具体得到
*/
    catch (ClassNotFoundException ex) {
      logger.error(ex);
      return null;
    }
    catch (IllegalAccessException ex) {
      logger.error(ex);
      return null;
    }
    catch (InstantiationException ex) {
      logger.error(ex);
      return null;
    }
}
我们看其主要的实现方法:
/**
* 好友列表ID保存至文件
*/
public void friendIDsToFile(String ownId) {
    List l = this.getFriendDAO().findFriends(ownId, 0);
    StringBuffer sb = new StringBuffer();
    Friend f;
    for (int i = 0; i < l.size(); i++) {
      f = (Friend) l.get(i);
      sb.append(f.getFriendID());
      sb.append(",");
    }
    File toFile = new File(this.getUserConfig().getUserFilePath(ownId) + Constant.USER_FRIEND_FILE);
//public static final String USER_FRIEND_FILE = "UserFriendFile.txt";
    try {
FileUtils.writeStringToFile(toFile, sb.toString(), Constant.CHARSET);
} catch (IOException e) {
logger.error(e);
}
}
   
public List fileToFriendIDs(String ownId) {
    List
 
 
  
   l = new ArrayList
  
  
   
   ();
    File fromFile = new File(this.getUserConfig().getUserFilePath(ownId) + Constant.USER_FRIEND_FILE);
    try {
String fids = FileUtils.readFileToString(fromFile, Constant.CHARSET);
String[] ids = fids.split(",");//分割出来!
if (ids != null) {
    for (int i = 0; i < ids.length; i++) {
      //System.out.println(ids[i]);
      l.add(ids[i]);
    }
}
} catch (IOException e) {
logger.error(e);
}
    return l;
}

@SuppressWarnings("unchecked")
public List findFriendIds(String ownId, int isBlack) {
   List l = this.getFriendDAO().findFriendIds(ownId, isBlack);
   if (l.isEmpty()) {   //填充List
    l.add("0");
   }
   return l;
}

Friend DAO接口中提供如下方法:
public Friend saveFriend(Friend f);
public Friend findFriendByID(String id, String ownId);
public Friend findFriendByName(String fname, String ownId);
public long getFriendNum(String ownId, int isBlack);
public List findFriends(String ownId, int isBlack);
public void removeFriend(Friend f);
public void removeFriend(String id, String ownId);
public List findFriendIds(String ownId, int isBlack);
对于DAO的实现FriendHibernateDAO省略.由于上次忘记分析BookMarksHibernateDAO(拆表用的),这里我们详细分析一下FriendsHibernateDAO:(有一个属性private int modNum,而这个服务类其实也不用了)
public Friend findFriendByID(String id, String ownId) {
StringBuffer sb = new StringBuffer();
sb.append("from Friend");
sb.append(BBSCSUtil.getTableID(ownId, this.getModNum()));
/**
public static int getTableID(String userID, int num) {
int absNum = Math.abs(userID.hashCode());
return (int) (absNum % num); //0~~~9
}
public static int getTableID(long bid, int num) {
return (int) (bid % num);
}

*/
sb.append(" where id = ? and userID = ?");
Object[] o = { id, ownId };
List l = this.getHibernateTemplate().find(sb.toString(), o);
if (l == null || l.isEmpty()) {
   return null;
} else {
   return (Friend) l.get(0);
}

}
其它方法省略之.我们来看看Friend0.java:
public class Friend0 extends Friend implements Serializable {
private static final long serialVersionUID = 8915456842365368615L;
public Friend0() {
super();
}
}

   
   

    
    
     
     //现在没这个表了!5555
    
     
     
      
      
      
    
     
     
    
     
     
    
     
     
    
     
     
    
     
     
    
     
     
    
     
     

    
    

   
   

接下来是LoginErrorService:
public LoginError saveLoginError(LoginError loginError) throws BbscsException;
public LoginError findLoginErrorByID(String id); 
public LoginError findLoginErrorByUserID(String userID);
public List findLoginErrorsOutTime(long atime);//取得超过指定时间的LoginError列表
public void removeLoginErrorsOutTime(long atime) throws BbscsException;//删除超过指定时间的LoginError对象
public LoginError createLoginError(String userID) throws BbscsException;//创建或取得一个已有的LoginError对象
public boolean isCanNotLogin(String userID);//15分钟之内登录错误次数超过5次,不能登录
对于bean:
private String id;
private String userID;
private int errorTimes;//登录时间
private long loginTime;//错误次数
其hbm.xml文件中:

   
   

    
    
    
     
     
      
      
      
    
     
     
    
     
     
    
     
     
    
     
     //long型的时间

    
    

   
   
对于服务的实现层,主要还是由DAO去处理的,除了以下两个方法:
//创建或取得一个已有的LoginError对象
public LoginError createLoginError(String userID) throws BbscsException {
    LoginError loginError = this.getLoginErrorDAO().findLoginErrorByUserID(userID);
    if (loginError == null) {
      loginError = new LoginError();
      loginError.setErrorTimes(1);
      loginError.setLoginTime(System.currentTimeMillis());
      loginError.setUserID(userID);
    }
    else {
      loginError.setErrorTimes(loginError.getErrorTimes() + 1);//加1
      loginError.setLoginTime(System.currentTimeMillis());
    }
    try {
      return this.getLoginErrorDAO().saveLoginError(loginError);//保存
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
}

/**
   * 15分钟之内登录错误次数超过5次,不能登录,也就是15分钟后进行再进行登录不会执行本方法为true值,不过,再次登录错误的话,重新计算机loginError的LoginTime和ErrorTimes值!
      */
public boolean isCanNotLogin(String userID) {
    LoginError loginError = this.getLoginErrorDAO().findLoginErrorByUserID(userID);
    if (loginError != null) {
      if ( (System.currentTimeMillis() - loginError.getLoginTime()) <= 15 * 60000) {
        if (loginError.getErrorTimes() >= 5) {
          return true;
        }
      }
    }
    return false;
}
而DAO提供如下方法:(应该是根据serive imp中的需要而定吧!个人意见)
public LoginError saveLoginError(LoginError loginError);
public LoginError findLoginErrorByID(String id);
public LoginError findLoginErrorByUserID(String userID);
public List findLoginErrorsOutTime(long atime);
public void removeLoginErrorsOutTime(long atime);
public void removeLoginError(LoginError loginError);
DAO实现省略(下同)

下个是NoteService:(先看bean和hbm.xml)
        private String id;
private String fromID;
private String fromUserName;
private String fromNickName;
private String toID;
private String toUserName;
private String toNickName;
private int noteType;//留言类型
private String noteContext;
private Date createTime;//`CreateTime` datetime NOT NULL default ??-00-00 00:00:00', 创建时间
private int isNew;//`IsNew` tinyint(1) default ?', 已读标志
private int needRe; //需要回执
private String noteTitle;
        private int isRe;//回复标志
private int sysMsg; //系统留言标志
注意它用实现了Serializable接品,在bean包中也有许多Note0...Note9及Note0.hbm.xml...Note9.hbm.xml ---> 
   
   
    
    
在service层也有NoteFactory,在serivce imp层NoteFactoryImp和NotesFactoryImp两种不同的实例化Note对象的同步方法,在DAO实现层也有NotesHibernateDAO用于对相应table(如:bbscs_note_9)的操作,这些与Friend类似,而Subscibe类也差不多如此,因此省略之..
OK,我们继续看Note.hbm.xml:

    
    

     
     
    
      
       
        
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      //直接text类型,OK!
    
      
      //timstamp类型
    
      
      
    
      
      
    
      
      
    
      
      

     
     

    
    
我们继续看NoteService中的公开方法:
public Note saveNote(Note note) throws BbscsException;
public Note[] createNote(Note inbodNote, Note outboxNote) throws BbscsException;//创建纸条
public Note findNoteByIDFromID(String id, String fromID);
public Note findNoteByIDToID(String id, String toID);
public long getNoteAllNumOutBox(String fromID);//取得发件箱纸条数量
public PageList findNotesOutBox(final String fromId, Pages pages);
public long getNoteAllNumInBox(String toID);//取得收件箱纸条数量
public PageList findNotesInBox(final String toID, Pages pages);
public long getNoteNumInBoxByIsNew(String toID, int isNew);
public List findNotesInIDsOutBox(final String fromId, final List values);//取得发件箱中指定ID的Note列表
public List findNotesInIDsOutBox(final String fromId, final List values);
public List findNotesInIDsInBox(final String toId, final List values);
public void removeNote(Note note) throws BbscsException;
public void removeAllOutBox(String fromID) throws BbscsException;
public void removeAllInBox(String toID) throws BbscsException;
public void removeByIDFromID(String id, String fromID) throws BbscsException;//根据ID和发送者ID删除Note对象
public void removeByIDToID(String id, String toID) throws BbscsException;
public void removeInIDsFromID(List values, String fromID) throws BbscsException;
public void removeInIDsToID(List values, String toID) throws BbscsException;
我们看实现层,createNote创建纸条,返回一个Note数组:
public Note[] createNote(Note inbodNote, Note outboxNote) throws BbscsException {
    try {
      inbodNote = this.getNoteDAO().saveNote(inbodNote);
      outboxNote = this.getNoteDAO().saveNote(outboxNote);
      Note[] note = {inbodNote, outboxNote};
      return note;
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
}
由于大部分方法直接给DAO层去完成,我们直接看DAO实现层:(NoteHibernateDAO)
首先是查询HQL定义
private static final String LOAD_BY_ID_FROMID = "from Note where id = ? and fromID = ?";
private static final String LOAD_BY_ID_TOID = "from Note where id = ? and toID = ?";
private static final String GET_ALL_NUM_OUTBOX =
     "select count(*) from Note where fromID = ? and noteType = ?";
private static final String LOADS_OUTBOX =
      "from Note where fromID = ? and noteType = ? order by createTime desc";
private static final String GET_ALL_NUM_INBOX =
      "select count(*) from Note where toID = ? and noteType = ?";
private static final String LOADS_INBOX =
      "from Note where toID = ? and noteType = ? order by createTime desc";
private static final String GET_NUM_INBOX_BY_ISNEW =
      "select count(*) from Note where toID = ? and noteType = ? and isNew = ?";
private static final String LODAS_INIDS_OUTBOX =
      "from Note where id in (:ids) and fromID = :fromId";
private static final String LODAS_INIDS_INBOX = "from Note where id in (:ids) and toID = :toId";
private static final String REMOVE_ALL_OUTBOX =
      "delete from Note where fromID = ? and noteType = ?";
private static final String REMOVE_ALL_INBOX = "delete from Note where toID = ? and noteType = ?";
private static final String REMOVE_BY_ID_FROMID = "delete from Note where id = ? and fromID = ?";
private static final String REMOVE_BY_ID_TOID = "delete from Note where id = ? and toID = ?";
private static final String REMOVE_INIDS_FROMID =
      "delete from Note where id in (:ids) and fromID = :fromId";
private static final String REMOVE_INIDS_TOID =
      "delete from Note where id in (:ids) and toID = :toId";
下面是取得发件箱纸条数量的方法:
public long getNoteAllNumOutBox(String fromID) {
    Object[] o = {fromID, new Integer(0)}; //noteType=0
    List l = this.getHibernateTemplate().find(GET_ALL_NUM_OUTBOX, o);
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Long) l.get(0)).longValue();
    }
}
取得发件箱Note分页列表:
public List findNotesOutBox(final String fromId, final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        Query query = s.createQuery(LOADS_OUTBOX);
        query.setString(0, fromId);
        query.setInteger(1, 0);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);

        List list = query.list();
        return list;
      }
    });
}

我们看PermissionService,先进入BEAN(权限信息),它有五个属性:
private Long id;// default ?'
private String permissionName; //权限名称 default ''
private String resource;//权限资源(URI)default NULL
private String action;//动作,Action default NULL
private int typeID;//权限类型,default `0`
这里我发现在这个系统的数据库表设计中它尽量的提供了default值!!!(不过也可能是导出时工具生成的)

    
    

     
     
    
      
       
        //自动增加键! 
      
    
      
      
    
      
      
    
      
      
    
      
      

     
     

    
    
这次我们来看看数据库表Permission,发现它是一个基本固定不变的定义表,注意: 
权限类型有4种,普通权限(0) 特殊权限(1) 版区普通权限(2) 版区特殊权限(3),这里都为0 
122 XXXX(PermissionName)     /amdinUOTimeSet *                       0
209 XXXX(由于我本地乱码)     /userFace        index,uppage,up,delme   0
OK!看方法:
public Permission savePermission(Permission permission) throws BbscsException;
public Permission updatePermission(Permission permission) throws BbscsException;
public Permission findPermissionByID(long id);
public List findPermissionsAll();
public List findPermissionsByTypeID(int typeID);
public List findPermissionnIDs(List ids);
PermissionServiceImp实现之,首先引入了userPermissionCache,以便在保存和更新后清理缓存数据!
我们直接进入PermissionHibernateDAO:
private static final String LOADS_ALL = "from Permission order by id";
private static final String LOADS_BY_TYPEID = "from Permission where typeID = ? order by id";
private static final String LOADS_IN_IDS = "from Permission where id in (:ids)";
我们只看一个方法:
public List findPermissionnIDs(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;
      }
    });
}

   
   
  
  
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
计算八字神煞需要用到农历和天干地支的知识,可以使用 PHP 的日期和时间函数来实现。 步骤如下: 1. 获取用户输入的阳历生日,使用 PHP 的 date 函数将其转换为农历生日。 2. 根据农历生日,计算出生年、生月、生日的天干地支。 3. 根据天干地支,查找对应的八字神煞。 以下是一个简单的 PHP 代码示例: ```php // 获取用户输入的生日 $birthday = '1990-01-01'; // 将阳历生日转换为农历生日 $chineseBirthday = getChineseBirthday($birthday); // 计算出生年、月、日的天干地支 list($yearTianGan, $yearDiZhi) = getTianGanDiZhi($chineseBirthday['year']); list($monthTianGan, $monthDiZhi) = getTianGanDiZhi($chineseBirthday['month']); list($dayTianGan, $dayDiZhi) = getTianGanDiZhi($chineseBirthday['day']); // 查找八字神煞 $shenSha = getShenSha($yearTianGan, $yearDiZhi, $monthTianGan, $monthDiZhi, $dayTianGan, $dayDiZhi); // 输出结果 echo '您的八字神煞为:' . implode(',', $shenSha); // 获取农历生日 function getChineseBirthday($birthday) { // 使用 PHP 的 DateTime 类将阳历生日转换为农历生日 $dateTime = new DateTime($birthday); $chineseCalendar = new ChineseCalendar($dateTime); $chineseBirthday = [ 'year' => $chineseCalendar->getChineseYear(), 'month' => $chineseCalendar->getChineseMonth(), 'day' => $chineseCalendar->getChineseDay(), ]; return $chineseBirthday; } // 计算天干地支 function getTianGanDiZhi($chineseValue) { // 天干 $tianGan = ['甲', '', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸']; // 地支 $diZhi = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']; // 计算天干地支 $index = ($chineseValue - 4) % 60; $tianGanIndex = $index % 10; $diZhiIndex = $index % 12; $tianGanValue = $tianGan[$tianGanIndex]; $diZhiValue = $diZhi[$diZhiIndex]; return [$tianGanValue, $diZhiValue]; } // 查找八字神煞 function getShenSha($yearTianGan, $yearDiZhi, $monthTianGan, $monthDiZhi, $dayTianGan, $dayDiZhi) { // 八字神煞表 $shenShaTable = [ '甲子' => ['天', '文昌'], '甲戌' => ['天厨', '文曲'], '丑' => ['吊客', '天哭'], '酉' => ['陀罗', '天虚'], '丙寅' => ['将星', '天月'], '丙申' => ['天巫', '天德'], '丁卯' => ['天才', '天福'], '丁酉' => ['天寿', '天恩'], '戊辰' => ['天贵', '天使'], '戊戌' => ['天荫', '天罡'], '己巳' => ['天福', '天官'], '己亥' => ['天伤', '天蓬'], '庚午' => ['天空', '天任'], '庚子' => ['天后', '天伯'], '辛未' => ['天印', '天威'], '辛酉' => ['天权', '天禄'], '壬申' => ['天德', '天'], '壬子' => ['天才', '天英'], '癸未' => ['天寿', '天巫'], '癸酉' => ['天恩', '天贵'], ]; // 查找八字神煞 $shenSha = []; $key = $yearTianGan . $yearDiZhi; if (isset($shenShaTable[$key])) { $shenSha = array_merge($shenSha, $shenShaTable[$key]); } $key = $monthTianGan . $monthDiZhi; if (isset($shenShaTable[$key])) { $shenSha = array_merge($shenSha, $shenShaTable[$key]); } $key = $dayTianGan . $dayDiZhi; if (isset($shenShaTable[$key])) { $shenSha = array_merge($shenSha, $shenShaTable[$key]); } return $shenSha; } ``` 需要注意的是,以上代码示例中使用了第三方库 `ChineseCalendar` 来实现阳历和农历的转换,使用前需要先安装该库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值