前端页面
查询页面
<table border="1" >
<tr>
<td>编号</td>
<td>姓名</td>
<td>工资</td>
<td>操作</td>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.empno }</td>
<td>${emp.ename }</td>
<td>${emp.sal }</td>
<td>
<a href='https://www.ctyun.cn/portal/link.html?target=emp%2FtoAddEmp'>增加</a>
<a href='https://www.ctyun.cn/portal/link.html?target=emp%2FupdateEmp%3Fempno%3D%24%7Bemp.empno+%7D'>编辑</a>
<a href='https://www.ctyun.cn/portal/link.html?target=emp%2FdelEmp%3Fempno%3D%24%7Bemp.empno+%7D'>删除</a>
</td>
</tr>
</c:forEach>
</table>
增加页面
<form action="emp/AddEmp" method="post">
编号:<input type="text" name="empno" /><br/>
姓名:<input type="text" name="ename" /><br/>
工资:<input type="text" name="sal" /><br/>
<input type="submit" value="增加">
</form>
更新页面
<form action="emp/updateEmps" method="post">
<c:forEach items="${emps}" var="emp">
<input type="hidden" value="${emp.empno }" name="empno" />
name:<input type="text" value="${emp.ename }" name="ename" />
sal:<input type="text" value="${emp.sal }" name="sal" />
</c:forEach>
<input type="submit" value="编辑" />
</form>
数据层接口
//数据层的接口
public interface IEmpDao {
//数据层的查询方法
public List<Emp>getEmps();
//根据id查询的方法
public List<Emp>oneEmps(int n);
//增加的方法
public int addEmp(Emp e);
//删除的方法
public int delEmp(int n);
//修改的方法
public int updateEmp(Emp e);
}
mapper映射文件
<mapper namespace="aaa.dao.IEmpDao">
<select id="getEmps" resultType="aaa.entity.Emp" >
select empno,ename,sal from emp
</select>
<select id="oneEmps" resultType="aaa.entity.Emp" parameterType="int" >
select empno,ename,sal from emp where empno=#{empno}
</select>
<insert id="addEmp" parameterType="aaa.entity.Emp">
insert into emp(empno,ename,sal) values(#{empno},#{ename},#{sal})
</insert>
<delete id="delEmp" parameterType="int">
delete from emp where empno=#{empno}
</delete>
<update id="updateEmp" parameterType="aaa.entity.Emp">
update emp set ename=#{ename},sal=#{sal} where empno=#{empno}
</update>
</mapper>
业务层接口
//业务层接口
public interface IEmpService {
public List<Emp> getEmps();
//增加的方法
public int addEmp(Emp e);
//删除的方法
public int delEmp(int n);
//根据id查询的方法
public List<Emp>oneEmps(int n);
//修改的方法
public int updateEmp(Emp e);
}
业务层实现类
package aaa.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import aaa.dao.IEmpDao;
import aaa.entity.Emp;
import aaa.service.IEmpService;
@Component
public class EmpService implements IEmpService {
@Resource
private IEmpDao empDao;
public List<Emp> getEmps() {
return empDao.getEmps();
}
public int addEmp(Emp e) {
return empDao.addEmp(e);
}
public int delEmp(int n) {
return empDao.delEmp(n);
}
public List<Emp> oneEmps(int n) {
return empDao.oneEmps(n);
}
public int updateEmp(Emp e) {
return empDao.updateEmp(e);
}
}
控制器代码
package aaa.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import aaa.entity.Emp;
import aaa.service.IEmpService;
@Controller
@RequestMapping("/emp")
public class EmpController {
//byName注入
@Resource
private IEmpService service;
@RequestMapping("/list")
public String getList(Model model){
List<Emp> emps = service.getEmps();
model.addAttribute("emps",emps);
return "list";
}
@RequestMapping("/toAddEmp")
public String toAddEmp(){
return "AddEmp";
}
@RequestMapping("/AddEmp")
public String AddEmp(Emp e){
service.addEmp(e);
return "redirect:list";
}
@RequestMapping("/delEmp")
public String delEmp(HttpServletRequest req){
int n = Integer.parseInt(req.getParameter("empno"));
service.delEmp(n);
return "redirect:list";
}
@RequestMapping("/updateEmp")
public String updateEmp(HttpServletRequest req,Model model){
int n = Integer.parseInt(req.getParameter("empno"));
List<Emp> emps = service.oneEmps(n);
model.addAttribute("emps",emps);
return "update";
}
@RequestMapping("/updateEmps")
public String updateEmps(Emp e){
service.updateEmp(e);
return "redirect:list";
}
}