• Java单体应用


    原文地址:http://www.work100.net/training/monolithic-project-iot-cloud-admin-manager-rebuild-service-impl.html
    更多教程:光束云 - 免费课程

    重构 Service 实现

    序号 文内章节 视频
    1 概述 -
    2 定义AbstractBaseServiceImpl通用实现 -
    3 重构AuthManagerServiceImpl实现 -
    4 实例源码 -

    请参照如上章节导航进行阅读

    1.概述

    上一节课已经对 Service 层进行了重构,提取了通用方法,本节将对 Service 层的 实现 进行重构。

    2.定义AbstractBaseServiceImpl通用实现

    iot-cloud-commons 项目下新增 AbstractBaseServiceImpl 实现,代码如下:

    package net.work100.training.stage2.iot.cloud.commons.service.impl;
    
    import net.work100.training.stage2.iot.cloud.commons.dao.BaseDao;
    import net.work100.training.stage2.iot.cloud.commons.dto.AbstractBaseDomain;
    import net.work100.training.stage2.iot.cloud.commons.dto.BaseSearcher;
    import net.work100.training.stage2.iot.cloud.commons.service.BaseService;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import java.util.List;
    
    /**
     * <p>Title: BaseServiceImpl</p>
     * <p>Description: 通用的抽象Service实现</p>
     *
     * @author liuxiaojun
     * @date 2020-03-18 10:53
     * ------------------- History -------------------
     * <date>      <author>       <desc>
     * 2020-03-18   liuxiaojun     初始创建
     * -----------------------------------------------
     */
    public abstract class AbstractBaseServiceImpl<T extends AbstractBaseDomain, Searcher extends BaseSearcher, D extends BaseDao<T>> implements BaseService<T, Searcher> {
    
        @Autowired
        private D dao;
    
        @Override
        public List<T> selectAll() {
            return dao.selectAll();
        }
    
    
        @Override
        public T getById(Long id) {
            return dao.getById(id);
        }
    
        @Override
        public T getByKey(String entityKey) {
            return dao.getByKey(entityKey);
        }
    
    
        @Override
        public void delete(String entityKey) {
            dao.delete(entityKey);
        }
    
        @Override
        public void multiDelete(String[] entityKeys) {
            dao.multiDelete(entityKeys);
        }
    }
    

    3.重构AuthManagerServiceImpl实现

    修改 AuthManagerServiceImpl 实现,代码如下:

    package net.work100.training.stage2.iot.cloud.web.admin.service.impl;
    
    import net.work100.training.stage2.iot.cloud.commons.dto.BaseResult;
    import net.work100.training.stage2.iot.cloud.commons.dto.PageInfo;
    import net.work100.training.stage2.iot.cloud.commons.service.impl.AbstractBaseServiceImpl;
    import net.work100.training.stage2.iot.cloud.commons.utils.EncryptionUtils;
    import net.work100.training.stage2.iot.cloud.domain.AuthManager;
    import net.work100.training.stage2.iot.cloud.web.admin.dao.AuthManagerDao;
    import net.work100.training.stage2.iot.cloud.web.admin.dto.auth.ManagerSearcher;
    import net.work100.training.stage2.iot.cloud.web.admin.service.AuthManagerService;
    import org.apache.commons.lang3.time.DateFormatUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.*;
    
    /**
     * <p>Title: AuthManagerServiceImpl</p>
     * <p>Description: </p>
     * <p>Url: http://www.work100.net/training/monolithic-project-iot-cloud-admin.html</p>
     *
     * @author liuxiaojun
     * @date 2020-02-23 23:06
     * ------------------- History -------------------
     * <date>      <author>       <desc>
     * 2020-02-23   liuxiaojun     初始创建
     * -----------------------------------------------
     */
    @Service
    public class AuthManagerServiceImpl extends AbstractBaseServiceImpl<AuthManager, ManagerSearcher, AuthManagerDao> implements AuthManagerService {
    
        @Autowired
        private AuthManagerDao authManagerDao;
    
        @Override
        public BaseResult insert(AuthManager authManager) {
            if (authManagerDao.getByUserName(authManager.getUserName()) != null) {
                return BaseResult.fail("用户名已经存在");
            }
            try {
                // 生成 userKey
                authManager.setUserKey(generateUserKey(authManager.getUserName()));
    
                // 密码加密
                authManager.setPassword(EncryptionUtils.encryptPassword(EncryptionUtils.EncryptionType.MD5, authManager.getPassword()));
                authManager.setCreated(new Date());
                authManager.setUpdated(new Date());
    
                authManagerDao.insert(authManager);
                return BaseResult.success("新增账户成功");
            } catch (Exception ex) {
                return BaseResult.fail("未知错误");
            }
        }
    
        @Override
        public BaseResult update(AuthManager authManager) {
            if (authManagerDao.getByKey(authManager.getUserKey()) == null) {
                return BaseResult.fail("用户不存在");
            }
            try {
                authManager.setUpdated(new Date());
    
                authManagerDao.update(authManager);
                return BaseResult.success("账户更新成功");
            } catch (Exception ex) {
                return BaseResult.fail("未知错误");
            }
        }
    
        @Override
        public List<AuthManager> search(ManagerSearcher managerSearcher) {
            AuthManager authManager = new AuthManager();
            if (!managerSearcher.isAdvanced()) {
                authManager.setUserName(managerSearcher.getKeyword());
                authManager.setRoles("");
                authManager.setStatus(-1);
            } else {
                authManager.setUserName(managerSearcher.getUserName());
                authManager.setRoles(managerSearcher.getRoles());
                authManager.setStatus(managerSearcher.getStatus());
            }
            return authManagerDao.search(authManager);
        }
    
        @Override
        public PageInfo<AuthManager> pageSearch(int draw, int start, int length, ManagerSearcher managerSearcher) {
            Map<String, Object> params = new HashMap<>();
            params.put("start", start);
            params.put("length", length);
            AuthManager authManager = new AuthManager();
    
            if (!managerSearcher.isAdvanced()) {
                authManager.setUserName(managerSearcher.getKeyword());
                authManager.setRoles("");
                authManager.setStatus(-1);
    
                params.put("userName", managerSearcher.getKeyword());
                params.put("roles", "");
                params.put("status", -1);
            } else {
                authManager.setUserName(managerSearcher.getUserName());
                authManager.setRoles(managerSearcher.getRoles());
                authManager.setStatus(managerSearcher.getStatus());
    
                params.put("userName", managerSearcher.getUserName());
                params.put("roles", managerSearcher.getRoles());
                params.put("status", managerSearcher.getStatus());
            }
    
            // 处理分页结果
            PageInfo<AuthManager> authManagerPageInfo = new PageInfo<>();
            authManagerPageInfo.setDraw(draw);
    
            // 获取记录数
            int recordsTotal = authManagerDao.count(authManager);
            authManagerPageInfo.setRecordsTotal(recordsTotal);
            authManagerPageInfo.setRecordsFiltered(recordsTotal);
    
            // 获取分页数据
            List<AuthManager> data = authManagerDao.pageSearch(params);
            authManagerPageInfo.setData(data);
    
            return authManagerPageInfo;
        }
    
        @Override
        public AuthManager login(String userName, String password) {
            AuthManager authManager = authManagerDao.getByUserName(userName);
            if (authManager != null && authManager.getStatus() == 1) {
                // 验证密码,如果验证通过,则返回用户信息
                if (EncryptionUtils.validateEncryptPassword(password, authManager.getPassword())) {
                    return authManager;
                }
            }
            return null;
        }
    
        /**
         * 生成 userKey
         *
         * @param userName 用户名
         * @return
         */
        private String generateUserKey(String userName) {
            String strDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
            String sourceUserKey = String.format("%s%s", userName.toLowerCase(), strDate);
            return EncryptionUtils.encryptText(EncryptionUtils.EncryptionType.MD5, sourceUserKey);
        }
    
    }
    

    4.实例源码

    实例源码已经托管到如下地址:


    上一篇:重构Service层

    下一篇:重构搜索功能


    如果对课程内容感兴趣,可以扫码关注我们的 公众号QQ群,及时关注我们的课程更新

  • 相关阅读:
    .net core获取数据库连接 抛出The type initializer to throw an exception
    解决本地Bootstrap字体图标不可见的问题
    Bootstrap 遮罩插件jquery.mloading
    oracle 数据库触发器,插入更新时间戳
    入门wpf—— 3、样式
    VisualStudio 连接 MySql 实现增删查改
    Bootstrap-table 使用总结,和其参数说明
    oracle数据库 TIMESTAMP(6)时间戳类型
    3.xadmin配置
    2.Model设计
  • 原文地址:https://www.cnblogs.com/liuxiaojun/p/training-monolithic-project-iot-cloud-admin-manager-rebuild-service-impl.html
Copyright © 2020-2023  润新知