当前位置:   article > 正文

ssm实现分页(增删改及多条件查询)_ssm实现增加,修改,删除,分页功能的实现

ssm实现增加,修改,删除,分页功能的实现

1、数据库(分别是年级表、性别表、学生表、学科表)

附上数据库代码(如下)

  1. /*
  2. SQLyog Ultimate v12.09 (64 bit)
  3. MySQL - 5.7.14-log : Database - schooldemo3
  4. *********************************************************************
  5. */
  6. /*!40101 SET NAMES utf8 */;
  7. /*!40101 SET SQL_MODE=''*/;
  8. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  9. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  10. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  11. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  12. CREATE DATABASE /*!32312 IF NOT EXISTS*/`schooldemo3` /*!40100 DEFAULT CHARACTER SET utf8 */;
  13. USE `schooldemo3`;
  14. /*Table structure for table `class` */
  15. DROP TABLE IF EXISTS `class`;
  16. CREATE TABLE `class` (
  17. `id` INT(10) NOT NULL AUTO_INCREMENT,
  18. `sid` INT(10) NOT NULL,
  19. `classes` VARCHAR(10) NOT NULL,
  20. PRIMARY KEY (`id`)
  21. ) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
  22. /*Data for the table `class` */
  23. INSERT INTO `class`(`id`,`sid`,`classes`) VALUES (1,1,'一班'),(2,2,'二班'),(3,3,'三班');
  24. /*Table structure for table `sex` */
  25. DROP TABLE IF EXISTS `sex`;
  26. CREATE TABLE `sex` (
  27. `id` INT(10) NOT NULL AUTO_INCREMENT,
  28. `sid` INT(10) NOT NULL,
  29. `sex` VARCHAR(10) NOT NULL,
  30. PRIMARY KEY (`id`)
  31. ) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  32. /*Data for the table `sex` */
  33. INSERT INTO `sex`(`id`,`sid`,`sex`) VALUES (1,1,'男'),(2,2,'女');
  34. /*Table structure for table `student` */
  35. DROP TABLE IF EXISTS `student`;
  36. CREATE TABLE `student` (
  37. `id` INT(10) NOT NULL AUTO_INCREMENT,
  38. `sname` VARCHAR(20) NOT NULL,
  39. `ssex` INT(10) NOT NULL,
  40. `sage` INT(10) NOT NULL,
  41. `sclass` INT(10) NOT NULL,
  42. `szym` VARCHAR(50) DEFAULT NULL,
  43. `ssubject` INT(10) DEFAULT NULL,
  44. PRIMARY KEY (`id`)
  45. ) ENGINE=INNODB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
  46. /*Data for the table `student` */
  47. INSERT INTO `student`(`id`,`sname`,`ssex`,`sage`,`sclass`,`szym`,`ssubject`) VALUES (1,'张三',1,16,1,'读书使我快乐',1),(2,'李四',2,15,1,'我爱读书',2),(3,'王五',1,15,2,'书中自有黄金屋',3),(4,'赵六',1,16,2,'书中自有颜如玉',2),(5,'夏七',2,15,3,'书籍是人类进步的阶梯',1),(6,'jack',1,16,3,'I like china',1),(7,'rouse',2,15,3,'study everyday',3);
  48. /*Table structure for table `subject` */
  49. DROP TABLE IF EXISTS `subject`;
  50. CREATE TABLE `subject` (
  51. `id` INT(10) NOT NULL AUTO_INCREMENT,
  52. `sid` INT(10) NOT NULL,
  53. `subject` VARCHAR(10) NOT NULL,
  54. PRIMARY KEY (`id`)
  55. ) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
  56. /*Data for the table `subject` */
  57. INSERT INTO `subject`(`id`,`sid`,`subject`) VALUES (1,1,'语文'),(2,2,'数学'),(3,3,'英语');
  58. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  59. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  60. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  61. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

 2、项目整体结构

