前后端分离,SSM后台控制层模板
提示:用SSM框架开发时,控制层的代码基本相同,所以写了一下代码方便今后开发另一个模块的代码
Controller层相关代码:
提示:在com.cn.util这个包里引入了Info、ReturnVal两个工具类。
作用是项目运行时请求方法后返还日志信息。
package com.cn.medical_school.faculty.appointmenttype;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.util.Info;
import com.cn.util.ReturnVal;
@Controller
@RequestMapping(value="TblAppointmentType")
public class TblAppointmentTypeContorller {
@Autowired
private TblAppointmentTypeService tblAppointmentTypeService;
boolean flag = false;
//查询所有列表
@RequestMapping(value="ViewAll",method = RequestMethod.POST, produces = "application/json;charset=utf-8" )
@ResponseBody
public ReturnVal<TblAppointmentType> viewAllFun() {
try {
// 查询列表
List<TblAppointmentType> tblAppointmentTypeList = tblAppointmentTypeService.viewAllFun();
// 查询成功,返回成功信息和数据
return new ReturnVal(Info.CODE, Info.SUCCESS, tblAppointmentTypeList);
} catch (Exception e) {
e.printStackTrace();
// 遇到其他错误,返回失败信息
return new ReturnVal<TblAppointmentType>(Info.CODE, Info.OTHER, null);
}
}
// 按ID查询
@RequestMapping(value = "ViewById", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@ResponseBody
public ReturnVal viewByIdFun(TblAppointmentType tblAppointmentType) {
if (tblAppointmentType == null || tblAppointmentType.equals("")) {
// 如果参数为空,则查询失败,返回失败信息
return new ReturnVal(Info.CODE, Info.EMPTY, null);
} else if (!(tblAppointmentType == null || tblAppointmentType.equals(""))) {
// 如果参数不为空,执行查询操作
try{
TblAppointmentType info = tblAppointmentTypeService.viewByIdFun(tblAppointmentType);
// 查询成功,返回成功信息和数据
return new ReturnVal(Info.CODE,Info.SUCCESS,info);
}catch (Exception e){
e.printStackTrace();
// 遇到错误,返回失败信息
return new ReturnVal(Info.CODE,Info.ERROR,null);
}
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
}
// 删除*******
@RequestMapping(value = "Remove", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@ResponseBody
public ReturnVal removeFun(Integer appointmentId) {
if (appointmentId == null || appointmentId.equals("")) {
// 如果参数为空,则删除失败,返回失败信息
return new ReturnVal(Info.CODE, Info.EMPTY, null);
} else if (!(appointmentId == null || appointmentId.equals(""))) {
// 如果参数不为空,执行删除操作
flag = tblAppointmentTypeService.removeById(appointmentId);
if (flag == true) {
// 如果删除成功,则返回成功信息
return new ReturnVal(Info.CODE, Info.SUCCESS, null);
} else if (flag == false) {
// 如果删除失败,则返回失败信息
return new ReturnVal(Info.CODE, Info.ERROR, null);
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
}
// 增加
@RequestMapping(value = "/Add", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@ResponseBody
public ReturnVal addFun(@RequestBody TblAppointmentType tblAppointmentType) {
if (tblAppointmentType == null || tblAppointmentType.equals("")) {
// 如果参数为空,则添加失败,返回失败信息
return new ReturnVal(Info.CODE, Info.EMPTY, null);
} else if (!(tblAppointmentType == null || tblAppointmentType.equals(""))) {
// 如果参数不为空,则先查询是否与表中数据有冲突(一般是唯一约束,查询唯一约束)
TblAppointmentType info = tblAppointmentTypeService.viewByCodeFun(tblAppointmentType);
if (info == null) {
// 如果查询结果为空,则执行添加
flag = tblAppointmentTypeService.addFun(tblAppointmentType);
// 判断插入数据是否成功
if (flag == true) {
// 如果添加成功,则返回成功信息
return new ReturnVal(Info.CODE, Info.SUCCESS, null);
} else if (flag == false) {
// 如果添加失败,则返回失败信息
return new ReturnVal(Info.CODE, Info.ERROR, null);
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
} else if (!(info == null)) {
// 如果查询到结果,则返回失败信息
return new ReturnVal(Info.CODE, Info.REPEAT, null);
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
}
// 修改
@RequestMapping(value = "Modify", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@ResponseBody
public ReturnVal updateByIdFun(@RequestBody TblAppointmentType tblAppointmentType) {
if (tblAppointmentType == null || tblAppointmentType.equals("")) {
// 如果参数为空,则删除失败,返回失败信息
return new ReturnVal(Info.CODE, Info.EMPTY, null);
} else if (!(tblAppointmentType == null || tblAppointmentType.equals(""))) {
// 如果参数不为空,则执行修改操作
flag = tblAppointmentTypeService.modifyFun(tblAppointmentType);
if (flag == true) {
// 如果修改成功,则返回成功信息
return new ReturnVal(Info.CODE, Info.SUCCESS, null);
} else if (flag == false) {
// 如果修改失败,则返回失败信息
return new ReturnVal(Info.CODE, Info.ERROR, null);
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
} else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE, Info.OTHER, null);
}
}
//模糊查询
@RequestMapping(value = "/Vaguequery", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
@ResponseBody
public ReturnVal VagueQueryFun(@RequestBody TblAppointmentType tblAppointmentType) {
if (tblAppointmentType == null || tblAppointmentType.equals("")){
// 如果参数为空,则查询失败,返回失败信息
return new ReturnVal(Info.CODE,Info.EMPTY,null);
}else if(!(tblAppointmentType == null || tblAppointmentType.equals(""))){
// 如果参数不为空,则执行查询操作
try{
List<TblAppointmentType> info = tblAppointmentTypeService.vaguequeryFun(tblAppointmentType);
// 查询成功,返回成功信息
return new ReturnVal(Info.CODE,Info.SUCCESS,info);
}catch (Exception e){
e.printStackTrace();
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE,Info.OTHER,null);
}
}else {
// 遇到其他错误,返回失败信息
return new ReturnVal(Info.CODE,Info.OTHER,null);
}
}
}
Info类相关代码:
package com.cn.util;
public class Info {
/*
* 公共的返回信息
*/
public static final int CODE = 0; // 接收到请求返回的代码
public static final String EMPTY = "参数为空!"; // 传入参数为空返回的信息,需要传参的方法用到
public static final String SUCCESS = "OK"; // 操作成功返回的信息,每个方法都用到
public static final String ERROR = "Error"; // 操作失败返回的信息,每个方法都用到
public static final String REPEAT = "值已经存在"; // 唯一值重复返回的信息,添加和修改时验证唯一约束用到
public static final String OTHER = "未知错误"; // 其他错误返回信息,每个方法都用到
}
ReturnVal类相关代码:
package com.cn.util;
public class ReturnVal<T> {
private Integer errorCode;
private String errorMsg;
private T data;
public ReturnVal() {
}
public ReturnVal(Integer errorCode, String errorMsg, T data) {
super();
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.data = data;
}
public Integer getErrorCode() {
return errorCode;
}
public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}