栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java AjaxResult 操作消息提醒返回

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java AjaxResult 操作消息提醒返回

工具类: AjaxResult   
package com.thk.utils;

import java.util.HashMap;


public class AjaxResult extends HashMap
{
    private static final long serialVersionUID = 1L;

    
    public static final String CODE_TAG = "code";

    
    public static final String MSG_TAG = "msg";

    
    public static final String DATA_TAG = "data";

    
    public AjaxResult()
    {
    }

    
    public AjaxResult(int code, String msg)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    
    public AjaxResult(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (StringUtils.isNotNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    
    public static AjaxResult success()
    {
        return AjaxResult.success("操作成功");
    }

    
    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

    
    public static AjaxResult success(String msg)
    {
        return AjaxResult.success(msg, null);
    }

    
    public static AjaxResult success(String msg, Object data)
    {
        return new AjaxResult(HttpStatus.SUCCESS, msg, data);
    }

    
    public static AjaxResult error()
    {
        return AjaxResult.error("操作失败");
    }

    
    public static AjaxResult error(String msg)
    {
        return AjaxResult.error(msg, null);
    }

    
    public static AjaxResult error(String msg, Object data)
    {
        return new AjaxResult(HttpStatus.ERROR, msg, data);
    }

    
    public static AjaxResult error(int code, String msg)
    {
        return new AjaxResult(code, msg, null);
    }

    
    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}

返回状态码
package com.thk.utils;


public class HttpStatus
{
    
    public static final int SUCCESS = 200;

    
    public static final int CREATED = 201;

    
    public static final int ACCEPTED = 202;

    
    public static final int NO_CONTENT = 204;

    
    public static final int MOVED_PERM = 301;

    
    public static final int SEE_OTHER = 303;

    
    public static final int NOT_MODIFIED = 304;

    
    public static final int BAD_REQUEST = 400;

    
    public static final int UNAUTHORIZED = 401;

    
    public static final int FORBIDDEN = 403;

    
    public static final int NOT_FOUND = 404;

    
    public static final int BAD_METHOD = 405;

    
    public static final int CONFLICT = 409;

    
    public static final int UNSUPPORTED_TYPE = 415;

    
    public static final int ERROR = 500;

    
    public static final int NOT_IMPLEMENTED = 501;
}

controller
package com.thk.controller;


@RestController
public class PeopleController {
    @Autowired
    private IPeopleService peopleService;

    
    @GetMapping("/selectAll")
    public AjaxResult selectAll(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, People people) {
        //接收前端传送的当前页和总条数,放入PageHelper当中,如果没有就取默认值 1和10
        PageHelper.startPage(pageNo, pageSize);
        return AjaxResult.success(peopleService.selectAll(people));
    }

    
    @PostMapping("/people")
    public AjaxResult people(@RequestBody People people) {
        return AjaxResult.success(peopleService.people(people));
    }

    
    @GetMapping("/selectOne/{id}")
    public AjaxResult selectOne(@PathVariable Long id) {
        return AjaxResult.success(peopleService.selectOne(id));
    }

    
    @DeleteMapping("/delPeople/{id}")
    public AjaxResult delPeople(@PathVariable Long id) {
        return AjaxResult.success(peopleService.delPeople(id));
    }

}

service
package com.thk.service;


public interface IPeopleService extends IService {
    
    AjaxResult selectAll(People people);

    
    AjaxResult people(People people) ;

    
    AjaxResult selectOne(Long id);

    
    AjaxResult delPeople(Long id) ;
}

service实现类:
package com.thk.service.impl;

@Service
public class PeopleImpl extends ServiceImpl implements IPeopleService {

    @Autowired
    private PeopleMapper peopleMapper;


    
    public AjaxResult selectAll(People people) {
        return AjaxResult.success(baseMapper.selectList(null));
    }

    
    public AjaxResult people(People people)  {
        //判断id是否为空,如果为空就是添加
        if (StringUtils.isEmpty(people.getId().toString())) {
            SnowFlake snowFlake = new SnowFlake(2, 3);
            long l = snowFlake.nextId();
            //设置id
            people.setId(l);
            System.out.println("添加的id是:" + l);
            //添加数据
            return AjaxResult.success(baseMapper.insert(people));

        } else {
            //如果id不为空,就是修改
            //到数据库查询是否存在这条数据,如果不存在抛出异常
            People people1 = peopleMapper.selectById(people.getId());
            if (StringUtils.isNotNull(people1)) {
                return AjaxResult.error("数据不存在");
            }
            //修改数据
            return AjaxResult.success(baseMapper.updateById(people));
        }
    }

    
    public AjaxResult selectOne(Long id) {
        return AjaxResult.success(peopleMapper.selectById(id));
    }

    
    public AjaxResult delPeople(Long id)  {
        if (!StringUtils.isEmpty(id.toString())) {
            People people = peopleMapper.selectById(id);
            if (people != null) {
                //删除数据
                return AjaxResult.success(peopleMapper.deleteById(id));
            } else {
                return AjaxResult.error("修改的数据不存在!!!");
            }
        } else {
            return AjaxResult.error("数据异常!!!");
        }
    }
}
mapper接口
package com.thk.mapper;


@Mapper
public interface PeopleMapper extends BaseMapper {

}
mapper.xml




启动程序:

测试:

以前没有返回状态码,操作提示等等

 

 现在返回:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838207.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号