springboot 使用session实现一个简单的部门管理系统
程序员文章站
2024-03-23 22:14:46
...
系统描述
具体实现
Department类代码
其中springboot通过注解的方式实现对参数的验证并用拦截器拦截抛出自定义json的细节可以参考这篇
https://blog.csdn.net/qq_42590163/article/details/104566607
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Department{
public interface Add extends Default {
}
public interface Select extends Default {
}
@NotEmpty(message = "id不能为空", groups = Select.class)
@Null(message = "id必须为空", groups = Add.class)
private String id;
@NotEmpty(message = "部门名字不能为空")
private String departmentName;
@NotEmpty(message = "部门负责人不能为空")
private String leader;
@NotEmpty(message = "上级部门不能为空")
private String higherDepartmentID;
@Null(message = "时间必须为空")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date create_time;
@Null(message = "时间必须为空")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date update_time;
}
DepartmentController代码
@RestController
public class DepartmentController {
@PostMapping("/addDepartment")
public JSONResult addDepartment(@Validated(value = Department.Add.class) @RequestBody Department department,
HttpSession session) {
Date time = new Date();
Department department1 = new Department("1","最大的部门!","me!","0",time,time);
session.setAttribute("最大的部门!",department1);
department.setId(String.valueOf(UUID.randomUUID()));
department.setCreate_time(time);
department.setUpdate_time(time);
String sessionKey = department.getDepartmentName() + department.getLeader();
if (!queryDepartmentIsHave(session,sessionKey)) {
return JSONResult.errorMsg("部门已存在");
}
session.setAttribute(sessionKey, department);
return JSONResult.ok("部门创建成功");
}
@PostMapping("/selectAll")
public JSONResult selectAll(HttpSession session) {
Enumeration<String> attributeNames = session.getAttributeNames();
boolean flag = true;
ArrayList<Department> departmentList = new ArrayList<>();
while (flag) {
if (!attributeNames.hasMoreElements()) {
flag = false;
}else {
Department tempDepartment = (Department)session.getAttribute(attributeNames.nextElement());
departmentList.add(tempDepartment);
}
}
return JSONResult.ok(departmentList);
}
@PostMapping("/selectByNameAndLeader")
public JSONResult selectDepartment(@Validated(value = Department.Select.class) HttpSession session,String departmentName,String leader) {
String selectParams = departmentName+leader;
Enumeration<String> attributeNames = session.getAttributeNames();
boolean flag = true;
ArrayList<Department> departmentList = new ArrayList<>();
while (flag) {
if (!attributeNames.hasMoreElements()) {
flag = false;
}else {
String nextElement = attributeNames.nextElement();
char[] selectParam = selectParams.toCharArray();
for (char selectparam:selectParam) {
String regex=".*"+selectparam+".*";
if(nextElement.matches(regex)){
Department tempDepartment = (Department)session.getAttribute(nextElement);
departmentList.add(tempDepartment);
break;
}
}
}
}
return JSONResult.ok(departmentList);
}
private boolean queryDepartmentIsHave(HttpSession session,String sessionKey) {
boolean flag = true;
Enumeration<String> attributeNames = session.getAttributeNames();
while (flag) {
if (attributeNames.nextElement().equals(sessionKey)) {
return false;
}
if (!attributeNames.hasMoreElements()) {
flag = false;
}
}
return true;
}
}
其中模糊查询的实现可以参考https://blog.csdn.net/qq_42590163/article/details/104566869
恩 具体看代码吧我相信你可以看懂的这都是很基础的东西。而且我写的也乱七八糟。。。只是能实现功能罢了。
其中如果你要在test类中测试方法的话其中session需要使用
@Autowired
MockHttpSession session;
注入session
源码下载:
链接:https://pan.baidu.com/s/1jr911qyHsPOGQYSwSMPjRA
提取码:5cte