@Service("userService")
public class UserServiceImpl implements UserService{@Autowiredprivate UserDao userDao;/*** 添加用戶,一般來說需要檢查用戶為空、用戶名為空、密碼為空*/public void add(User user) throws UserCanNotBeNullException, UserNameCanNotBeNullException, UserPwdCanNotBeNullException, UserAireadyExistException, OtherThingsException {//先檢查用戶是否存在if (null == user) {//拋出用戶為空的自定義異常throw new UserCanNotBeNullException("User can not be Null");}//用戶名不能為空檢查if (StringUtils.isEmpty(user.getLoginId())) {//拋出用戶名為空的自定義異常throw new UserNameCanNotBeNullException("User name can not be Null");}//用戶密碼不能為空檢查if (StringUtils.isEmpty(user.getPwd())) {//拋出用戶密碼為空的自定義異常throw new UserPwdCanNotBeNullException("User name can not be Null");}//由于我這個是管理系統,根據業務需求來說,我們的用戶基本信息都是不能為空的//基本信息包括:姓名、年齡、用戶名、密碼、性別、手機號,年齡大于18if ( StringUtils.isEmpty(user.getSex())|| user.getAge() < 18|| StringUtils.isEmpty(user.getCellNumber())) {//其他綜合異常throw new OtherThingsException("Some use's base info can not be null");}//已經存在相同用戶if (null != userDao.findOneById(user.getLoginId())) {//存在相同的用戶異常throw new UserAireadyExistException("Register User Failed,Because the user Aiready exist");}int result = 0; //受影響的行數默認為0try {result = userDao.add(user);} catch (Exception e) {System.out.println("添加用戶失敗,用戶已經存在");//其他用戶添加失敗異常throw new OtherThingsException(e);}if (result > 0)System.out.println("添加用戶成功");}/*** 查找用戶** @param user 用戶bean* @throws Exception*/public User findUser(User user) throws Exception {return userDao.findOneById(user.getLoginId());}/*** 用于更新sessionId*/public void updateLoginSession(String sessionId, String loginId) {userDao.updateLoginSession(sessionId, loginId);}
}