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 {
	@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"; 
	}
	
	
	
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

teayear

读后有收获可以获取更多资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值