• SpringMVC之CRUD


    目录

    SpringMVC实现增删改查

    1. 导入相关依赖

     2. 添加配置文件

    3. 逆向生成对应的类

    4. 后台代码编写

     5. 前台代码编写


    SpringMVC实现增删改查

    1. 导入相关依赖

    pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. org.example
    6. xisslzy_ssm
    7. 1.0-SNAPSHOT
    8. war
    9. xisslzy_ssm Maven Webapp
    10. http://www.example.com
    11. UTF-8
    12. 1.8
    13. 1.8
    14. 3.7.0
    15. 5.0.2.RELEASE
    16. 3.4.5
    17. 5.1.44
    18. 5.1.2
    19. 1.3.1
    20. 2.1.1
    21. 2.4.3
    22. 2.9.1
    23. 4.12
    24. 4.0.0
    25. 1.18.2
    26. org.springframework
    27. spring-context
    28. ${spring.version}
    29. org.springframework
    30. spring-orm
    31. ${spring.version}
    32. org.springframework
    33. spring-tx
    34. ${spring.version}
    35. org.springframework
    36. spring-aspects
    37. ${spring.version}
    38. org.springframework
    39. spring-web
    40. ${spring.version}
    41. org.springframework
    42. spring-test
    43. ${spring.version}
    44. org.mybatis
    45. mybatis
    46. ${mybatis.version}
    47. mysql
    48. mysql-connector-java
    49. ${mysql.version}
    50. com.github.pagehelper
    51. pagehelper
    52. ${pagehelper.version}
    53. org.mybatis
    54. mybatis-spring
    55. ${mybatis.spring.version}
    56. org.apache.commons
    57. commons-dbcp2
    58. ${commons.dbcp2.version}
    59. org.apache.commons
    60. commons-pool2
    61. ${commons.pool2.version}
    62. org.apache.logging.log4j
    63. log4j-core
    64. ${log4j2.version}
    65. org.apache.logging.log4j
    66. log4j-api
    67. ${log4j2.version}
    68. org.apache.logging.log4j
    69. log4j-web
    70. ${log4j2.version}
    71. junit
    72. junit
    73. ${junit.version}
    74. javax.servlet
    75. javax.servlet-api
    76. ${servlet.version}
    77. provided
    78. org.projectlombok
    79. lombok
    80. ${lombok.version}
    81. provided
    82. org.springframework
    83. spring-webmvc
    84. ${spring.version}
    85. javax.servlet.jsp
    86. javax.servlet.jsp-api
    87. 2.3.3
    88. jstl
    89. jstl
    90. 1.2
    91. taglibs
    92. standard
    93. 1.1.2
    94. xisslzy_ssm
    95. src/main/java
    96. **/*.xml
    97. src/main/resources
    98. jdbc.properties
    99. *.xml
    100. org.mybatis.generator
    101. mybatis-generator-maven-plugin
    102. 1.3.2
    103. mysql
    104. mysql-connector-java
    105. 5.1.44
    106. true
    107. maven-clean-plugin
    108. 3.1.0
    109. maven-resources-plugin
    110. 3.0.2
    111. maven-compiler-plugin
    112. 3.8.0
    113. maven-surefire-plugin
    114. 2.22.1
    115. maven-war-plugin
    116. 3.2.2
    117. maven-install-plugin
    118. 2.5.2
    119. maven-deploy-plugin
    120. 2.8.2

    web.xml 

    1. "1.0" encoding="UTF-8"?>
    2. "http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    5. version="3.1">
    6. Archetype Created Web Application
    7. contextConfigLocation
    8. classpath:spring-context.xml
    9. org.springframework.web.context.ContextLoaderListener
    10. encodingFilter
    11. org.springframework.web.filter.CharacterEncodingFilter
    12. true
    13. encoding
    14. UTF-8
    15. encodingFilter
    16. /*
    17. SpringMVC
    18. org.springframework.web.servlet.DispatcherServlet
    19. contextConfigLocation
    20. classpath:spring-mvc.xml
    21. 1
    22. true
    23. SpringMVC
    24. /

     2. 添加配置文件

    jdbc.properties

    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
    3. jdbc.username=root
    4. jdbc.password=123456

     generatorConfig.xml

    1. "1.0" encoding="UTF-8" ?>
    2. "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
    4. "jdbc.properties"/>
    5. "D:\\maven\\mvn_repository\\mysql\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
    6. "infoGuardian">
    7. "suppressAllComments" value="true"/>
    8. "suppressDate" value="true"/>
    9. "${jdbc.driver}"
    10. connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
    11. "forceBigDecimals" value="false"/>
    12. "com.xissl.model"
    13. targetProject="src/main/java">
    14. "enableSubPackages" value="false"/>
    15. "constructorBased" value="true"/>
    16. "trimStrings" value="false"/>
    17. "immutable" value="false"/>
    18. "com.xissl.mapper"
    19. targetProject="src/main/java">
    20. "enableSubPackages" value="false"/>
    21. "com.xissl.mapper"
    22. targetProject="src/main/java" type="XMLMAPPER">
    23. "enableSubPackages" value="false"/>
    24. "" tableName="t_mysql_student" domainObjectName="Student"
    25. enableCountByExample="false" enableDeleteByExample="false"
    26. enableSelectByExample="false" enableUpdateByExample="false">

    spring-mvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:mvc="http://www.springframework.org/schema/mvc"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    10. package="com.xissl"/>
    11. "org.springframework.web.servlet.view.InternalResourceViewResolver">
    12. "viewClass"
    13. value="org.springframework.web.servlet.view.JstlView">
    14. "prefix" value="/WEB-INF/jsp/"/>
    15. "suffix" value=".jsp"/>

    3. 逆向生成对应的类

    实体类

    1. package com.xissl.model;
    2. import lombok.ToString;
    3. @ToString
    4. public class Student {
    5. private String sid;
    6. private String sname;
    7. private String sage;
    8. private String ssex;
    9. public Student(String sid, String sname, String sage, String ssex) {
    10. this.sid = sid;
    11. this.sname = sname;
    12. this.sage = sage;
    13. this.ssex = ssex;
    14. }
    15. public Student() {
    16. super();
    17. }
    18. public String getSid() {
    19. return sid;
    20. }
    21. public void setSid(String sid) {
    22. this.sid = sid;
    23. }
    24. public String getSname() {
    25. return sname;
    26. }
    27. public void setSname(String sname) {
    28. this.sname = sname;
    29. }
    30. public String getSage() {
    31. return sage;
    32. }
    33. public void setSage(String sage) {
    34. this.sage = sage;
    35. }
    36. public String getSsex() {
    37. return ssex;
    38. }
    39. public void setSsex(String ssex) {
    40. this.ssex = ssex;
    41. }
    42. }

     mapper接口

    1. package com.xissl.mapper;
    2. import com.xissl.model.Student;
    3. import org.springframework.stereotype.Repository;
    4. import java.util.List;
    5. @Repository
    6. public interface StudentMapper {
    7. int deleteByPrimaryKey(String sid);
    8. int insert(Student record);
    9. int insertSelective(Student record);
    10. Student selectByPrimaryKey(String sid);
    11. int updateByPrimaryKeySelective(Student record);
    12. int updateByPrimaryKey(Student record);
    13. List listPager(Student student);
    14. }

    sql映射文件

    1. "1.0" encoding="UTF-8" ?>
    2. "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. "com.xissl.mapper.StudentMapper" >
    4. "BaseResultMap" type="com.xissl.model.Student" >
    5. "sid" jdbcType="VARCHAR" javaType="java.lang.String" />
    6. "sname" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. "sage" jdbcType="VARCHAR" javaType="java.lang.String" />
    8. "ssex" jdbcType="VARCHAR" javaType="java.lang.String" />
    9. "Base_Column_List" >
    10. sid, sname, sage, ssex
    11. "deleteByPrimaryKey" parameterType="java.lang.String" >
    12. delete from t_mysql_student
    13. where sid = #{sid,jdbcType=VARCHAR}
    14. "insert" parameterType="com.xissl.model.Student" >
    15. insert into t_mysql_student (sid, sname, sage,
    16. ssex)
    17. values (#{sid,jdbcType=VARCHAR}, #{sname,jdbcType=VARCHAR}, #{sage,jdbcType=VARCHAR},
    18. #{ssex,jdbcType=VARCHAR})
    19. "insertSelective" parameterType="com.xissl.model.Student" >
    20. insert into t_mysql_student
    21. "(" suffix=")" suffixOverrides="," >
    22. <if test="sid != null" >
    23. sid,
    24. if>
    25. <if test="sname != null" >
    26. sname,
    27. if>
    28. <if test="sage != null" >
    29. sage,
    30. if>
    31. <if test="ssex != null" >
    32. ssex,
    33. if>
    34. "values (" suffix=")" suffixOverrides="," >
    35. <if test="sid != null" >
    36. #{sid,jdbcType=VARCHAR},
    37. if>
    38. <if test="sname != null" >
    39. #{sname,jdbcType=VARCHAR},
    40. if>
    41. <if test="sage != null" >
    42. #{sage,jdbcType=VARCHAR},
    43. if>
    44. <if test="ssex != null" >
    45. #{ssex,jdbcType=VARCHAR},
    46. if>
    47. "updateByPrimaryKeySelective" parameterType="com.xissl.model.Student" >
    48. update t_mysql_student
    49. <if test="sname != null" >
    50. sname = #{sname,jdbcType=VARCHAR},
    51. if>
    52. <if test="sage != null" >
    53. sage = #{sage,jdbcType=VARCHAR},
    54. if>
    55. <if test="ssex != null" >
    56. ssex = #{ssex,jdbcType=VARCHAR},
    57. if>
    58. where sid = #{sid,jdbcType=VARCHAR}
    59. "updateByPrimaryKey" parameterType="com.xissl.model.Student" >
    60. update t_mysql_student
    61. set sname = #{sname,jdbcType=VARCHAR},
    62. sage = #{sage,jdbcType=VARCHAR},
    63. ssex = #{ssex,jdbcType=VARCHAR}
    64. where sid = #{sid,jdbcType=VARCHAR}

    4. 后台代码编写

    业务逻辑层

    1. package com.xissl.biz;
    2. import com.xissl.model.Student;
    3. import com.xissl.utils.PageBean;
    4. import java.util.List;
    5. public interface StudentBiz {
    6. int deleteByPrimaryKey(String sid);
    7. int insert(Student record);
    8. int insertSelective(Student record);
    9. Student selectByPrimaryKey(String sid);
    10. int updateByPrimaryKeySelective(Student record);
    11. int updateByPrimaryKey(Student record);
    12. List listPager(Student student, PageBean pageBean);
    13. }

    实现接口

    1. package com.xissl.biz.impl;
    2. import com.xissl.biz.StudentBiz;
    3. import com.xissl.mapper.StudentMapper;
    4. import com.xissl.model.Student;
    5. import com.xissl.utils.PageBean;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. import java.util.List;
    9. /**
    10. * @author xissl
    11. * @create 2023-09-07 16:07
    12. */
    13. @Service
    14. public class StudentBizImpl implements StudentBiz {
    15. @Autowired
    16. private StudentMapper studentMapper;
    17. @Override
    18. public int deleteByPrimaryKey(String sid) {
    19. return studentMapper.deleteByPrimaryKey(sid);
    20. }
    21. @Override
    22. public int insert(Student record) {
    23. return studentMapper.insert(record);
    24. }
    25. @Override
    26. public int insertSelective(Student record) {
    27. return studentMapper.insertSelective(record);
    28. }
    29. @Override
    30. public Student selectByPrimaryKey(String sid) {
    31. return studentMapper.selectByPrimaryKey(sid);
    32. }
    33. @Override
    34. public int updateByPrimaryKeySelective(Student record) {
    35. return studentMapper.updateByPrimaryKeySelective(record);
    36. }
    37. @Override
    38. public int updateByPrimaryKey(Student record) {
    39. return studentMapper.updateByPrimaryKey(record);
    40. }
    41. @Override
    42. public List listPager(Student student, PageBean pageBean) {
    43. return studentMapper.listPager(student);
    44. }
    45. }

     controller层

    1. package com.xissl.web;
    2. import com.xissl.biz.StudentBiz;
    3. import com.xissl.model.Student;
    4. import com.xissl.utils.PageBean;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Controller;
    8. import org.springframework.ui.Model;
    9. import org.springframework.web.bind.annotation.PathVariable;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import javax.servlet.http.HttpServletRequest;
    12. import java.util.List;
    13. /**
    14. * @author xissl
    15. * @create 2023-09-07 16:59
    16. */
    17. @Controller
    18. @RequestMapping("/stu")
    19. public class StudentController {
    20. @Autowired
    21. private StudentBiz studentBiz;
    22. // 增
    23. @RequestMapping("/add")
    24. public String add(Student student){
    25. int i = studentBiz.insertSelective(student);
    26. return "redirect:list";
    27. }
    28. // 删
    29. @RequestMapping("/del")
    30. public String del(String sid){
    31. studentBiz.deleteByPrimaryKey(sid);
    32. return "redirect:/stu/list";
    33. }
    34. // 改
    35. @RequestMapping("/edit")
    36. public String edit(Student student){
    37. studentBiz.updateByPrimaryKey(student);
    38. return "redirect:list";
    39. }
    40. // 查
    41. @RequestMapping("/list")
    42. public String list(Student student, HttpServletRequest request){
    43. // Student student是前台用来给后台传递参数的
    44. PageBean pageBean = new PageBean();
    45. pageBean.setRequest(request);
    46. List students = studentBiz.listPager(student, pageBean);
    47. request.setAttribute("list",students);
    48. request.setAttribute("pageBean",pageBean);
    49. return "stu/list";
    50. }
    51. // 数据回显
    52. @RequestMapping("/preSave")
    53. public String preSave(Student student, Model model){
    54. if(student != null && student.getSid()!=null){
    55. Student s = studentBiz.selectByPrimaryKey(student.getSid());
    56. model.addAttribute("s",s);
    57. }
    58. return "stu/edit";
    59. }
    60. }

     5. 前台代码编写

    list.jsp

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. <%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
    4. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    5. "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    6. "Content-Type" content="text/html; charset=UTF-8">
    7. href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
    8. rel="stylesheet">
    9. src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js">
    10. 博客列表
    11. "form-inline"
    12. action="${pageContext.request.contextPath }/stu/list" method="post">
    13. "form-group mb-2">
    14. "text" class="form-control-plaintext" name="sname"
    15. placeholder="请输入学生姓名">
    16. <%-- "pagination" value="false" type="hidden">--%>
  • "table table-striped">
  • var="b" items="${list }">
  • "col">学生编号"col">学生姓名"col">学生年龄"col">学生性别"col">操作
    ${b.sid }${b.sname }${b.sage }${b.ssex }
  • "${pageBean }">
  • edit.jsp 

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. Title
    3. "${pageContext.request.contextPath }/${empty s ? 'stu/add' : 'stu/edit'}" method="post">
    4. 学生编号:"text" name="sid" value="${s.sid }">
    5. 学生姓名:"text" name="sname" value="${s.sname }">
    6. 学生年龄:"text" name="sage" value="${s.sage }">
    7. 学生性别:"text" name="ssex" value="${s.ssex }">
    8. "submit">

  • 相关阅读:
    MapStruct的使用
    C. Target Practice
    电脑多位置同时输入方法
    这几个 Python 小游戏,上班摸鱼我能玩一天 | 内附源码
    深度学习之目标检测模型后处理(非极大值抑制与WBF)
    MySQL之事务、存储引擎、索引
    《MLB棒球创造营》:走近棒球运动·旧金山巨人队
    怎么判断MES系统好不好?MES又是如何帮企业省钱的?
    Linux 安装 CenOS7 MySQL - 8.0.26
    Hexagon_V65_Programmers_Reference_Manual(7)
  • 原文地址:https://blog.csdn.net/lijie1025/article/details/132742543