spring+springMVC+mybatis的整合 part5

简介: UserService实现(注意编程思维的养成)根据面向接口编程的思维来讲,在Service中核心是实现Dao层,并调用Dao层。UserDao层通过单元测试了,现在中心就应该放在业务逻辑的实现,毕竟数据持久化已经实现了。

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

根据面向接口编程的思维来讲,在Service中核心是实现Dao层,并调用Dao层。
UserDao层通过单元测试了,现在中心就应该放在业务逻辑的实现,毕竟数据持久化已经实现了。
从服务端程序的角度看来,用户的主要业务有注册、登录、注销登录、注销帐号等等,这里我们先拿注册来说事。

用户注册流程分析(用户角度):
填写帐号相关信息
提交注册信息
服务器返回是否注册成功

用户注册流程分析(服务器角度):
收到用户注册请求
解包数据→封装到UserBean解包数据失败(请求信息异常),返回错误提示信息
针对具体的用户信息检查是否符合标准不符合检查标准,返回对应的错误提示
通过检查,调用Dao检查是否存在同样的用户数据库已经存在相同的用户信息,不能重复添加,返回错误提示信息
不存在同样的用户,添加新用户,并返回成功的提示信息

流程图反映如下:


ssm%E6%A1%86%E6%9E%B6%E7%94%A8%E6%88%B7%

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

自定义异常
新建一个与daodomain同级的包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集成之路

目录
相关文章
|
3天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
17 4
|
6天前
|
XML Java 数据库连接
Spring Boot与MyBatis:整合与实战
【4月更文挑战第29天】在现代的Java Web应用开发中,持久化层框架扮演了至关重要的角色。MyBatis作为一款优秀的持久化框架,被广泛应用于Java开发中。Spring Boot提供了简化开发流程的功能,而与MyBatis的整合也变得更加便捷。
16 0
|
10天前
|
Java 数据库连接 数据库
spring+mybatis_编写一个简单的增删改查接口
spring+mybatis_编写一个简单的增删改查接口
14 2
|
22天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
22天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
22天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
31 3
|
22天前
|
Java 数据库连接 Spring
黄金搭档:Spring与MyBatis完美整合的终极指南
黄金搭档:Spring与MyBatis完美整合的终极指南
22 0
黄金搭档:Spring与MyBatis完美整合的终极指南
|
1月前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
2月前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
90 0
|
2月前
|
移动开发 Java 测试技术
Spring MVC+Spring+Mybatis实现支付宝支付功能(附完整代码)
Spring MVC+Spring+Mybatis实现支付宝支付功能(附完整代码)
22 1