美文网首页
Java - Spring Boot + MyBatis-Plu

Java - Spring Boot + MyBatis-Plu

作者: 西半球_ | 来源:发表于2020-05-25 16:14 被阅读0次

    Service层的一些代码:

    itemService代码:

    @Service
    @Transactional(readOnly = true)
    public class DictItemService {
        @Autowired
        private DictItemDao dictItemDao;
    
        /**
         * 新增一个字典项
         */
        @Transactional(readOnly = false)
        public DictItem addDictItem(DictItem dictItem){
            DictItem selectItem = getDictItemByValue(dictItem.getValue());
            if(selectItem ==null) {
                SysDictItemEntity entity = new SysDictItemEntity(dictItem);
                dictItemDao.insert(entity);
                return DaoUtil.getData(entity);
            }else {
                return null;
            }
        }
    
        /**
         * 删除一个或多个字典项
         */
        @Transactional(readOnly = false)
        public Boolean deleteDictItemByIdArr(List<String> itemIds){
            List<SysDictItemEntity> mList = new ArrayList<>();
            for (String id :itemIds){
                //进行id验证,查到再删
                DictItem selectItem = getDictItemById(id);
                if (selectItem !=null){
                    dictItemDao.deleteById(id);
                }else {
                    return false;
                }
            }
            return true;
        }
    
        /**
         * 根据字典项id, 修改一个字典项
         */
        @Transactional(readOnly = false)
        public DictItem updateDictItemById(String id, DictItem dictItem){
            SysDictItemEntity selectItemEntity = dictItemDao.selectById(id);
            if (selectItemEntity !=null){
                if (dictItem.getLabel() != null){
                    selectItemEntity.setLabel(dictItem.getLabel());
                }
                if (dictItem.getValue() != null){
                    selectItemEntity.setValue(dictItem.getValue());
                }
                if (dictItem.getSort() != null){
                    selectItemEntity.setSort(dictItem.getSort());
                }
                dictItemDao.updateById(selectItemEntity);
                return DaoUtil.getData(selectItemEntity);
            }else {
                return null;
            }
        }
    
        /**
         * 根据字典项id, 查询一个字典项
         */
        public DictItem getDictItemById(String id){
            return DaoUtil.getData(dictItemDao.selectById(id));
        }
    
        /**
         * 根据字典项value, 查询一个字典项
         */
        public DictItem getDictItemByValue(String value){
            QueryWrapper<SysDictItemEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("value",value);
            return DaoUtil.getData(dictItemDao.selectOne(wrapper));
        }
    
        /**
         * 根据类型表的value , 查询字典项列表
         */
        public List<DictItem> getDictItemListByTypeValue(String value) {
            QueryWrapper<SysDictItemEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("type",value);
            wrapper.orderByAsc("sort");
            return DaoUtil.convertDataList(dictItemDao.selectList(wrapper));
        }
    
    }
    

    dictService代码:

    @Service
    @Transactional(readOnly = true)
    public class DictTypeService {
        @Autowired
        private DictTypeDao dictTypeDao;
    
        @Autowired
        private DictItemDao dictItemDao;
    
        @Autowired
        DictItemService dictItemService;
    
        /**
         * 新增一个类型 先根据value查询是否存在,不存在才可以新增
         */
        @Transactional(readOnly = false)
        public DictType addDictType(DictType dictType) {
            DictType dictType_select = getDictTypeByValue(dictType.getValue());
            if (dictType_select == null) {
                SysDictTypeEntity entity = new SysDictTypeEntity(dictType);
                dictTypeDao.insert(entity);
                return DaoUtil.getData(entity);
            } else {
                return null;
            }
        }
    
        /**
         * 根据类型id, 删除一个类型(删除该类型下所有字典项)
         */
        @Transactional(readOnly = false)
        public void deleteDictTypeById(String id, String value) {
            Map<String, Object> mMap = new HashMap<>();
            mMap.put("type", value);
            dictTypeDao.deleteById(id);
            dictItemDao.deleteByMap(mMap);
        }
    
        /**
         * 根据类型id, 修改一个类型
         */
        @Transactional(readOnly = false)
        public DictType updateDictTypeById(String id, DictType dictType) {
            SysDictTypeEntity selectTypeEntity = dictTypeDao.selectById(id);
            if (selectTypeEntity!=null){
                if(dictType.getName() != null) {
                    selectTypeEntity.setName(dictType.getName());
                }
                if(dictType.getValue() != null) {
                    selectTypeEntity.setValue(dictType.getValue());
                }
                if(dictType.getDescription() != null) {
                    selectTypeEntity.setDescription(dictType.getDescription());
                }
                if(dictType.getSort() != null) {
                    selectTypeEntity.setSort(dictType.getSort());
                }
                if(dictType.getParentId() != null) {
                    selectTypeEntity.setParentId(dictType.getParentId());
                }
                if(dictType.getEnabled() != null) {
                    selectTypeEntity.setEnabled(dictType.getEnabled());
                }
                dictTypeDao.updateById(selectTypeEntity);
                return DaoUtil.getData(selectTypeEntity);
            }else {
                return null;
            }
        }
    
        /**
         * 根据类型id,查询一个类型
         */
        public DictType getDictTypeById(String id) {
            return DaoUtil.getData(dictTypeDao.selectById(id));
        }
    
        /**
         * GET
         * 根据类型 type 对应的value,查询一个字典项
         */
        public DictType getDictTypeByValue(String value) {
            QueryWrapper<SysDictTypeEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("value", value);
            return DaoUtil.getData(dictTypeDao.selectOne(wrapper));
        }
    
        /**
         * GET
         * 不分页查询 全部 类型列表
         */
        public List<DictType> getDictTypes() {
            Map<String, Object> queryName = Maps.newHashMap();
            return DaoUtil.convertDataList(dictTypeDao.selectList(
                    Wrappers.<SysDictTypeEntity>query()
                            .allEq(queryName, false)
                            .orderByAsc("sort")
            ));
        }
    
        /**
         * 分页查询类型列表
         */
        public IPage<DictType> getDictTypeListWithPage(String name, String parentId, int page, int limit) {
            QueryWrapper<SysDictTypeEntity> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(name), "name", name);
            wrapper.eq(StringUtils.isNotBlank(parentId), "parent_id", parentId);
            wrapper.orderByAsc("sort");
            IPage<SysDictTypeEntity> pageRaw = new Page<>(page, limit);
            IPage<SysDictTypeEntity> iPage = dictTypeDao.selectPage(pageRaw, wrapper);
            return pageRaw.convert(raw -> DaoUtil.getData(raw));
        }
    
        /**
         * 查询子类型列表
         */
        public List<DictType> getDictChildList(String parent_value, String parentId) {
            QueryWrapper<SysDictTypeEntity> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(parent_value),"value", parent_value);
            wrapper.eq(StringUtils.isNotBlank(parentId),"parent_id", parentId);
            wrapper.orderByAsc("sort");
            return DaoUtil.convertDataList(dictTypeDao.selectList(wrapper));
        }
    
        /**
         * 获取全部字典类型及该类型所对应的字典项, 传字典类型对应的value,该项若为空则默认为所有
         */
        public Map<String, Object> getDictsByTypeValue(String value) {
            HashMap<String, Object> jsonObject = new HashMap<>();
            if (StringUtils.isNotBlank(value)) {
                //查询类型数据
                DictType dictType = getDictTypeByValue(value);
                List<DictItem> dictItems = dictItemService.getDictItemListByTypeValue(value);
                HashMap<String, String> tempDict = new HashMap<>();
                for (DictItem dictItem : dictItems) {
                    tempDict.put(dictItem.getValue(), dictItem.getLabel());
                }
                jsonObject.put(dictType.getValue(), tempDict);
            } else {
                List<DictType> dictTypes = getDictTypes();
                for (DictType dictType : dictTypes) {
                    List<DictItem> dictItems = dictItemService.getDictItemListByTypeValue(dictType.getValue());
                    HashMap<String, String> tempDict = new HashMap<>();
                    for (DictItem dictItem : dictItems) {
                        tempDict.put(dictItem.getValue(), dictItem.getLabel());
                    }
                    jsonObject.put(dictType.getValue(), tempDict);
                }
            }
            return jsonObject;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Java - Spring Boot + MyBatis-Plu

          本文链接:https://www.haomeiwen.com/subject/peetahtx.html