工具类:
AjaxResult
package com.thk.utils;
import java.util.HashMap;
/**
* 操作消息提醒
*
*
*/
public class AjaxResult extends HashMap<String, Object>
{
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";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
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);
}
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success()
{
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回错误消息
*
* @return
*/
public static AjaxResult error()
{
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
/**
* 方便链式调用
*
* @param key 键
* @param value 值
* @return 数据对象
*/
@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;
/**
* 不允许的http方法
*/
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;
/**
* 查询全部用户
*
* @param pageNo 当前页
* @param pageSize 条数
* @param people
* @return
*/
@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));
}
/**
* 增加用户或者修改用户
*
* @param people
* @return
*/
@PostMapping("/people")
public AjaxResult people(@RequestBody People people) {
return AjaxResult.success(peopleService.people(people));
}
/**
* 根据id查询一条数据
*
* @param id
* @return
*/
@GetMapping("/selectOne/{id}")
public AjaxResult selectOne(@PathVariable Long id) {
return AjaxResult.success(peopleService.selectOne(id));
}
/**
* 删除数据
*
* @param id
* @return
*/
@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<People> {
/**
* 查询全部用户
*
* @param people
* @return
*/
AjaxResult selectAll(People people);
/**
* 增加用户或者修改用户
*
* @param people
* @return
*/
AjaxResult people(People people) ;
/**
* 根据id查询一条数据
*
* @param id
* @return
*/
AjaxResult selectOne(Long id);
/**
* 删除数据
* @param id
* @return
*/
AjaxResult delPeople(Long id) ;
}
service实现类:
package com.thk.service.impl;
@Service
public class PeopleImpl extends ServiceImpl<PeopleMapper, People> implements IPeopleService {
@Autowired
private PeopleMapper peopleMapper;
/**
* 查询全部用户
*
* @param people
* @return
*/
public AjaxResult selectAll(People people) {
return AjaxResult.success(baseMapper.selectList(null));
}
/**
* 增加用户或者修改用户
*
* @param people
* @return
*/
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));
}
}
/**
* 根据id查询一条数据
*
* @param id
* @return
*/
public AjaxResult selectOne(Long id) {
return AjaxResult.success(peopleMapper.selectById(id));
}
/**
* 删除数据
*
* @param id
* @return
*/
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<People> {
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.thk.mapper.PeopleMapper"> </mapper>
启动程序:

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

现在返回:

版权声明:本文为weixin_58696998原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。