新增-员工的添加
新增-逻辑
在index.jsp页面点击“新增”
弹出新增对话框
去数据库查询部门列表,显示在对话框中
用户输入数据,并进行校验
jquery前端校验,ajax用户名重复校验,重要数据(后端校验(JSR303),唯一约束)
完成保存
URI:
/emp/{id}GET 查询员工
/empPOST 保存员工
/emp/{id}PUT 修改员工
/emp/{id}DELETE删除员工
在controller新增DepartmentController.java
package com.liantao.crud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liantao.crud.bean.Department;
import com.liantao.crud.bean.Msg;
import com.liantao.crud.service.DepartmentService;
/**
* 处理和部门有关的请求
* @author liantao.me
*
*/
@Controller
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
/**
* 返回所有的部门信息
*/
@RequestMapping("/depts")
@ResponseBody
public Msg getDepts(){
//查出的所有部门信息
List list = departmentService.getDepts();
return Msg.success().add("depts", list);
}
}
在service 新增DepartmentService.java
package com.liantao.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liantao.crud.bean.Department;
import com.liantao.crud.dao.DepartmentMapper;
@Service
public class DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
public List getDepts() {
// TODO Auto-generated method stub
List list = departmentMapper.selectByExample(null);
return list;
}
}
修改EmployeeController.java,(添加JSR303校验,添加判断新增的用户名是否可用的方法)
pom.xml添加:
org.hibernate
hibernate-validator
5.4.1.Final
EmployeeController.java 修改
package com.liantao.crud.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liantao.crud.bean.Employee;
import com.liantao.crud.bean.Msg;
import com.liantao.crud.service.EmployeeService;
/**
* 处理员工的crud请求
* @author liantao.me
*
*/
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
/**
* 检查用户名是否可用
* @param empName
* @return
*/
@ResponseBody
@RequestMapping("/checkuser")
public Msg checkuser(@RequestParam("empName")String empName){
//先判断用户名是否是合法的表达式
String regx = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\u2E80-\u9FFF]{2,5})";
if(!empName.matches(regx)){
return Msg.fail().add("va_msg", "java:用户名为2-5位中文或者6-16位英文和数字的组合");
}
//数据库用户名重复校验
boolean b = employeeService.checkUser(empName);
if(b){
return Msg.success();//code 100
}else
{
return Msg.fail().add("va_msg", "用户名不可用");//code 200
}
}
/**
* 员工保存<