代码如下:
@Component
public class Employee {
private Long id; //员工id
private String empId; //员工编号
private String empName; //员工姓名
private int sex; //员工性别
.................................
@Repository
public interface EmployeeDao {
public int add(Employee employee);
public int edit(Employee employee);
public int delete(String ids);
public List<Employee> findList(Map<String, Object> queryMap);
public int getTotal(Map<String, Object> queryMap);
}
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(Employee employee){
Map<String, String> ret = new HashMap<String, String>();
if(employee == null) {
ret.put("type", "errror");
ret.put("msg", "请正确输入添加员工的相关信息!");
}
if (StringUtils.isEmpty(employee.getEmpId())) {
ret.put("type", "error");
ret.put("msg", "请填写员工编号!");
}
if(StringUtils.isEmpty(employee.getEmpName())) {
ret.put("type", "error");
ret.put("msg", "请填写员工姓名!");
}
if(StringUtils.isEmpty(employee.getSex())) {
ret.put("type", "error");
ret.put("msg", "请选择员工性别");
}
ret.put("type", "success");
ret.put("msg", "员工信息登记成功!");
return ret;
}
}
ajax提交成功
function openAdd(){
//$('#add-form').form('clear');
$('#add-dialog').dialog({
closed: false,
modal:true,
title: "添加部门信息",
buttons: [{
text: '确定',
iconCls: 'icon-ok',
handler: add
}, {
text: '取消',
iconCls: 'icon-cancel',
handler: function () {
$('#add-dialog').dialog('close');
}
}],
onBeforeOpen:function(){
$("#add-form input").val('');
}
});
}
function add(){
var validate = $("#add-form").form("validate");
if(!validate){
$.messager.<span>alert</span>("消息提醒","请检查你输入的数据!","warning");
return;
}
var data = $("#add-form").serialize();
console.log(data);
console.log(data);
$.ajax({
url:'add',
dataType:'json',
type:'post',
data:data,
success:function(data){
if(data.type == 'success'){
$.messager.<span>alert</span>('信息提示','添加成功!','info');
//$("#add-content").val('');
$('#add-dialog').dialog('close');
$('#data-datagrid').datagrid('reload');
}else{
$.messager.<span>alert</span>('信息提示',data.msg,'warning');
}
}
});
}
mapper 如下:
<mapper namespace="com.ischoolbar.programmer.dao.admin.EmployeeDao">
<!-- 添加员工 -->
<insert id="add" parameterType="Employee">
insert into emp(id,empId,empName,sex) values(null,#{empId},#{empName},#{sex})
</insert>
<!-- 分页获取员工列表 -->
<select id="findList" parameterType="Map" resultType="Employee">
select * from emp
<if test="empName != null">
where empName like '%${empName}%'
</if>
<if test="offset != null and pageSize != null">
limit #{offset},#{pageSize}
</if>
</select>
<!-- 获取符合结果的总记录数 -->
<select id="getTotal" parameterType="Map" resultType="Integer">
select count(*) from emp
<if test="empName != null">
where empName like '%${empName}%'
</if>
</select>
</mapper>