SSM框架實現增刪改查(CRUD)
阿新 • • 發佈:2019-02-06
2:步驟說明
2.1:StudentService新增相應的方法
public interface StudentService { List<Student> list(); int total(); List<Student> list(Page page); //增 public void add(Student student); //刪 public void delete(Student student); //改 public void update(Student student); //查 public Student get(int id); }
2.2:Studentimpl實現(自然而然我們想到在Mapper中新增相應的方法)
package com.ccut.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ccut.mapper.StudentMapper; import com.ccut.page.Page; import com.ccut.pojo.Student; import com.ccut.service.StudentService; @Service public class StudentServiceImpl implements StudentService { @Autowired StudentMapper studentMapper; @Override public List<Student> list() { return studentMapper.list(); } @Override public int total() { return studentMapper.total(); } @Override public List<Student> list(Page page) { return studentMapper.list(page); } @Override public void add(Student student) { studentMapper.add(student); } @Override public void delete(Student student) { studentMapper.delete(student); } @Override public void update(Student student) { studentMapper.update(student); } @Override public Student get(int id) { Student student=new Student(); student.setId(id); return studentMapper.get(student); } }
2.3:studentMapper新增相應方法
package com.ccut.mapper; import java.util.List; import com.ccut.page.Page; import com.ccut.pojo.Student; public interface StudentMapper { public List<Student> list(); //分頁查詢 public List<Student> list(Page page); public int total(); //增加 public void add(Student student); //刪 public void delete(Student student); //改 public void update(Student student); //查 public Student get(Student student); }
2.4:studentMapper.xml新增相應sql語句(改我們先通過get方法獲得,然後通過update去修改)
<insert id="add" parameterType="Student">
insert into student(name)
values(#{name})
</insert>
<delete id="delete" parameterType="Student">
delete from student where id=#{id}
</delete>
<update id="update" parameterType="Student">
update student set name=#{name} where id=#{id}
</update>
<select id="get" parameterType="Student" resultType="Student">
select * from student where id=#{id}
</select>
2.5:我們寫Controller
// 增加
@RequestMapping("addStudent")
public ModelAndView addStudent(Student student) {
studentService.add(student);
ModelAndView mav = new ModelAndView("redirect:/listStudentBypage");
return mav;
}
// 刪除
@RequestMapping("deleteStudent")
public ModelAndView deleteStudent(Student student) {
studentService.delete(student);
ModelAndView mav = new ModelAndView("redirect:/listStudentBypage");
return mav;
}
//修改,先通過get方法得到一個物件
@RequestMapping("editStudent")
public ModelAndView editStudent(Student student) {
Student st = studentService.get(student.getId());
System.out.println(st.toString());
ModelAndView mav = new ModelAndView("editStudent");
mav.addObject("c",st);
return mav;
}
// 修改
@RequestMapping("updateStudent")
public ModelAndView updateStudent(Student student) {
studentService.update(student);
ModelAndView mav = new ModelAndView("redirect:/listStudentBypage");
return mav;
}
2.6:listStudent.jsp頁面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table align="center" border="1" cellspacing="0">
<tr>
<td>id</td>
<td>name</td>
<td>編輯</td>
<td>刪除</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td><a href="editStudent?id=${c.id}">編輯</a></td>
<td><a href="deleteStudent?id=${c.id}">刪除</a></td>
</tr>
</c:forEach>
</table>
<div style="text-align:center">
<a href="?start=0">首頁</a>
<a href="?start=${page.start-page.count<0?0:page.start-page.count}">上一頁</a>
<a href="?start=${page.start+page.count>page.end?page.end:page.start+page.count }">下一頁</a>
<a href="?start=${page.end}">末頁</a>
</div>
<div style="text-align:center;margin-top:40px">
<form method="post" action="addStudent">
分類名稱: <input name="name" value="" type="text"> <br><br>
<input type="submit" value="增加分類">
</form>
</div>
</body>
</html>
2.7:editStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div style="width:500px;margin:0px auto;text-align:center">
<div style="text-align:center;margin-top:40px">
<form method="post" action="updateStudent">
分類名稱: <input name="name" value="${c.name}" type="text"> <br><br>
<input type="hidden" value="${c.id}" name="id">
<input type="submit" value="增加分類">
</form>
</div>
</div>
2.8: 截圖
2.9:流程分析
1:增刪我們不用說了,就是在Controller呼叫相應方法完成
2:這個更新我們說一下,我們點選某一欄,我們傳的是這一欄的id
<td><a href="editStudent?id=${c.id}">編輯</a></td>
到我們的Controller中去
通過
Student st = studentService.get(student.getId());
System.out.println(st.toString());
ModelAndView mav = new ModelAndView("editStudent");
mav.addObject("c",st);
將這個Id內容的資訊找出來,然後轉到editStudent.jsp裡面.效果如圖
然後點選增加分類,將該的值傳到Controller中,完成修改,重定向到listStudentByPage.jsp整體完成
// 修改
@RequestMapping("updateStudent")
public ModelAndView updateStudent(Student student) {
studentService.update(student);
ModelAndView mav = new ModelAndView("redirect:/listStudentBypage");
return mav;
}