SSM实现增加,修改,删除,分页功能的实现
Posted SmallCuteMonkey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM实现增加,修改,删除,分页功能的实现相关的知识,希望对你有一定的参考价值。
相关的包结构:
StudentController类:
package com.zjj.controller;
import com.github.pagehelper.PageInfo;
import com.zjj.pojo.Student;
import com.zjj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
/* @RequestMapping("list")
@ResponseBody //可以使查询的对象显示在浏览器中
public List<Student> queryALlStudents(){
List<Student> students = studentService.queryAllStudents();
return students;
}*/
@RequestMapping("stulist")
public String queryAllStudents(Student student,Model model){
List<Student> students = studentService.queryAllStudents();
model.addAttribute("stuList",students);
return "stuList";
}
// 前端通过id进行删除
@RequestMapping("/deleteById")
public String deleteByid(Integer idStu){
studentService.deleteByid(idStu);
return "redirect:stulist";
}
// 分页
@RequestMapping("listpage")
public String listAllStudents(Integer currentPage,Integer pageSize,Model model){
if (currentPage==null){
currentPage=1;
}
if (pageSize==null){
pageSize=3;
}
PageInfo<Student> page=studentService.queryStudentsByPage(currentPage,pageSize);
model.addAttribute("page",page);
return "stulistpage";
}
// 添加中转站
@RequestMapping("toadd")
public String ToaddStudent(){
return "add";
}
// 增加
@RequestMapping("/add")
public String addStudent(Student student){
studentService.addStudent(student);
return "redirect:listpage";
}
// 修改中转
@RequestMapping("load")
//注意这个 形参sid要和前端的sid保持一致 否则获取不到数据
public String ToupdateStudent(Model model,int sid){
// 通过id查询得到的数据进行相关的回显
Student student1 = studentService.queryAllStudentsByid(sid);
model.addAttribute("s",student1);
return "update";
}
@RequestMapping("update")
public String updateStudent(Student student){
boolean s = studentService.updateStudent(student);
return "redirect:listpage";
}
@RequestMapping("delete")
public String deleteByStudent(int sid){
studentService.deleteByid(sid);
return "redirect:listpage";
}
}
StudentDao类:
package com.zjj.dao;
import com.zjj.pojo.Student;
import java.util.List;
public interface StudentDao {
// 查询所有学生
public List<Student> queryAllStudents();
//id进行查询
public Student queryAllStudentsByid(int id);
public boolean addStudent(Student student);
public boolean updateStudent(Student student);
// 通过id进行相应的删除
public boolean deleteStudent(int id);
}
Student类:
package com.zjj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
Integer sid;
String sname;
Date birthday;
String address;
String photo;//头像
}
StudentService类:
package com.zjj.service;
import com.github.pagehelper.PageInfo;
import com.zjj.pojo.Student;
import java.util.List;
public interface StudentService {
public List<Student> queryAllStudents();
public Student queryAllStudentsByid(int id);
// 进行分页查询
public PageInfo<Student> queryStudentsByPage(int currentPage,int pageSize);
public void deleteByid(Integer idStu);
public boolean addStudent(Student student);
public boolean updateStudent(Student student);
public boolean deleteStudent(int id);
}
StudentServiceimpl类:
package com.zjj.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjj.dao.StudentDao;
import com.zjj.pojo.Student;
import com.zjj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class StudentServiceimpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> queryAllStudents() {
return studentDao.queryAllStudents();
}
@Override
public Student queryAllStudentsByid(int id) {
return studentDao.queryAllStudentsByid(id);
}
@Override
public PageInfo<Student> queryStudentsByPage(int currentPage, int pageSize) {
// 传入参数:当前页和每页条数
PageHelper.startPage(currentPage,pageSize);
List<Student> students = studentDao.queryAllStudents();
// 通过包装获取分页所需的其他值
PageInfo<Student> pageInfo = new PageInfo<>(students);
//
return pageInfo;
}
@Override
public void deleteByid(Integer idStu) {
studentDao.deleteStudent(idStu);
}
@Override
public boolean addStudent(Student student){
boolean row = studentDao.addStudent(student);
if(row){
return true;
}
return false;
}
// 更新学生
@Override
public boolean updateStudent(Student student) {
boolean row = studentDao.updateStudent(student);
if(row){
return true;
}
return false;
}
@Override
public boolean deleteStudent(int id) {
boolean row = studentDao.deleteStudent(id);
if(row){
return true;
}
return false;
}
}
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zjj.dao.StudentDao">
<resultMap id="studentResultMap" type="Student">
<!-- Integer sid;
String sname;
Date birthday;
String address;
String photo;//头像-->
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
<result property="photo" column="photo"/>
</resultMap>
<select id="queryAllStudents" resultMap="studentResultMap">
select *from tb_stu
</select>
<select id="queryAllStudentsByid" resultType="Student">
select *from tb_stu where sid=#{sid}
</select>
<!-- 删除Byid-->
<delete id="deleteByid" parameterType="Integer">
delete from tb_stu where id=#{id}
</delete>
<!-- 增加-->
<insert id="addStudent" parameterType="Student">
INSERT INTO tb_stu VALUES (0,#{sname},#{birthday},#{address},#{photo})
</insert>
<update id="updateStudent" parameterType="Student">
UPDATE `tb_stu` SET `sname` = #{sname}, `birthday` = #{birthday}, `address` = #{address}, `photo` = #{photo} WHERE `sid` = #{sid};
</update>
<delete id="deleteStudent" parameterType="Student">
delete from tb_stu where sid=#{sid}
</delete>
</mapper>
stulistpage.jsp:
<%--
Created by IntelliJ IDEA.
User: CourageAndLove
Date: 2021/5/17
Time: 11:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
<style>
table{
border: 1px solid black;
align:center;
}
</style>
</head>
<body>
<center>
<h2>学生管理系统</h2>
<table width="50%" height="60%" cellspacing="0">
<tr>
<td>编号</td>
<td>学生姓名 </td>
<td>学生生日</td>
<td>学生地址</td>
<td>学生照片</td>
<td><a href="toadd">添加学生信息</a></td>
</tr>
<%--这里是用集合的形式进行显示 一定为page.list--%>
<c:forEach items="${page.list}" var="s">
<tr>
<td>${s.sid}</td>
<td>${s.sname}</td>
<%-- springmvc文件中已经配置了FomattingConversion--%>
<td> <fmt:formatDate value="${s.birthday}" pattern="yyyy-MM-dd hh:mm:ss"></fmt:formatDate> </td>
<td>${s.address}</td>
<%-- 图片的显示, ${s.photo取到的是图片的路径}--%>
<td>
<img src=" ${s.photo}" width="150px" height="150px">
</td>
<%-- 通过id进行删除功能--%>
<td >
<a href="delete?sid=${s.sid}" style="color: red">删除</a>
</td>
<%-- 这个是通过id进行相关的修改,然后通过id查询进行回显--%>
<td>
<a href="load?sid=${s.sid}"> 修改</a>
</td>
</tr>
</c:forEach>
<table>
<input type="button" onclick="toFirst()" value="首页"/>
<input type="button" onclick="onPre()" value="上一页"/>
<%-- pageNum和page.pages是源码中写死的 --%>
当前页${page.pageNum}|${page.pages}总页数
<input type="button" onclick="toNext()" value="下一页"/>
<input type="button" onclick="toLast()" value="尾页"/>
每页显示 <input type="text" size="2" id="pageNo" value="${page.pageSize}"/>条数
</center>
<script>
function toFirst(){
//通过js进行获取id选择器
var pageSize=document.getElementById("pageNo").value;
location.href="listpage?currentPage=1&pageSize="+pageSize;
}
function toPre(){
var pageSize=document.getElementById("pageNo").value;
location.href="listpage?currentPage=${page.pageSize}&pageSize="+pageSize;
}
function toNext(){
var pageSize=document.getElementById("pageNo").value;
location.href="listpage?currentPage=${page.pageSize}&pageSize="+pageSize;
}
function toLast(){
var pageSize=document.getElementById("pageNo").value;
location.href="listpage?currentPage=${page.pageSize}&pageSize="+pageSize;
}
</script>
</body>
</html>
add.jsp:
<%--
Created by IntelliJ IDEA.
User: CourageAndLove
Date: 2021/5/24
Time: 9:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
bootstrap+Ajax+SSM(maven搭建)实现增删改查
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十五(实现商品页面搜索功能,以及分页功能)
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十五(实现商品页面搜索功能,以及分页功能)