3、导入pom.xml依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="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. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.hbt</groupId>
  6. <artifactId>SSMTest6</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>SSMTest6 Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <!--导入spring的依赖-->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-webmvc</artifactId>
  21. <version>5.2.5.RELEASE</version>
  22. </dependency>
  23. <!--Spring操作数据库的话,还需要一个spring-jdbc-->
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-jdbc</artifactId>
  27. <version>5.3.9</version>
  28. </dependency>
  29. <!--aop注入包-->
  30. <dependency>
  31. <groupId>org.aspectj</groupId>
  32. <artifactId>aspectjweaver</artifactId>
  33. <version>1.9.4</version>
  34. </dependency>
  35. <!--mybatis与spring整合的包-->
  36. <dependency>
  37. <groupId>org.mybatis</groupId>
  38. <artifactId>mybatis-spring</artifactId>
  39. <version>2.0.6</version>
  40. </dependency>
  41. <!--junit-->
  42. <dependency>
  43. <groupId>junit</groupId>
  44. <artifactId>junit</artifactId>
  45. <version>4.7</version>
  46. <scope>test</scope>
  47. </dependency>
  48. <!--mysql驱动-->
  49. <dependency>
  50. <groupId>mysql</groupId>
  51. <artifactId>mysql-connector-java</artifactId>
  52. <version>5.1.47</version>
  53. </dependency>
  54. <!--mybatis-->
  55. <dependency>
  56. <groupId>org.mybatis</groupId>
  57. <artifactId>mybatis</artifactId>
  58. <version>3.5.2</version>
  59. </dependency>
  60. <!--log4j-->
  61. <dependency>
  62. <groupId>log4j</groupId>
  63. <artifactId>log4j</artifactId>
  64. <version>1.2.12</version>
  65. </dependency>
  66. <!--Lombok-->
  67. <dependency>
  68. <groupId>org.projectlombok</groupId>
  69. <artifactId>lombok</artifactId>
  70. <version>1.18.10</version>
  71. </dependency>
  72. <!--阿里的连接池-->
  73. <dependency>
  74. <groupId>com.alibaba</groupId>
  75. <artifactId>druid</artifactId>
  76. <version>1.2.6</version>
  77. </dependency>
  78. <!--mvc-->
  79. <dependency>
  80. <groupId>javax.servlet</groupId>
  81. <artifactId>servlet-api</artifactId>
  82. <version>2.5</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>javax.servlet.jsp</groupId>
  86. <artifactId>jsp-api</artifactId>
  87. <version>2.2</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>javax.servlet</groupId>
  91. <artifactId>jstl</artifactId>
  92. <version>1.2</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>com.alibaba</groupId>
  96. <artifactId>fastjson</artifactId>
  97. <version>1.2.59</version>
  98. </dependency>
  99. <dependency>
  100. <groupId>org.springframework</groupId>
  101. <artifactId>spring-oxm</artifactId>
  102. <version>5.2.5.RELEASE</version>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.springframework</groupId>
  106. <artifactId>spring-orm</artifactId>
  107. <version>5.2.5.RELEASE</version>
  108. </dependency>
  109. <dependency>
  110. <groupId>org.springframework</groupId>
  111. <artifactId>spring-context-support</artifactId>
  112. <version>5.2.5.RELEASE</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>commons-io</groupId>
  116. <artifactId>commons-io</artifactId>
  117. <version>2.6</version>
  118. </dependency>
  119. <dependency>
  120. <groupId>commons-fileupload</groupId>
  121. <artifactId>commons-fileupload</artifactId>
  122. <version>1.4</version>
  123. </dependency>
  124. <!-- 引入JSON -->
  125. <dependency>
  126. <groupId>org.codehaus.jackson</groupId>
  127. <artifactId>jackson-core-asl</artifactId>
  128. <version>1.9.13</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>org.codehaus.jackson</groupId>
  132. <artifactId>jackson-mapper-asl</artifactId>
  133. <version>1.9.13</version>
  134. </dependency>
  135. <!-- <dependency>-->
  136. <!-- <groupId>com.fasterxml.jackson.core</groupId>-->
  137. <!-- <artifactId>jackson-databind</artifactId>-->
  138. <!-- <version>2.8.5</version>-->
  139. <!-- </dependency>-->
  140. <dependency>
  141. <groupId>com.fasterxml.jackson.core</groupId>
  142. <artifactId>jackson-core</artifactId>
  143. <version>2.9.5</version>
  144. </dependency>
  145. <dependency>
  146. <groupId>com.fasterxml.jackson.core</groupId>
  147. <artifactId>jackson-databind</artifactId>
  148. <version>2.9.5</version>
  149. </dependency>
  150. <dependency>
  151. <groupId>com.fasterxml.jackson.core</groupId>
  152. <artifactId>jackson-annotations</artifactId>
  153. <version>2.9.5</version>
  154. </dependency>
  155. <dependency>
  156. <groupId>com.fasterxml.jackson.module</groupId>
  157. <artifactId>jackson-module-jaxb-annotations</artifactId>
  158. <version>2.9.5</version>
  159. </dependency>
  160. </dependencies>
  161. </project>

 4、编写ssm配置文件

