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

Java - Spring Boot + MyBatis-Plu

作者: 西半球_ | 来源:发表于2020-05-13 17:58 被阅读0次

    Controller -> Service > Entity

    Controller 代码

    @RestController
    @RequestMapping("v1/api")
    
    public class DictsController {
    
        @Autowired
        DictService dictService;
    
        @ModelAttribute
        public Dict get(@PathVariable(required = false) String id) {
            Dict entity = null;
            if (StringUtils.isNotBlank(id)) {
                entity = dictService.getById(id);
    //            System.out.println("-----entity -----"+entity);
            }
            if (entity == null) {
                entity = Dict.builder().build();
    //            System.out.println("-----entity == null------");
            }
            return entity;
        }
    
    
        /**
         * post请求 添加 localhost:8080/v1/api/dict
         */
        @PostMapping("/dict")
        public Dict addDict(Dict dict){
    //        System.out.println(" addDict --- "+dict);
            dict = dictService.addDict(dict);
            return dict;
        }
    
    //    @PostMapping("/dict")
    //    public Dict post(
    //            @RequestParam(required = false) String value,
    //            @RequestParam(required = false) String label,
    //            @RequestParam(required = false) String type,
    //            @RequestParam(required = false) String description,
    //            @RequestParam(required = false) Integer sort,
    //            @RequestParam(required = false) String parent_id,
    //            @RequestParam(required = false) String disabled){
    //
    ////        System.out.println("post传的参数"+dict);
    //
    //        Dict dict = Dict.builder()
    //                .value(value)
    //                .label(label)
    //                .type(type)
    //                .description(description)
    //                .sort(sort)
    //                .parentId(parent_id)
    //                .disabled(disabled)
    //                .build();
    ////        System.out.println("dict"+dict);
    //
    //        dict = dictService.addDict(dict);
    //
    //        return dict;
    //    }
    
    
        /**
         * post请求 删除 localhost:8080/v1/api/dict/123
         */
        @DeleteMapping(value = "/dict/{id:\\w+}")
        public void delete(@PathVariable String id) {
            dictService.deleteById(id);
        }
    
    
        /**
         * post请求 更新 localhost:8080/v1/api/dict/123
         */
        @PostMapping(value = "/dict/{id:\\w+}")
        public Dict updateDict(Dict dict){
    //        System.out.println("post请求 更新 ---- "+dict);
            dict = dictService.updateDict(dict);
            return dict;
        }
    
        /**
         * get请求 传ID 查询信息 localhost:8080/v1/api/dict/123
         */
        @GetMapping(value = "/dict/{id:\\w+}")
        public Dict getDictWithID(@PathVariable(required = false) String id) {
            return dictService.getById(id);
        }
    
    
        /**
         * get请求 传type 获取列表 localhost:8080/v1/api/dicts?type=111
         */
        @GetMapping("/dicts")
        public List<Dict> getDictList(@RequestParam(required = false) Dict dict) {
            return dictService.getDictList(dict);
        }
    }
    

    Service 代码

    @Service
    //@Transactional(readOnly = true)
    public class DictService {
        @Autowired
        private DictDao dictDao;
    
        //新增
        public Dict addDict(Dict dict){
            SysDictEntity entity = new SysDictEntity(dict);
            if(StringUtils.isBlank(dict.getId())) {
                dictDao.insert(entity);
            } else {
                dictDao.updateById(entity);
            }
            return entity.toDto();
        }
    
        //删除
        public void deleteById(String id){
            dictDao.deleteById(id);
        }
    
        //更新
        public Dict updateDict(Dict dict){
            SysDictEntity entity = new SysDictEntity(dict);
            if(StringUtils.isBlank(dict.getId())) {
                dictDao.insert(entity);
            } else {
                dictDao.updateById(entity);
            }
    
            return entity.toDto();
        }
    
        //查询 根据ID
        public Dict getById(String id){
           return DaoUtil.getData(dictDao.selectById(id));
        }
    
        //查询列表
        public List<Dict> getDictList(Dict dict) {
            Map<String,Object> queryName = Maps.newHashMap();
            if(dict != null) {
                queryName.put("type", dict.getType());
            }
            return DaoUtil.convertDataList(dictDao.selectList(Wrappers.<SysDictEntity>query()
                    .allEq(queryName,false)));
        }
    }
    
    

    Dao 代码

    public interface DictDao extends BaseMapper<SysDictEntity> {
    }
    

    Entity 代码

    @Data
    @EqualsAndHashCode(callSuper = true)
    @ToString(callSuper = true)
    @TableName("dict")
    public class SysDictEntity extends BaseUUIDEntity<Dict> {
        private String value;   // 数据值
        private String label;   // 标签名
        private String type;    // 类型
        private String description;// 描述
        private Integer sort;   // 排序
        private String parentId;//父Id
        private String disabled;
    
        @Override
        public Dict toDto() {
            return Dict.builder()
                    .id(id)
                    .value(value)
                    .label(label)
                    .type(type)
                    .description(description)
                    .sort(sort)
                    .parentId(parentId)
                    .disabled(disabled)
                    .build();
        }
        public SysDictEntity(Dict dict) {
            try {
                BeanUtils.copyProperties(this, dict);
            } catch (IllegalAccessException | InvocationTargetException e) {
            }
        }
    
        public SysDictEntity() {
        }
        
    }
    

    相关文章

      网友评论

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

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