天天看点

spring+springMVC+mybatis的整合 part5

UserService实现(注意编程思维的养成)

根据面向接口编程的思维来讲,在

Service

中核心是实现

Dao

层,并调用

Dao

层。

UserDao

层通过单元测试了,现在中心就应该放在业务逻辑的实现,毕竟数据持久化已经实现了。

从服务端程序的角度看来,用户的主要业务有注册、登录、注销登录、注销帐号等等,这里我们先拿注册来说事。

用户注册流程分析(用户角度):

填写帐号相关信息

提交注册信息

服务器返回是否注册成功

用户注册流程分析(服务器角度):

收到用户注册请求

解包数据→封装到

UserBean

解包数据失败(请求信息异常),返回错误提示信息

针对具体的用户信息检查是否符合标准不符合检查标准,返回对应的错误提示

通过检查,调用

Dao

检查是否存在同样的用户数据库已经存在相同的用户信息,不能重复添加,返回错误提示信息

不存在同样的用户,添加新用户,并返回成功的提示信息

流程图反映如下:

ssm框架用户行为解析流程图

自定义异常

新建一个与

dao

domain

同级的包

exception

来存放自定义的异常

所有的自定义异常都要继承于

Exception

UserAireadyExistException

用户已经存在异常

public UserAireadyExistException(String s) {
        super(s);
    }
    public UserAireadyExistException(String message, Throwable cause) {
        super(message, cause);
    }
           

下面的

UserCanNotBeNullException

(用户为空异常),

UserNameCanNotBeNullException

(用户名为空异常),

UserPwdCanNotBeNullException

(用户密码为空)异常代码相类似

还有个其他异常

public class OtherThingsException extends Exception {
    public OtherThingsException(String message) {
        super(message);
    }
    public OtherThingsException(Exception e){
        this(e.getMessage());
    }
    public OtherThingsException(String message, Throwable cause) {
        super(message, cause);
    }
}
           

可能会抛出的异常写完了,下面来实现用户注册的

Service

首先还是先写个

BaseService

接口,用泛型解耦

public interface BaseService<T> {
    void add(T t) throws Exception;
}
           

用户注册接口

不同对象的业务体系不同,

BaseService

并不能完全替代不同对象的具体行为表现

UserService

public interface UserService extends BaseService<User>{
    void add(User user) throws Exception;
    User findUser(User user) throws Exception;
}
           
实例化用户注册接口

UserServiceImpl

service

包下创建一个子包,叫

serviceImpl

,用来存放业务层接口实现类

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService{
    @Autowired
    private 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");
        }
        //由于我这个是管理系统,根据业务需求来说,我们的用户基本信息都是不能为空的
        //基本信息包括:姓名、年龄、用户名、密码、性别、手机号,年龄大于18
        if ( 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; //受影响的行数默认为0
        try {
            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);
    }
}
           

在这里我们用到了StringUtils这个JAVA的工具类

http://www.jianshu.com/p/2eb9e42ecf44

写完每个

Service

后,都需要针对具体的对象的行为进行单元测试,

UserServiceTest

代码如下:

同样,要继承基础测试类

public class UserServiceTest extends BaseTest{
    @Autowired
    private UserServiceImpl userService;
    //此处直接使用UserService的实现类,主要是方便抛出异常,然后异常出现时候可以针对性的处理

    @Test
    public void testAdd() {
        User user = new User();
        user.setLoginId("20171");
        user.setName("意识流1");
        user.setPwd("123456");
        user.setSex("不知道");
        user.setDuty("老大");
        user.setCellNumber("12345678910");
        user.setAge(10);
        try {
            userService.add(user);
        } catch (UserCanNotBeNullException e) {
            //用户不能为空异常抛出
            e.printStackTrace();
        } catch (UserNameCanNotBeNullException e) {
            //用户名不能为空
            e.printStackTrace();
        } catch (UserPwdCanNotBeNullException e) {
            //用户密码不能为空
            e.printStackTrace();
        } catch (UserAireadyExistException e) {
            //用户存在抛出
            e.printStackTrace();
        } catch (OtherThingsException e) {
            //其他综合异常或是不能处理的异常
            e.printStackTrace();
        }
    }
    @Test
    public void testFindUser() throws Exception {
        User user = new User();
        user.setLoginId("20171");
        User result = null; //受影响的行数默认为0
        try {
            result = userService.findUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查找用户失败");
        }
        if (null!=result)
            System.out.println("查找用户成功\n"+result.toString());
    }
}
           

同样的,我们的Service的测试代码执行后,我们可以在mysql中看到具体的数据变化

主要参考于大牛

Clone丶记忆

的SSM集成之路