ssm整合2 增删改
生活随笔
收集整理的這篇文章主要介紹了
ssm整合2 增删改
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前端頁面
查詢頁面
?
<table border="1" style="color:red"><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='emp/toAddEmp'>增加</a><a href='emp/updateEmp?empno=${emp.empno }'>編輯</a><a href='emp/delEmp?empno=${emp.empno }'>刪除</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 {@Resourceprivate 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注入@Resourceprivate 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"; }}?
?
?
?
?
?
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ssm整合2 增删改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高薪面试系列一.HR
- 下一篇: 软件设计文档国家标准_GB8567--8