-
创建一个BaseApiService.java
import java.util.HashMap;
import java.util.Map;
public class BaseApiService {
//setResult(200,"success",null)
public static Map<String, Object> setResultSuccess() {
return setResult(BaseApiContents.RES_HTTP_CODE_200_VALUE, BaseApiContents.RES_HTTP_CODE_SUCCESS, null);
}
//setResult(200,msg,null)
public static Map<String, Object> setResultSuccessMsg(String msg) {
return setResult(BaseApiContents.RES_HTTP_CODE_200_VALUE, msg, null);
}
//setResult(200,"success",data)
public static Map<String, Object> setResultSuccessData(Object data) {
return setResult(BaseApiContents.RES_HTTP_CODE_200_VALUE, BaseApiContents.RES_HTTP_CODE_SUCCESS, data);
}
//setResult(500,"error",null)
public static Map<String, Object> setResultError() {
return setResult(BaseApiContents.RES_HTTP_CODE_500_VALUE, BaseApiContents.RES_HTTP_CODE_ERROR, null);
}
//setResult(500,msg,null)
public static Map<String, Object> setResultErrorMsg(String msg) {
return setResult(BaseApiContents.RES_HTTP_CODE_500_VALUE, msg, null);
}
/**
* 自定义查询结果
*/
public static Map<String, Object> setResult(Integer code, String msg, Object data) {
Map<String, Object> map = new HashMap<>();
map.put(BaseApiContents.RES_HTTP_CODE_NAME, code);
map.put(BaseApiContents.RES_HTTP_CODE_MSG, msg);
map.put(BaseApiContents.RES_HTTP_CODE_DATA, data);
return map;
}
/**
* 封装JqGrid分页查询方法
*/
public static Map<String,Object> setResultJqGrid(Integer rows,Integer page,Integer records, Object data){
Map<String,Object> map = new HashMap<>();
//总页数
Integer total = null;
if(records%rows == 0){
total = records/rows;
}else{
total = records/rows+1;
}
//存入Map集合
map.put("rows",data);
map.put("page",page);
map.put("total",total);
map.put("records",records);
return map;
}
}
-
创建一个BaseApiContents.java接口
public interface BaseApiContents {
//响应 code
String RES_HTTP_CODE_NAME= "code";
//响应 200
Integer RES_HTTP_CODE_200_VALUE= 200;
//响应 500
Integer RES_HTTP_CODE_500_VALUE= 500;
//响应 信息
String RES_HTTP_CODE_MSG= "msg";
//响应 数据
String RES_HTTP_CODE_DATA= "data";
//响应 成功
String RES_HTTP_CODE_SUCCESS= "success";
//响应 错误
String RES_HTTP_CODE_ERROR= "error";
//JqGrid响应
//响应状态
String RES_HTTP_OPER_NAME = "status";
//添加
String RES_HTTP_OPER_ADD = "addOk";
//删除
String RES_HTTP_OPER_DEL = "delOk";
//编辑
String RES_HTTP_OPER_EDIT = "editOk";
//错误
String RES_HTTP_OPER_ERROR = "error";
}
-
Controller中就可以继承BaseApiService类,使用其中的方法了
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseApiService {
@Autowired
private AdminService adminService;
//登录验证
@RequestMapping("login")
public Map<String,Object> login(String username,String password,String code,HttpServletRequest request){
//获得验证码
String validationCode = (String) request.getSession().getAttribute("code");
if(!validationCode.equalsIgnoreCase(code)){
//使用BaseApiService中的方法
return setResultErrorMsg("验证码错误");
}
try {
return adminService.login(username, password, code, request);
}catch (Exception e){
return setResultErrorMsg(e.getMessage());
}
}
//退出登录,清除session
@RequestMapping("clearSession")
public String clearSession(HttpServletRequest request){
request.getSession().removeAttribute("admin");
return "ok";
}
}
-
ServiceImpl中也可以用BaseApiService类中的方法
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDAO, Admin> implements AdminService {
@Autowired
private AdminDAO adminDAO;
@Override
//登录验证
public Map<String, Object> login(String username, String password, String code, HttpServletRequest request) {
Map<String,Object> map = new HashMap<>();
//查询用户名是否存在
map.put("name",username);
List<Admin> admins = adminDAO.selectByMap(map);
if(admins.size() == 0 || admins == null){
throw new RuntimeException("用户不存在");
}else{
Admin admin = admins.get(0);
if(!password.equals(admin.getPassword())){
throw new RuntimeException("密码错误");
}else{
//调用BaseApiService中的静态setResultSuccess()方法
map = BaseApiService.setResultSuccess();
request.getSession().setAttribute("admin",admin);
}
}
return map;
}
}
-
其他地方的使用
@RequestMapping("operBanner")
//增删改操作
public Map<String,Object> operBanner(String oper,Banner banner){
HashMap map = new HashMap();
if ("add".equals(oper)) {
bannerService.add(banner);
return setResultSuccessMsg(BaseApiContents.RES_HTTP_OPER_ADD);
} else if ("del".equals(oper)){
bannerService.deleteById(banner.getId());
return setResultSuccessMsg(BaseApiContents.RES_HTTP_OPER_DEL);
}else if("edit".equals(oper)){
bannerService.updateById(banner);
return setResultSuccessMsg(BaseApiContents.RES_HTTP_OPER_EDIT);
}else {
return setResultErrorMsg(BaseApiContents.RES_HTTP_OPER_ERROR);
}
}
用的JqGrid和Mybatis-plus,两个api用着还是挺舒服的。