解决:用户名查重

1.jsp

lay-verify="required|checkName" placeholder="请输入部门名称" autocomplete="off" class="layui-input">

2.js

//用户名查重
form.verify({
    checkName:function(value, item) {
        var html=$.ajax({
            url:"/department/checkName",
            data:{"name":value,"id":$("input[name='id']").val()},
            async:false
        }).responseText;
        if(html=='false'){
            return "用户名重复";
        }
    }
});

3.Controller

//查名字
@RequestMapping("/checkName")
@ResponseBody
public Boolean checkName(String name,Long id){
    try {
        if (id==null){
            Department department = departmentService.findByUsername(name);
            return department==null;
        }else {
            Department department = departmentService.findOne(id);
            if (department.getName().equals(name)){
                return true;
            }else {
                department = departmentService.findByUsername(name);
                return department==null;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

4.Service

接口

//验证名字
Department findByUsername(String name);

实现

@Autowired
private DepartmentMapper departmentMapper;

@Override
public Department findByUsername(String name) {
    return departmentMapper.findByUsername(name);
}

5.mapeer

//验证名字
Department findByUsername(String name);

6.Mapper.xml


 

你可能感兴趣的:(项目)