【SSM整合篇】六. SSM整合增删改 基于【SSM整合篇】五
github源码(day58-ssm-crud) https://github.com/1196557363/ideaMavenProject
项目准备
- 本项目基于 【SSM整合篇】五
SSM整合篇】五. SSM整合+layui分页 基于【SSM整合篇】四 https://blog.csdn.net/TheNew_One/article/details/103904387
1. 增加
1.1 添加jqeury.js
1.2 在empPage.html上添加 连接跳转到 添加页面
<a href="emp/addEmpHtml">添加员工</a>
1.3 创建AddEmp.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<base th:href="${#request.getContextPath()+'/'}">
</head>
<body>
<form action="emp/addEmp" method="post">
<table>
<tr>
<td>empName</td>
<td><input type="text" name="empName" value="测试账号"/></td>
</tr>
<tr>
<td>job</td>
<td><input type="text" name="job" value="测试职业"/></td>
</tr>
<tr>
<td>hireDate</td>
<td><input type="text" name="hireDate" value="2020-1-10"/></td>
</tr>
<tr>
<td>super</td>
<td>
<input type="text" name="superName" id="superName"/>
<input type="hidden" name="superId" id="superId" value="" />
</td>
</tr>
<tr>
<td>deptNo</td>
<td>
<select name="deptNo">
<option value="0">---请选择---</option>
<option th:each="dept:${deptList}" th:value="${dept.deptId}" th:text="${dept.deptName}"></option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
<script src="js/jquery-1.8.0.min.js"></script>
<script>
// 页面加载的时候调用
$(function(){
// 动态给领导的输入框绑定一个失去焦点事件
$("#superName").blur(function(){
// 1.先获取用户输入的领导id
var superId = $("#superName").val();
if(superId != ''){
$.get("emp/getEmpBySuperId/"+superId,"",function(data){
console.log(data);
$("#superName").val(data.empName);
$("#superId").val(data.empId)
},"JSON");
}
})
})
</script>
</body>
</html>
1.4 写dao,mapper,service及其impl
1.4.1 Empdao
/**
* 添加Emp
* @param emp
*/
void addEmp(Emp emp);
/**
* 根据领导的Id编号查询Emp
* @param superId
* @return
*/
Emp getEmpBySuperId(Integer superId);
1.4.2 mapper
<sql id="emp_column">
(emp_name,
job,
hire_date,
super_id,
dept_no)
</sql>
<insert id="addEmp">
INSERT INTO emp
<include refid="emp_column" />
VALUES
(#{empName},#{job},#{hireDate},#{superId},#{deptNo})
</insert>
<select id="getEmpBySuperId" resultMap="empMap">
SELECT * FROM emp WHERE emp_id = #{superId}
</select>
1.4.3 service
/**
* 添加Emp
* @param emp
*/
void addEmp(Emp emp);
/**
* 根据领导的Id编号查询Emp
* @param superId
* @return
*/
Emp getEmpBySuperId(Integer superId);
1.4.4 serivceImpl
@Autowired
private IDeptService iDeptService;
public void addEmp(Emp emp) {
iEmpDao.addEmp(emp);
}
public Emp getEmpBySuperId(Integer superId) {
return iEmpDao.getEmpBySuperId(superId);
}
1.5 写controller
@RequestMapping("/addEmpHtml")
public String addEmp(ModelMap map){
List<Dept> deptList = iDeptService.getAllDept();
map.put("deptList", deptList);
return "addEmp";
}
@RequestMapping(value = "/addEmp", method = RequestMethod.POST)
public String addEmp(Emp emp){
System.out.println("------------------------" + emp);
iEmpService.addEmp(emp);
return "redirect:getEmpPage";
}
@RequestMapping(value = "/getEmpBySuperId/{superId}")
@ResponseBody
public String getEmpBySuperId(@PathVariable Integer superId){
System.out.println(iEmpService.getEmpBySuperId(superId));
return new Gson().toJson(iEmpService.getEmpBySuperId(superId));
}