后端结构:com.lm.ssm.action->EmpAction.java
com.lm.ssm.dao->EmpDao.java
com.lm.ssm.service->EmpService.java
com.lm.ssm.vo->Emp.java
EmpMapper.xml
1)vo层代码
Emp.java
private Integer id; private String name; private Double sal; private String sex; private Date date;
无参和有参构造函数,set/get省略
EmpMapper.xml
<mapper namespace="com.lm.ssm.dao.EmpDao"> <resultMap type="com.lm.ssm.vo.Emp" id="empMap"> <id property="id" column="eid" jdbcType="INTEGER"/> <result property="name" column="ename" jdbcType="VARCHAR"/> <result property="sal" column="esal" jdbcType="DOUBLE"/> <result property="sex" column="esex" jdbcType="VARCHAR"/> <result property="date" column="date" jdbcType="TIMESTAMP"/> </resultMap> <sql id="Base_Column_List"> eid,ename,esal,esex,date </sql> <!-- 增加员工 --> <insert id="add" parameterType="com.lm.ssm.vo.Emp" > insert into emps(eid,ename,esal,esex,date) values(#{id},#{name},#{sal},#{sex},#{date}) </insert> <!-- 查询所有 --> <select id="listAll" parameterType="java.util.HashMap" resultMap="empMap" > SELECT * <!-- <include refid="Base_Column_List"/> --> from emps </select> <!-- 查询id --> <select id="findById" parameterType="java.lang.Integer" resultMap="empMap"> SELECT <include refid="Base_Column_List"/> from emps where eid=#{id,jdbcType=INTEGER} </select> <update id="update" parameterType="com.lm.ssm.vo.Emp"> UPDATE emps <set> <if test="name != null"> ename=#{name,jdbcType=VARCHAR}, </if> <if test="sal != null"> esal=#{sal,jdbcType=DOUBLE}, </if> <if test="sex != null"> esex=#{sex,jdbcType=VARCHAR}, </if> <if test="date != null"> date=#{date,jdbcType=TIMESTAMP}, </if> </set> WHERE eid=#{id,jdbcType=INTEGER} </update> <delete id="delete" parameterType="com.lm.ssm.vo.Emp"> DELETE FROM emps WHERE eid=#{id,jdbcType=INTEGER} </delete> </mapper>
2)dao层代码 EmpDao.java public void add(Emp emp); public List<Emp> listAll(); public Emp findById(Integer id); public void update(Emp emp); public void delete(Integer id);
3)service层代码 EmpService.java
@Component public class EmpService { @Autowired private EmpDao empDao; /** * 注册员工 */ @Transactional public void register(Emp emp) throws Exception{ empDao.add(emp); } public List<Emp> listAll(){ return empDao.listAll(); } public Emp findById(Integer id){ return empDao.findById(id); } @Transactional public void update(Emp emp){ empDao.update(emp); } @Transactional public void delete(Integer id){ empDao.delete(id); } } 4)action代码EmpAction.java
@Component @RequestMapping(value="/emp") public class EmpAction { @Autowired private EmpService empService; @RequestMapping(value="/emps",method=RequestMethod.POST) public String registerMethod(Emp emp) throws Exception{ emp.setDate(new Timestamp(new Date().getTime())); empService.register(emp); return "redirect:/emp/emps"; } //forward:/ @RequestMapping(value="/emps",method=RequestMethod.GET) public String lists(Map<String,List<Emp>> map){ List<Emp> listAll = empService.listAll(); map.put("listAll", listAll); return "list"; } @RequestMapping(value="/emps/{id}",method=RequestMethod.GET) public String updatelist(@PathVariable("id") Integer id,Map<String,Object> map){ Emp findById = empService.findById(id); map.put("emps", findById); return "updatelist"; } @RequestMapping(value="/emps",method=RequestMethod.PUT) public String updatesave(Emp emp){ try { empService.update(emp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "redirect:/emp/emps"; } @RequestMapping(value="/emps/{id}",method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id){ empService.delete(id); return "redirect:/emp/emp1"; } } 未完待续!------------------------------------------------------------------------------------------------