在resources下新建个配置文件database.properties,内容如下:

  1. driver=com.mysql.jdbc.Driver
  2. #在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
  3. url=jdbc:mysql://127.0.0.1:3306/schooldemo3?useUnicode=true&characterEncoding=utf-8
  4. user=root
  5. password=root

 在resources下新建个配置文件log4j.properties,内容如下:

  1. # rootLogger是所有日志的根日志,修改该日志属性将对所有日志起作用
  2. # 下面的属性配置中,所有日志的输出级别是info,输出源是con
  3. log4j.rootLogger=debugger,con
  4. # 定义输出源的输出位置是控制台
  5. log4j.appender.con=org.apache.log4j.ConsoleAppender
  6. # 定义输出日志的布局采用的类
  7. log4j.appender.con.layout=org.apache.log4j.PatternLayout
  8. # 定义日志输出布局
  9. log4j.appender.con.layout.ConversionPattern=%d{MM-dd HH:mm:ss}[%p]%c%n -%m%n

 在resources下新建个配置文件spring-dao.xml,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd">
  12. <context:property-placeholder location="classpath:database.properties"/>
  13. <!--配置数据源-->
  14. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  15. <property name="driverClassName" value="${driver}"></property>
  16. <property name="url" value="${url}"></property>
  17. <property name="username" value="${user}"></property>
  18. <property name="password" value="${password}"></property>
  19. <property name="maxActive" value="50"></property>
  20. <property name="initialSize" value="3"></property>
  21. <property name="maxWait" value="60000"></property>
  22. </bean>
  23. <!--生成session工厂-->
  24. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  25. <property name="dataSource" ref="dataSource"></property>
  26. <property name="typeAliasesPackage" value="com.hbt.entity"></property>
  27. <property name="mapperLocations">
  28. <array>
  29. <value>classpath:mapper/*Mapper.xml</value>
  30. </array>
  31. </property>
  32. </bean>
  33. <!--扫描mapper接口,代理生成mapper实例-->
  34. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  35. <property name="basePackage" value="com.hbt.dao"></property>
  36. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  37. </bean>
  38. </beans>

 在resources下新建个配置文件spring-mvc.xml,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd">
  12. <context:component-scan base-package="com.hbt"/>
  13. <!--视图解析器-->
  14. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="prefix" value="/WEB-INF/jsp/"></property>
  16. <property name="suffix" value=".jsp"></property>
  17. </bean>
  18. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
  19. </beans>

web.xml文件 

  1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  4. version="4.0">
  5. <display-name>Archetype Created Web Application</display-name>
  6. <!--welcome pages-->
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!--需要配置一个servlet springmvc的核心类,总控制器-->
  11. <servlet>
  12. <servlet-name>dispatchServlet</servlet-name>
  13. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  14. <init-param>
  15. <param-name>contextConfigLocation</param-name>
  16. <param-value>classpath:spring-mvc.xml</param-value>
  17. </init-param>
  18. <load-on-startup>1</load-on-startup>
  19. <async-supported>true</async-supported>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>dispatchServlet</servlet-name>
  23. <url-pattern>/</url-pattern>
  24. </servlet-mapping>
  25. <!-- 注册ServletContext监听器,创建容器对象,并且将ApplicationContext对象放到Application域中 -->
  26. <listener>
  27. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  28. </listener>
  29. <context-param>
  30. <param-name>contextConfigLocation</param-name>
  31. <param-value>classpath:spring-dao.xml</param-value>
  32. </context-param>
  33. <!-- 解决字符乱码问题 -->
  34. <filter>
  35. <filter-name>characterEncodingFilter</filter-name>
  36. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  37. <init-param>
  38. <param-name>encoding</param-name>
  39. <param-value>UTF-8</param-value>
  40. </init-param>
  41. <init-param>
  42. <param-name>forceEncoding</param-name>
  43. <param-value>true</param-value>
  44. </init-param>
  45. </filter>
  46. <filter-mapping>
  47. <filter-name>characterEncodingFilter</filter-name>
  48. <url-pattern>/*</url-pattern>
  49. </filter-mapping>
  50. </web-app>

iml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <module type="JAVA_MODULE" version="4">
  3. <component name="FacetManager">
  4. <facet type="web" name="Web">
  5. <configuration>
  6. <descriptors>
  7. <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
  8. </descriptors>
  9. <webroots>
  10. <root url="file://$MODULE_DIR$/web" relative="/" />
  11. </webroots>
  12. <sourceRoots>
  13. <root url="file://$MODULE_DIR$/src/main/java" />
  14. <root url="file://$MODULE_DIR$/src/main/resources" />
  15. </sourceRoots>
  16. </configuration>
  17. </facet>
  18. </component>
  19. </module>

5、entity层代码

Class实体类

  1. package com.hbt.dao;
  2. import com.hbt.entity.Sex;
  3. import com.hbt.entity.Student;
  4. import com.hbt.entity.Subject;
  5. import org.apache.ibatis.annotations.Param;
  6. import java.util.List;
  7. /**
  8. * @author houbotong
  9. * @version 0.0.3
  10. * @description StudentMapper
  11. * @since 2022-03-23 13:53
  12. */
  13. public interface StudentMapper {
  14. //下拉框加载学生性别
  15. public List<Sex> selsex();
  16. //下拉框加载学生班级
  17. public List<Class> selclass();
  18. //下拉框加载学生最喜欢学科
  19. public List<Subject> selsubject();
  20. //分页查询数据
  21. public List<Student> selAllinfo(@Param("start") Integer start,@Param("end") Integer end,
  22. @Param("sname") String sname,@Param("ssex") Integer ssex,
  23. @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
  24. //查询总条数
  25. public Integer count(@Param("sname") String sname,@Param("ssex") Integer ssex,
  26. @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
  27. //新增学生信息
  28. public void addinfo(Student student);
  29. //根据id查询出该学生信息
  30. public Student selByidinfo(Integer id);
  31. //修改学生信息
  32. public void upinfo(Student student);
  33. //删除学生信息
  34. public void delinfo(Integer id);
  35. }

Page实体类

  1. package com.hbt.entity;
  2. import java.util.List;
  3. /**
  4. * @author houbotong
  5. * @version 0.0.3
  6. * @description Page
  7. * @since 2022-03-23 14:02
  8. */
  9. public class Page {
  10. private Integer totalPage; //总页数
  11. private Integer totalNumber; //总条数
  12. private Integer pageNumber; //每页展示数据
  13. private Integer currentPage; //当前页
  14. private Integer backPage; //上一页
  15. private Integer nextPage; //下一页
  16. private List<Student> studentList;
  17. public Integer getTotalPage() {
  18. return totalPage;
  19. }
  20. public void setTotalPage(Integer totalPage) {
  21. this.totalPage = totalPage;
  22. }
  23. public Integer getTotalNumber() {
  24. return totalNumber;
  25. }
  26. public void setTotalNumber(Integer totalNumber) {
  27. this.totalNumber = totalNumber;
  28. }
  29. public Integer getPageNumber() {
  30. return pageNumber;
  31. }
  32. public void setPageNumber(Integer pageNumber) {
  33. this.pageNumber = pageNumber;
  34. }
  35. public Integer getCurrentPage() {
  36. return currentPage;
  37. }
  38. public void setCurrentPage(Integer currentPage) {
  39. this.currentPage = currentPage;
  40. }
  41. public Integer getBackPage() {
  42. return backPage;
  43. }
  44. public void setBackPage(Integer backPage) {
  45. this.backPage = backPage;
  46. }
  47. public Integer getNextPage() {
  48. return nextPage;
  49. }
  50. public void setNextPage(Integer nextPage) {
  51. this.nextPage = nextPage;
  52. }
  53. public List<Student> getStudentList() {
  54. return studentList;
  55. }
  56. public void setStudentList(List<Student> studentList) {
  57. this.studentList = studentList;
  58. }
  59. @Override
  60. public String toString() {
  61. return "Page{" +
  62. "totalPage=" + totalPage +
  63. ", totalNumber=" + totalNumber +
  64. ", pageNumber=" + pageNumber +
  65. ", currentPage=" + currentPage +
  66. ", backPage=" + backPage +
  67. ", nextPage=" + nextPage +
  68. ", studentList=" + studentList +
  69. '}';
  70. }
  71. }

Sex实体类

  1. package com.hbt.entity;
  2. /**
  3. * @author houbotong
  4. * @version 0.0.3
  5. * @description Sex
  6. * @since 2022-03-23 13:52
  7. */
  8. public class Sex {
  9. private Integer id;
  10. private Integer sid;
  11. private String sex;
  12. public Integer getId() {
  13. return id;
  14. }
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18. public Integer getSid() {
  19. return sid;
  20. }
  21. public void setSid(Integer sid) {
  22. this.sid = sid;
  23. }
  24. public String getSex() {
  25. return sex;
  26. }
  27. public void setSex(String sex) {
  28. this.sex = sex;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Sex{" +
  33. "id=" + id +
  34. ", sid=" + sid +
  35. ", sex='" + sex + '\'' +
  36. '}';
  37. }
  38. }

Student实体类

  1. package com.hbt.entity;
  2. /**
  3. * @author houbotong
  4. * @version 0.0.3
  5. * @description Student
  6. * @since 2022-03-23 13:44
  7. */
  8. public class Student {
  9. private Integer id; //学生表id
  10. private String sname; //学生姓名
  11. private Integer ssex; //学生性别id
  12. private String sex; //学生性别
  13. private Integer sage; //学生年龄
  14. private Integer sclass; //学生班级id
  15. private String classes; //学生班级
  16. private String szym; //座右铭
  17. private Integer ssubject;//最喜欢学科id
  18. private String subject; //最喜欢学科
  19. public Integer getId() {
  20. return id;
  21. }
  22. public void setId(Integer id) {
  23. this.id = id;
  24. }
  25. public String getSname() {
  26. return sname;
  27. }
  28. public void setSname(String sname) {
  29. this.sname = sname;
  30. }
  31. public Integer getSsex() {
  32. return ssex;
  33. }
  34. public void setSsex(Integer ssex) {
  35. this.ssex = ssex;
  36. }
  37. public String getSex() {
  38. return sex;
  39. }
  40. public void setSex(String sex) {
  41. this.sex = sex;
  42. }
  43. public Integer getSage() {
  44. return sage;
  45. }
  46. public void setSage(Integer sage) {
  47. this.sage = sage;
  48. }
  49. public Integer getSclass() {
  50. return sclass;
  51. }
  52. public void setSclass(Integer sclass) {
  53. this.sclass = sclass;
  54. }
  55. public String getClasses() {
  56. return classes;
  57. }
  58. public void setClasses(String classes) {
  59. this.classes = classes;
  60. }
  61. public String getSzym() {
  62. return szym;
  63. }
  64. public void setSzym(String szym) {
  65. this.szym = szym;
  66. }
  67. public Integer getSsubject() {
  68. return ssubject;
  69. }
  70. public void setSsubject(Integer ssubject) {
  71. this.ssubject = ssubject;
  72. }
  73. public String getSubject() {
  74. return subject;
  75. }
  76. public void setSubject(String subject) {
  77. this.subject = subject;
  78. }
  79. @Override
  80. public String toString() {
  81. return "Student{" +
  82. "id=" + id +
  83. ", sname='" + sname + '\'' +
  84. ", ssex=" + ssex +
  85. ", sex='" + sex + '\'' +
  86. ", sage=" + sage +
  87. ", sclass=" + sclass +
  88. ", classes='" + classes + '\'' +
  89. ", szym='" + szym + '\'' +
  90. ", ssubject=" + ssubject +
  91. ", subject='" + subject + '\'' +
  92. '}';
  93. }
  94. }

Subject实体类

  1. package com.hbt.entity;
  2. /**
  3. * @author houbotong
  4. * @version 0.0.3
  5. * @description Subject
  6. * @since 2022-03-23 13:53
  7. */
  8. public class Subject {
  9. private Integer id;
  10. private Integer sid;
  11. private String subject;
  12. public Integer getId() {
  13. return id;
  14. }
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18. public Integer getSid() {
  19. return sid;
  20. }
  21. public void setSid(Integer sid) {
  22. this.sid = sid;
  23. }
  24. public String getSubject() {
  25. return subject;
  26. }
  27. public void setSubject(String subject) {
  28. this.subject = subject;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Subject{" +
  33. "id=" + id +
  34. ", sid=" + sid +
  35. ", subject='" + subject + '\'' +
  36. '}';
  37. }
  38. }

6、dao层代码

  1. package com.hbt.dao;
  2. import com.hbt.entity.Sex;
  3. import com.hbt.entity.Student;
  4. import com.hbt.entity.Subject;
  5. import org.apache.ibatis.annotations.Param;
  6. import java.util.List;
  7. /**
  8. * @author houbotong
  9. * @version 0.0.3
  10. * @description StudentMapper
  11. * @since 2022-03-23 13:53
  12. */
  13. public interface StudentMapper {
  14. //下拉框加载学生性别
  15. public List<Sex> selsex();
  16. //下拉框加载学生班级
  17. public List<Class> selclass();
  18. //下拉框加载学生最喜欢学科
  19. public List<Subject> selsubject();
  20. //分页查询数据
  21. public List<Student> selAllinfo(@Param("start") Integer start,@Param("end") Integer end,
  22. @Param("sname") String sname,@Param("ssex") Integer ssex,
  23. @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
  24. //查询总条数
  25. public Integer count(@Param("sname") String sname,@Param("ssex") Integer ssex,
  26. @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
  27. //新增学生信息
  28. public void addinfo(Student student);
  29. //根据id查询出该学生信息
  30. public Student selByidinfo(Integer id);
  31. //修改学生信息
  32. public void upinfo(Student student);
  33. //删除学生信息
  34. public void delinfo(Integer id);
  35. }

7、service层代码

  1. package com.hbt.service;
  2. import com.hbt.entity.Page;
  3. import com.hbt.entity.Sex;
  4. import com.hbt.entity.Student;
  5. import com.hbt.entity.Subject;
  6. import org.apache.ibatis.annotations.Param;
  7. import java.util.List;
  8. /**
  9. * @author houbotong
  10. * @version 0.0.3
  11. * @description StudentService
  12. * @since 2022-03-23 13:54
  13. */
  14. public interface StudentService {
  15. //下拉框加载学生性别
  16. public List<Sex> selsex();
  17. //下拉框加载学生班级
  18. public List<Class> selclass();
  19. //下拉框加载学生最喜欢学科
  20. public List<Subject> selsubject();
  21. //分页查询数据
  22. public Page selinfo(Integer start, Integer end, String sname, Integer ssex, Integer sclass, Integer ssubject);
  23. //新增学生信息
  24. public void addinfo(Student student);
  25. //根据id查询出该学生信息
  26. public Student selByidinfo(Integer id);
  27. //修改学生信息
  28. public void upinfo(Student student);
  29. //删除学生信息
  30. public void delinfo(Integer id);
  31. }

8、serviceimpl层代码

  1. package com.hbt.service.impl;
  2. import com.hbt.dao.StudentMapper;
  3. import com.hbt.entity.Page;
  4. import com.hbt.entity.Sex;
  5. import com.hbt.entity.Student;
  6. import com.hbt.entity.Subject;
  7. import com.hbt.service.StudentService;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. /**
  12. * @author houbotong
  13. * @version 0.0.3
  14. * @description StudentServiceImpl
  15. * @since 2022-03-23 13:54
  16. */
  17. @Service
  18. public class StudentServiceImpl implements StudentService {
  19. @Autowired
  20. private StudentMapper studentMapper;
  21. @Override
  22. public List<Sex> selsex() {
  23. return studentMapper.selsex();
  24. }
  25. @Override
  26. public List<Class> selclass() {
  27. return studentMapper.selclass();
  28. }
  29. @Override
  30. public List<Subject> selsubject() {
  31. return studentMapper.selsubject();
  32. }
  33. @Override
  34. public Page selinfo(Integer start, Integer end, String sname, Integer ssex, Integer sclass, Integer ssubject) {
  35. Page page = new Page();
  36. Integer totalcount = studentMapper.count(sname, ssex, sclass, ssubject);
  37. if (start == null )start =1;
  38. page.setTotalNumber(totalcount);
  39. page.setCurrentPage(start == 0 ? 1:start);
  40. page.setTotalPage(totalcount%3>0 ? totalcount/3+1 : totalcount/3);
  41. page.setBackPage(start-1<0 ? 1:start-1);
  42. page.setNextPage(start+1>page.getTotalPage() ? page.getTotalPage() : start+1);
  43. List<Student> studentList =studentMapper.selAllinfo(
  44. (page.getCurrentPage()-1)*3, 3, sname, ssex, sclass, ssubject);
  45. page.setStudentList(studentList);
  46. return page;
  47. }
  48. @Override
  49. public void addinfo(Student student) {
  50. studentMapper.addinfo(student);
  51. }
  52. @Override
  53. public Student selByidinfo(Integer id) {
  54. return studentMapper.selByidinfo(id);
  55. }
  56. @Override
  57. public void upinfo(Student student) {
  58. studentMapper.upinfo(student);
  59. }
  60. @Override
  61. public void delinfo(Integer id) {
  62. studentMapper.delinfo(id);
  63. }
  64. }

9、controller层代码

  1. package com.hbt.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.hbt.entity.Page;
  4. import com.hbt.entity.Sex;
  5. import com.hbt.entity.Student;
  6. import com.hbt.entity.Subject;
  7. import com.hbt.service.StudentService;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.Model;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import javax.annotation.Resource;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.util.List;
  17. /**
  18. * @author houbotong
  19. * @version 0.0.3
  20. * @description StudentController
  21. * @since 2022-03-23 13:34
  22. */
  23. @Controller
  24. public class StudentController {
  25. @Resource
  26. private StudentService studentService;
  27. //默认页面
  28. @RequestMapping("/initWeb")
  29. public String initWeb(Model model){
  30. //初始页面下拉框加载
  31. List<Sex> sexList = studentService.selsex();
  32. List<Class> classList = studentService.selclass();
  33. List<Subject> subjectList = studentService.selsubject();
  34. model.addAttribute("sexList",sexList);
  35. model.addAttribute("classList",classList);
  36. model.addAttribute("subjectList",subjectList);
  37. return "index";
  38. }
  39. //分页展示所有数据
  40. @RequestMapping("/studentList")
  41. @ResponseBody
  42. public void selAllInfo(HttpServletRequest request, HttpServletResponse response, Integer start, Integer end,
  43. String sname, Integer ssex, Integer sclass, Integer ssubject) throws IOException {
  44. if (start == null) start=1;
  45. //System.out.println("sname:"+sname+";ssex:"+ssex+";sclass:"+sclass+";ssubject:"+ssubject);
  46. Page page = studentService.selinfo(start, end, sname, ssex, sclass, ssubject);
  47. JSONObject jobj = new JSONObject();
  48. jobj.put("page",page);
  49. //System.out.println(page);
  50. response.getWriter().append(jobj.toJSONString());
  51. }
  52. //新增学生信息
  53. @RequestMapping("/stuAdd")
  54. public String addinfo(Student student){
  55. studentService.addinfo(student);
  56. return "redirect:/initWeb";
  57. }
  58. //新增信息跳转
  59. @RequestMapping("/toAdd")
  60. public String toAdd(Model model){
  61. //新增页面下拉框加载
  62. List<Sex> sexList = studentService.selsex();
  63. List<Class> classList = studentService.selclass();
  64. List<Subject> subjectList = studentService.selsubject();
  65. model.addAttribute("sexList",sexList);
  66. model.addAttribute("classList",classList);
  67. model.addAttribute("subjectList",subjectList);
  68. return "addinfo";
  69. }
  70. //根据id查出该学生信息
  71. @RequestMapping("/selupinfo")
  72. public String selupinfo(Model model,Integer id){
  73. //修改页面下拉框加载
  74. List<Sex> sexList = studentService.selsex();
  75. List<Class> classList = studentService.selclass();
  76. List<Subject> subjectList = studentService.selsubject();
  77. model.addAttribute("sexList",sexList);
  78. model.addAttribute("classList",classList);
  79. model.addAttribute("subjectList",subjectList);
  80. Student student = studentService.selByidinfo(id);
  81. model.addAttribute("student",student);
  82. return "upinfo";
  83. }
  84. //修改学生信息
  85. @RequestMapping("/updateinfo")
  86. public String updateinfo(Student student){
  87. studentService.upinfo(student);
  88. return "redirect:/initWeb";
  89. }
  90. //删除学生信息
  91. @RequestMapping("/delindo")
  92. public String delinfo(Integer id){
  93. studentService.delinfo(id);
  94. return "redirect:/initWeb";
  95. }
  96. }

10、前端页面代码

添加页面

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>新增学生</title>
  6. </head>
  7. <body>
  8. <h2>新增学生</h2>
  9. <form action="stuAdd" method="post" accept-charset="UTF-8">
  10. <table border="1">
  11. <tr>
  12. <td>学生姓名</td>
  13. <td>
  14. <input type="text" name="sname">
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>年龄</td>
  19. <td>
  20. <input type="text" name="sage">
  21. </td>
  22. </tr>
  23. <tr>
  24. <td>座右铭</td>
  25. <td>
  26. <input type="text" name="szym">
  27. </td>
  28. </tr>
  29. <tr>
  30. <td>性别</td>
  31. <td>
  32. <select name="ssex" id="ssex">
  33. <c:forEach items="${sexList}" var="sex">
  34. <option value="${sex.sid}">${sex.sex}</option>
  35. </c:forEach>
  36. </select>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td>班级</td>
  41. <td>
  42. <select name="sclass" id="sclass">
  43. <c:forEach items="${classList}" var="cl">
  44. <option value="${cl.sid}">${cl.classes}</option>
  45. </c:forEach>
  46. </select>
  47. </td>
  48. </tr>
  49. <tr>
  50. <td>最爱学科</td>
  51. <td>
  52. <select name="ssubject" id="ssubject">
  53. <c:forEach items="${subjectList}" var="sub">
  54. <option value="${sub.sid}">${sub.subject}</option>
  55. </c:forEach>
  56. </select>
  57. </td>
  58. </tr>
  59. <tr>
  60. <td colspan="2">
  61. <input type="submit" value="提交">
  62. </td>
  63. </tr>
  64. </table>
  65. </form>
  66. </body>
  67. </html>

加载页面 

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>学生信息</title>
  6. </head>
  7. <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
  8. <script language="JavaScript">
  9. window.onload=function (){
  10. aaa();
  11. }
  12. function aaa(start){
  13. var info={
  14. sname : document.getElementById("sname").value,
  15. ssex : document.getElementById("ssex").value,
  16. sclass : document.getElementById("sclass").value,
  17. ssubject : document.getElementById("ssubject").value,
  18. start : start
  19. };
  20. //alert(info.ssex);
  21. $.ajax({
  22. url:"/SSMTest6_war_exploded/studentList",//服务器地址
  23. type:"post",//提交方式
  24. data: info,//提交数据
  25. dataType:"json",//回调数据类型
  26. success:function (page){
  27. var dataObj = page.page.studentList;//返回的result的json格式的数据
  28. var dataObj2 = page.page.totalPage;
  29. con = "";
  30. con2 = "";
  31. con+="<tr>";
  32. con+="<th>学生姓名</th>";
  33. con+="<th>年龄</th>";
  34. con+="<th>座右铭</th>";
  35. con+="<th>性别</th>";
  36. con+="<th>班级</th>";
  37. con+="<th>最爱学科</th>";
  38. con+="<th colspan=2>操作</th>";
  39. con+="</tr>";
  40. $.each(dataObj,function (index,item){
  41. con+="<tr>";
  42. con+="<td>"+item.sname+"</td>";
  43. con+="<td>"+item.sage+"</td>";
  44. con+="<td>"+item.szym+"</td>";
  45. con+="<td>"+item.sex+"</td>";
  46. con+="<td>"+item.classes+"</td>";
  47. con+="<td>"+item.subject+"</td>";
  48. con+="<td><a href='/SSMTest6_war_exploded/selupinfo?id="+item.id+"'>修改</a>&nbsp|&nbsp<a href='/SSMTest6_war_exploded/delindo?id="+item.id+"'>删除</a></td>";
  49. con+="</tr>";
  50. });
  51. $("#con").html(con);
  52. con2+="<button id='aaa' onclick='aaa(1)'>首页</button>";
  53. con2+="<button id='aaa' onclick='aaa("+page.page.backPage+")'>上一页</button>";
  54. con2+="<button id='aaa' onclick='aaa("+page.page.nextPage+")'>下一页</button>";
  55. con2+="<button id='aaa' onclick='aaa("+page.page.totalPage+")'>末页</button>";
  56. con2+="<span>第"+page.page.currentPage+"页/共"+page.page.totalPage+"页("+page.page.totalNumber+"条记录)</span><br/>";
  57. //con2+="<div>跳转至<input type='text' id='paper' onkeyup='this.value=this.value.replace(/\D/g,'')' onafterpaste='this.value=this.value.replace(/\D/g,'')'/>页<input type='button' value='GO' onclick='aaa()'/></div>";
  58. for(var i=1; i<=dataObj2; i++){
  59. con2+="<button id='aaa' onclick='aaa("+i+")'>"+i+"</button>";
  60. }
  61. $("#con2").html(con2);
  62. }
  63. });
  64. }
  65. </script>
  66. <body>
  67. <a href="toAdd">添加学生</a>
  68. <div>
  69. 学生名称<input type="text" name="sname" id="sname">&nbsp;&nbsp;
  70. 性别<select name="ssex" id="ssex">
  71. <option value="">--请选择--</option>
  72. <c:forEach items="${sexList}" var="sex">
  73. <option value="${sex.sid}">${sex.sex}</option>
  74. </c:forEach>
  75. </select>
  76. 班级<select name="sclass" id="sclass">
  77. <option value="">--请选择--</option>
  78. <c:forEach items="${classList}" var="cl">
  79. <option value="${cl.sid}">${cl.classes}</option>
  80. </c:forEach>
  81. </select>
  82. 最爱学科<select name="ssubject" id="ssubject">
  83. <option value="">--请选择--</option>
  84. <c:forEach items="${subjectList}" var="sub">
  85. <option value="${sub.sid}">${sub.subject}</option>
  86. </c:forEach>
  87. </select>
  88. <button id="aaa" onclick="aaa(1)">查询</button>
  89. </div>
  90. <div id="bbb">
  91. <table border="1" id="con">
  92. </table>
  93. </div>
  94. <div id="con2">
  95. </div>
  96. </body>
  97. </html>

修改页面 

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>修改学生信息</title>
  6. </head>
  7. <body>
  8. <h2>修改学生信息</h2>
  9. <form action="updateinfo" method="post">
  10. <input type="hidden" name="id" value="${student.id}">
  11. <table border="1">
  12. <tr>
  13. <td>学生姓名</td>
  14. <td>
  15. <input type="text" name="sname" value="${student.sname}">
  16. </td>
  17. </tr>
  18. <tr>
  19. <td>年龄</td>
  20. <td>
  21. <input type="text" name="sage" value="${student.sage}">
  22. </td>
  23. </tr>
  24. <tr>
  25. <td>座右铭</td>
  26. <td>
  27. <input type="text" name="szym" value="${student.szym}">
  28. </td>
  29. </tr>
  30. <tr>
  31. <td>性别</td>
  32. <td>
  33. <select name="ssex">
  34. <c:forEach items="${sexList}" var="sex">
  35. <c:if test="${sex.sid == student.ssex}">
  36. <option value="${sex.sid}" selected = "selected">${sex.sex}</option>
  37. </c:if>
  38. <c:if test="${sex.sid != student.ssex}">
  39. <option value="${sex.sid}">${sex.sex}</option>
  40. </c:if>
  41. </c:forEach>
  42. </select>
  43. </td>
  44. </tr>
  45. <tr>
  46. <td>班级</td>
  47. <td>
  48. <select name="sclass">
  49. <c:forEach items="${classList}" var="cl">
  50. <c:if test="${cl.sid == student.sclass}">
  51. <option value="${cl.sid}" selected = "selected">${cl.classes}</option>
  52. </c:if>
  53. <c:if test="${cl.sid != student.sclass}">
  54. <option value="${cl.sid}">${cl.classes}</option>
  55. </c:if>
  56. </c:forEach>
  57. </select>
  58. </td>
  59. </tr>
  60. <tr>
  61. <td>最爱学科</td>
  62. <td>
  63. <select name="ssubject">
  64. <c:forEach items="${subjectList}" var="sub">
  65. <c:if test="${sub.sid == student.ssubject}">
  66. <option value="${sub.sid}" selected = "selected">${sub.subject}</option>
  67. </c:if>
  68. <c:if test="${sub.sid != student.ssubject}">
  69. <option value="${sub.sid}">${sub.subject}</option>
  70. </c:if>
  71. </c:forEach>
  72. </select>
  73. </td>
  74. </tr>
  75. <tr>
  76. <td colspan="2">
  77. <input type="submit" value="提 交" />
  78. </td>
  79. </tr>
  80. </table>
  81. </form>
  82. </body>
  83. </html>

11、mapper文件StudentMapper.xml代码(sql

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--命名空间是对应接口的包名+类名 -->
  6. <mapper namespace="com.hbt.dao.StudentMapper">
  7. <!-- 下拉框加载学生性别 -->
  8. <select id="selsex" resultType="com.hbt.entity.Sex">
  9. SELECT * FROM sex
  10. </select>
  11. <!-- 下拉框加载学生班级 -->
  12. <select id="selclass" resultType="com.hbt.entity.Class">
  13. SELECT * FROM class
  14. </select>
  15. <!-- 下拉框加载学生最喜欢学科 -->
  16. <select id="selsubject" resultType="com.hbt.entity.Subject">
  17. SELECT * FROM SUBJECT
  18. </select>
  19. <!-- 分页查询学生数据 -->
  20. <select id="selAllinfo" resultType="com.hbt.entity.Student">
  21. SELECT a.id,a.sname,a.sage,a.szym,b.sex,c.classes,d.subject FROM student a
  22. INNER JOIN sex b ON b.sid=a.ssex
  23. INNER JOIN class c ON c.sid=a.sclass
  24. INNER JOIN SUBJECT d ON d.sid=a.ssubject
  25. <if test="sname != null and sname != ''">
  26. and a.sname like concat('%',#{sname},'%')
  27. </if>
  28. <if test="ssex != null and ssex != ''">
  29. and a.ssex = #{ssex}
  30. </if>
  31. <if test="sclass != null and sclass != ''">
  32. and a.sclass = #{sclass}
  33. </if>
  34. <if test="ssubject != null and ssubject != ''">
  35. and a.ssubject = #{ssubject}
  36. </if>
  37. ORDER BY a.id limit #{start },#{end }
  38. </select>
  39. <!-- 查询总条数 -->
  40. <select id="count" resultType="java.lang.Integer">
  41. SELECT COUNT(*) FROM student
  42. <trim prefix="where">
  43. 1=1
  44. <if test="sname != null and sname != ''">
  45. and sname like concat('%',#{sname},'%')
  46. </if>
  47. <if test="ssex != null and ssex != ''">
  48. and ssex = #{ssex}
  49. </if>
  50. <if test="sclass != null and sclass != ''">
  51. and sclass = #{sclass}
  52. </if>
  53. <if test="ssubject != null and ssubject != ''">
  54. and ssubject = #{ssubject}
  55. </if>
  56. </trim>
  57. </select>
  58. <!-- 新增学生信息 -->
  59. <insert id="addinfo" parameterType="com.hbt.entity.Student">
  60. INSERT INTO student(sname,ssex,sage,sclass,szym,ssubject)
  61. VALUES(#{sname}, #{ssex}, #{sage}, #{sclass}, #{szym}, #{ssubject})
  62. </insert>
  63. <!-- 根据id查询出该学生信息 -->
  64. <select id="selByidinfo" resultType="com.hbt.entity.Student">
  65. SELECT * FROM student WHERE id = #{id}
  66. </select>
  67. <!-- 修改学生信息 -->
  68. <update id="upinfo" parameterType="com.hbt.entity.Student">
  69. UPDATE student SET sname=#{sname},sage=#{sage},szym=#{szym},ssex=#{ssex},sclass=#{sclass},ssubject=#{ssubject}
  70. where id=#{id}
  71. </update>
  72. <!-- 根据id删除学生信息 -->
  73. <delete id="delinfo" parameterType="com.hbt.entity.Student">
  74. delete from student where id=#{id}
  75. </delete>
  76. </mapper>

功能实现

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/262796
推荐阅读
相关标签
  

闽ICP备14008679号