SSM整合

北冰洋以北  金牌会员 | 2024-2-6 20:13:14 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 917|帖子 917|积分 2751

1、环境搭建

1.1、在project创建新module


1.2、选择maven


1.3、设置module名称和路径



1.4、module初始状态


1.5、配置打包方式和引入依赖


注意:默认的打包方式为 jar,为了能配置web资源,需要将打包方式设置为 war
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>online.liaojy</groupId>
  7.     <artifactId>spring_springmvc_mybatis</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <packaging>war</packaging>
  10.    
  11.     <properties>
  12.         <spring.version>5.3.1</spring.version>
  13.     </properties>
  14.     <dependencies>
  15.         
  16.         <dependency>
  17.             <groupId>org.springframework</groupId>
  18.             <artifactId>spring-context</artifactId>
  19.             <version>${spring.version}</version>
  20.         </dependency>
  21.         
  22.         <dependency>
  23.             <groupId>org.springframework</groupId>
  24.             <artifactId>spring-beans</artifactId>
  25.             <version>${spring.version}</version>
  26.         </dependency>
  27.         
  28.         <dependency>
  29.             <groupId>org.springframework</groupId>
  30.             <artifactId>spring-web</artifactId>
  31.             <version>${spring.version}</version>
  32.         </dependency>
  33.         
  34.         <dependency>
  35.             <groupId>org.springframework</groupId>
  36.             <artifactId>spring-webmvc</artifactId>
  37.             <version>${spring.version}</version>
  38.         </dependency>
  39.         
  40.         <dependency>
  41.             <groupId>org.springframework</groupId>
  42.             <artifactId>spring-jdbc</artifactId>
  43.             <version>${spring.version}</version>
  44.         </dependency>
  45.         
  46.         <dependency>
  47.             <groupId>org.springframework</groupId>
  48.             <artifactId>spring-aspects</artifactId>
  49.             <version>${spring.version}</version>
  50.         </dependency>
  51.         
  52.         <dependency>
  53.             <groupId>org.springframework</groupId>
  54.             <artifactId>spring-test</artifactId>
  55.             <version>${spring.version}</version>
  56.         </dependency>
  57.         
  58.         <dependency>
  59.             <groupId>org.mybatis</groupId>
  60.             <artifactId>mybatis</artifactId>
  61.             <version>3.5.7</version>
  62.         </dependency>
  63.         
  64.         <dependency>
  65.             <groupId>org.mybatis</groupId>
  66.             <artifactId>mybatis-spring</artifactId>
  67.             <version>2.0.6</version>
  68.         </dependency>
  69.         
  70.         <dependency>
  71.             <groupId>com.alibaba</groupId>
  72.             <artifactId>druid</artifactId>
  73.             <version>1.0.9</version>
  74.         </dependency>
  75.         
  76.         <dependency>
  77.             <groupId>junit</groupId>
  78.             <artifactId>junit</artifactId>
  79.             <version>4.12</version>
  80.             <scope>test</scope>
  81.         </dependency>
  82.         
  83.         <dependency>
  84.             <groupId>mysql</groupId>
  85.             <artifactId>mysql-connector-java</artifactId>
  86.             <version>5.1.49</version>
  87.         </dependency>
  88.         
  89.         <dependency>
  90.             <groupId>log4j</groupId>
  91.             <artifactId>log4j</artifactId>
  92.             <version>1.2.17</version>
  93.         </dependency>
  94.         
  95.         <dependency>
  96.             <groupId>com.github.pagehelper</groupId>
  97.             <artifactId>pagehelper</artifactId>
  98.             <version>5.2.0</version>
  99.         </dependency>
  100.         
  101.         <dependency>
  102.             <groupId>ch.qos.logback</groupId>
  103.             <artifactId>logback-classic</artifactId>
  104.             <version>1.2.3</version>
  105.         </dependency>
  106.         
  107.         <dependency>
  108.             <groupId>javax.servlet</groupId>
  109.             <artifactId>javax.servlet-api</artifactId>
  110.             <version>3.1.0</version>
  111.             <scope>provided</scope>
  112.         </dependency>
  113.         
  114.         <dependency>
  115.             <groupId>com.fasterxml.jackson.core</groupId>
  116.             <artifactId>jackson-databind</artifactId>
  117.             <version>2.12.1</version>
  118.         </dependency>
  119.         
  120.         <dependency>
  121.             <groupId>commons-fileupload</groupId>
  122.             <artifactId>commons-fileupload</artifactId>
  123.             <version>1.3.1</version>
  124.         </dependency>
  125.         
  126.         <dependency>
  127.             <groupId>org.thymeleaf</groupId>
  128.             <artifactId>thymeleaf-spring5</artifactId>
  129.             <version>3.0.12.RELEASE</version>
  130.         </dependency>
  131.     </dependencies>
  132. </project>
复制代码
1.6、配置web资源目录


打开Project Structure,选择对应的module,并为该module创建一个web.xml文件

注意:web.xml文件需要放到web资源路径(工程路径\src\main\webapp)下

1.7、配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="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_4_0.xsd"
  5.          version="4.0">
  6.    
  7.    
  8.     <listener>
  9.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10.     </listener>
  11.    
  12.    
  13.     <context-param>
  14.         <param-name>contextConfigLocation</param-name>
  15.         <param-value>classpath:spring.xml</param-value>
  16.     </context-param>
  17.    
  18.     <filter>
  19.         <filter-name>CharacterEncodingFilter</filter-name>
  20.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  21.         <init-param>
  22.             
  23.             <param-name>encoding</param-name>
  24.             <param-value>UTF-8</param-value>
  25.         </init-param>
  26.         <init-param>
  27.             
  28.             <param-name>forceEncoding</param-name>
  29.             <param-value>true</param-value>
  30.         </init-param>
  31.     </filter>
  32.     <filter-mapping>
  33.         <filter-name>CharacterEncodingFilter</filter-name>
  34.         <url-pattern>/*</url-pattern>
  35.     </filter-mapping>
  36.    
  37.     <filter>
  38.         <filter-name>HiddenHttpMethodFilter</filter-name>
  39.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  40.     </filter>
  41.     <filter-mapping>
  42.         <filter-name>HiddenHttpMethodFilter</filter-name>
  43.         <url-pattern>/*</url-pattern>
  44.     </filter-mapping>
  45.    
  46.    
  47.     <servlet>
  48.         <servlet-name>SpringMVC</servlet-name>
  49.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  50.         
  51.         <init-param>
  52.             <param-name>contextConfigLocation</param-name>
  53.             <param-value>classpath:springmvc.xml</param-value>
  54.         </init-param>
  55.         
  56.         <load-on-startup>1</load-on-startup>
  57.     </servlet>
  58.     <servlet-mapping>
  59.         <servlet-name>SpringMVC</servlet-name>
  60.         <url-pattern>/</url-pattern>
  61.     </servlet-mapping>
  62. </web-app>
复制代码
1.8、创建SpringMVC的配置文件

  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:context="http://www.springframework.org/schema/context"
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7.    
  8.     <context:component-scan base-package="online.liaojy.ssm.controller"></context:component-scan>
  9.    
  10.     <bean id="viewResolver" >
  11.         <property name="order" value="1"/>
  12.         <property name="characterEncoding" value="UTF-8"/>
  13.         <property name="templateEngine">
  14.             <bean >
  15.                 <property name="templateResolver">
  16.                     <bean
  17.                             >
  18.                         
  19.                         <property name="prefix" value="/WEB-INF/templates/"/>
  20.                         
  21.                         <property name="suffix" value=".html"/>
  22.                         <property name="templateMode" value="HTML5"/>
  23.                         <property name="characterEncoding" value="UTF-8" />
  24.                     </bean>
  25.                 </property>
  26.             </bean>
  27.         </property>
  28.     </bean>
  29.    
  30.     <mvc:default-servlet-handler></mvc:default-servlet-handler>
  31.    
  32.     <mvc:annotation-driven></mvc:annotation-driven>
  33.    
  34.     <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
  35. </beans>
复制代码
1.9、创建请求控制器

  1. package online.liaojy.ssm.controller;
  2. import org.springframework.stereotype.Controller;
  3. /**
  4. * @author liaojy
  5. * @date 2023/12/3 - 18:32
  6. */
  7. @Controller
  8. public class EmployeeController {
  9. }
复制代码
1.10、创建模板目录及页面模板


注意:html要引入thymeleaf的约束:xmlns:th="http://www.thymeleaf.org"
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>首页</title>
  6. </head>
  7. <body>
  8. <h1>index.html</h1>
  9. </body>
  10. </html>
复制代码
1.11、创建jdbc.properties属性文件


注意:本例使用的数据库版本为 MySQL-5 ,因此驱动配置为 com.mysql.jdbc.Driver
如果使用的数据库版本为 MySQL-8 ,驱动配置应该为 com.mysql.cj.jdbc.Driver
  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root
复制代码
1.12、创建MyBatis的核心配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3.         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.    
  7.     <properties resource="jdbc.properties"/>
  8.    
  9.     <settings>
  10.         
  11.         <setting name="mapUnderscoreToCamelCase" value="true"/>
  12.         
  13.         
  14.         
  15.         <setting name="cacheEnabled" value="true"/>
  16.     </settings>
  17.    
  18.     <typeAliases>
  19.         <package name="online.liaojy.ssm.pojo"/>
  20.     </typeAliases>
  21.     <plugins>
  22.         
  23.         <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  24.     </plugins>
  25.    
  26.     <environments default="development">
  27.         <environment id="development">
  28.             <transactionManager type="JDBC"/>
  29.             <dataSource type="POOLED">
  30.                 <property name="driver" value="${jdbc.driver}"/>
  31.                 <property name="url" value="${jdbc.url}"/>
  32.                 <property name="username" value="${jdbc.username}"/>
  33.                 <property name="password" value="${jdbc.password}"/>
  34.             </dataSource>
  35.         </environment>
  36.     </environments>
  37.    
  38.     <mappers>
  39.         <package name="online.liaojy.ssm.mapper"/>
  40.     </mappers>
  41. </configuration>
复制代码
1.13、创建mapper接口

  1. package online.liaojy.ssm.mapper;
  2. /**
  3. * @author liaojy
  4. * @date 2023/12/4 - 7:00
  5. */
  6. public interface EmployeeMapper {
  7. }
复制代码
1.14、创建mapper映射文件


注意:mapper映射文件的目录结构和名称要和mapper接口的一致
mapper映射文件的命名空间,也要和mapper接口的全类名一致
  1. [/code][size=4]1.15、创建Spring的配置文件[/size]
  2. [img]https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231204172920582-339752083.png[/img]
  3. [code]<?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.        xmlns:context="http://www.springframework.org/schema/context"
  7.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  9.    
  10.     <context:component-scan base-package="online.liaojy.ssm.controller"></context:component-scan>
  11.    
  12.     <bean id="viewResolver" >
  13.         <property name="order" value="1"/>
  14.         <property name="characterEncoding" value="UTF-8"/>
  15.         <property name="templateEngine">
  16.             <bean >
  17.                 <property name="templateResolver">
  18.                     <bean
  19.                             >
  20.                         
  21.                         <property name="prefix" value="/WEB-INF/templates/"/>
  22.                         
  23.                         <property name="suffix" value=".html"/>
  24.                         <property name="templateMode" value="HTML5"/>
  25.                         <property name="characterEncoding" value="UTF-8" />
  26.                     </bean>
  27.                 </property>
  28.             </bean>
  29.         </property>
  30.     </bean>
  31.    
  32.     <mvc:default-servlet-handler></mvc:default-servlet-handler>
  33.    
  34.     <mvc:annotation-driven></mvc:annotation-driven>
  35.    
  36.     <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
  37. </beans>                                       
复制代码
1.16、创建业务层接口及实现类

  1. package online.liaojy.ssm.service;
  2. /**
  3. * @author liaojy
  4. * @date 2023/12/4 - 20:32
  5. */
  6. public interface EmployeeService {
  7. }
复制代码
  1. package online.liaojy.ssm.service.impl;
  2. import online.liaojy.ssm.service.EmployeeService;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Transactional;
  5. /**
  6. * @author liaojy
  7. * @date 2023/12/4 - 20:34
  8. */
  9. @Service
  10. @Transactional
  11. public class EmployeeServiceImpl implements EmployeeService {
  12. }
复制代码
1.17、创建日志文件log4j.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  4.     <appender name="STDOUT" >
  5.         <param name="Encoding" value="UTF-8" />
  6.         <layout >
  7.             <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
  8.         </layout>
  9.     </appender>
  10.     <logger name="java.sql">
  11.         <level value="debug" />
  12.     </logger>
  13.     <logger name="org.apache.ibatis">
  14.         <level value="info" />
  15.     </logger>
  16.     <root>
  17.         <level value="debug" />
  18.         <appender-ref ref="STDOUT" />
  19.     </root>
  20. </log4j:configuration>
复制代码
1.18、配置tomcat




1.19、测试效果



2、实战案例

2.1、创建表

  1. CREATE TABLE `t_emp` (
  2. `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `emp_name` varchar(20) DEFAULT NULL,
  4. `age` int(11) DEFAULT NULL,
  5. `sex` char(1) DEFAULT NULL,
  6. `email` varchar(50) DEFAULT NULL,
  7. PRIMARY KEY (`emp_id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
复制代码
2.2、插入数据

  1. insert into t_emp (emp_name,age,sex,email) values ('a',18,'男','11@qq.com');
  2. insert into t_emp (emp_name,age,sex,email) values ('b',19,'男','22@qq.com');
  3. insert into t_emp (emp_name,age,sex,email) values ('c',20,'男','33@qq.com');
  4. insert into t_emp (emp_name,age,sex,email) values ('d',21,'男','44@qq.com');
  5. insert into t_emp (emp_name,age,sex,email) values ('e',22,'男','55@qq.com');
  6. insert into t_emp (emp_name,age,sex,email) values ('f',23,'男','66@qq.com');
  7. insert into t_emp (emp_name,age,sex,email) values ('g',24,'男','77@qq.com');
  8. insert into t_emp (emp_name,age,sex,email) values ('h',25,'男','88@qq.com');
  9. insert into t_emp (emp_name,age,sex,email) values ('i',26,'男','99@qq.com');
  10. insert into t_emp (emp_name,age,sex,email) values ('j',27,'男','00@qq.com');
  11. insert into t_emp (emp_name,age,sex,email) values ('k',28,'男','111@qq.com');
  12. insert into t_emp (emp_name,age,sex,email) values ('l',29,'男','222@qq.com');
  13. insert into t_emp (emp_name,age,sex,email) values ('m',30,'男','333@qq.com');
  14. insert into t_emp (emp_name,age,sex,email) values ('n',31,'男','444@qq.com');
  15. insert into t_emp (emp_name,age,sex,email) values ('o',32,'男','555@qq.com');
  16. insert into t_emp (emp_name,age,sex,email) values ('p',33,'男','666@qq.com');
  17. insert into t_emp (emp_name,age,sex,email) values ('q',34,'男','777@qq.com');
  18. insert into t_emp (emp_name,age,sex,email) values ('r',35,'男','888@qq.com');
  19. insert into t_emp (emp_name,age,sex,email) values ('s',36,'男','999@qq.com');
  20. insert into t_emp (emp_name,age,sex,email) values ('t',37,'男','000@qq.com');
  21. insert into t_emp (emp_name,age,sex,email) values ('u',38,'男','1111@qq.com');
  22. insert into t_emp (emp_name,age,sex,email) values ('v',39,'男','2222@qq.com');
  23. insert into t_emp (emp_name,age,sex,email) values ('w',40,'男','3333@qq.com');
  24. insert into t_emp (emp_name,age,sex,email) values ('x',41,'男','4444@qq.com');
  25. insert into t_emp (emp_name,age,sex,email) values ('y',42,'男','5555@qq.com');
  26. insert into t_emp (emp_name,age,sex,email) values ('z',43,'男','6666@qq.com');
复制代码

2.3、创建实体类

  1. package online.liaojy.ssm.pojo;
  2. import java.io.Serializable;
  3. /**
  4. * @author liaojy
  5. * @date 2023/12/4 - 23:06
  6. */
  7. public class Employee implements Serializable {
  8.     private Integer empId;
  9.     private String empName;
  10.     private Integer age;
  11.     private String sex;
  12.     private String email;
  13.     public Employee() {
  14.     }
  15.     public Employee(Integer empId, String empName, Integer age, String sex, String email) {
  16.         this.empId = empId;
  17.         this.empName = empName;
  18.         this.age = age;
  19.         this.sex = sex;
  20.         this.email = email;
  21.     }
  22.     public Integer getEmpId() {
  23.         return empId;
  24.     }
  25.     public void setEmpId(Integer empId) {
  26.         this.empId = empId;
  27.     }
  28.     public String getEmpName() {
  29.         return empName;
  30.     }
  31.     public void setEmpName(String empName) {
  32.         this.empName = empName;
  33.     }
  34.     public Integer getAge() {
  35.         return age;
  36.     }
  37.     public void setAge(Integer age) {
  38.         this.age = age;
  39.     }
  40.     public String getSex() {
  41.         return sex;
  42.     }
  43.     public void setSex(String sex) {
  44.         this.sex = sex;
  45.     }
  46.     public String getEmail() {
  47.         return email;
  48.     }
  49.     public void setEmail(String email) {
  50.         this.email = email;
  51.     }
  52.     @Override
  53.     public String toString() {
  54.         return "Employee{" +
  55.                 "empId=" + empId +
  56.                 ", empName='" + empName + '\'' +
  57.                 ", age=" + age +
  58.                 ", sex='" + sex + '\'' +
  59.                 ", email='" + email + '\'' +
  60.                 '}';
  61.     }
  62. }
复制代码
2.4、持久层

  1.     /**
  2.      * 查询所有员工信息
  3.      * @return
  4.      */
  5.     List<Employee> getAllEmployee();
复制代码
  1.    
  2.     <select id="getAllEmployee" resultType="Employee">
  3.         select * from t_emp
  4.     </select>
复制代码
2.5、业务层

  1.     /**
  2.      * 查询所有员工信息
  3.      * @return
  4.      */
  5.     List<Employee> getAllEmployee();
复制代码
  1.     @Autowired
  2.     EmployeeMapper employeeMapper;
  3.     public List<Employee> getAllEmployee() {
  4.         return employeeMapper.getAllEmployee();
  5.     }
复制代码
2.6、控制层

  1.     @Autowired
  2.     EmployeeService employeeService;
  3.     @GetMapping("/employee")
  4.     public String getAllEmployee(Model model){
  5.         // 查询所有员工信息
  6.         List<Employee> employees = employeeService.getAllEmployee();
  7.         //将员工信息共享到请求域
  8.         model.addAttribute("employees",employees);
  9.         //跳转到员工列表页面employee_list.html
  10.         return "employee_list";
  11.     }
复制代码
2.7、页面视图

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>员工列表</title>
  6. </head>
  7. <body>
  8. <table border="10">
  9.     <tr>
  10.         
  11.         <th colspan="7">员工列表</th>
  12.     </tr>
  13.     <tr>
  14.         <th>序号</th>
  15.         <th>员工id</th>
  16.         <th>员工姓名</th>
  17.         <th>员工年龄</th>
  18.         <th>员工性别</th>
  19.         <th>员工邮箱</th>
  20.         <th>操作(<a target="_blank" href="https://www.cnblogs.com/">添加员工</a>)</th>
  21.     </tr>
  22.    
  23.    
  24.     <tr th:each="employee,status : ${employees}">
  25.         
  26.         <td th:text="${status.count}"></td>
  27.         <td th:text="${employee.empId}"></td>
  28.         <td th:text="${employee.empName}"></td>
  29.         <td th:text="${employee.age}"></td>
  30.         <td th:text="${employee.sex}"></td>
  31.         <td th:text="${employee.email}"></td>
  32.         <td>
  33.             <a target="_blank" href="https://www.cnblogs.com/">修改员工</a>
  34.             <a target="_blank" href="https://www.cnblogs.com/">删除员工</a>
  35.         </td>
  36.     </tr>
  37. </table>
  38. </body>
  39. </html>
复制代码
2.8、添加访问入口

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/employee}">查询所有员工信息</a>
复制代码
2.9、测试效果



3、分页优化

3.1、业务层

  1.     /**
  2.      * 分页查询所有员工信息
  3.      * @param pageNum
  4.      * @return
  5.      */
  6.     PageInfo<Employee> getAllEmployeeWithPage(Integer pageNum,Integer pageSize);
复制代码
  1.     public PageInfo<Employee> getAllEmployeeWithPage(Integer pageNum,Integer pageSize) {
  2.         // 在查询之前,开启分页功能,并设置当前页的页码和每页的数据条数
  3.         PageHelper.startPage(pageNum,pageSize);
  4.         // 查询所有的员工信息()
  5.         // 分页插件会在查询前先进行拦截,添加相关的sql分页代码后(因此在mapper映射文件编写的sql不能使用分号结束),再放行
  6.         List<Employee> employees = employeeMapper.getAllEmployee();
  7.         // 根据分页数据(employees)和分页导航的页码数(navigatePages),创建分页数据对象
  8.         PageInfo<Employee> employeePageInfo = new PageInfo<Employee>(employees, 5);
  9.         return employeePageInfo;
  10.     }
复制代码
3.2、控制层

  1.     @GetMapping(value = {"/employee/page/{pageNum}","/employee/page/{pageNum}/{pageSize}"})
  2.     public String getAllEmployeeWithPage(Model model,
  3.                                          @PathVariable("pageNum") Integer pageNum,
  4.                                          @PathVariable(value = "pageSize",required = false) Integer pageSize){
  5.         // 判断请求中是否有设置每页的数据条数,没有的话就给一个默认值
  6.         if (pageSize == null){
  7.             pageSize = 4;
  8.         }
  9.         // 根据当前页的页码,分页查询所有员工信息
  10.         PageInfo<Employee> employeePageInfo = employeeService.getAllEmployeeWithPage(pageNum,pageSize);
  11.         //将分页数据共享到请求域
  12.         model.addAttribute("employeePageInfo",employeePageInfo);
  13.         //跳转到员工列表分页的页面employee_page.html
  14.         return "employee_page";
  15.     }
复制代码
3.3、页面视图


关于分页数据对象的属性细节,请参考12.3.3.1节
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>员工列表</title>
  6. </head>
  7. <body>
  8. <table border="10">
  9.     <tr>
  10.         
  11.         <th colspan="7">员工列表</th>
  12.     </tr>
  13.     <tr>
  14.         <th>序号</th>
  15.         <th>员工id</th>
  16.         <th>员工姓名</th>
  17.         <th>员工年龄</th>
  18.         <th>员工性别</th>
  19.         <th>员工邮箱</th>
  20.         <th>操作(<a target="_blank" href="https://www.cnblogs.com/">添加员工</a>)</th>
  21.     </tr>
  22.    
  23.    
  24.     <tr th:each="employee,status : ${employees}">
  25.         
  26.         <td th:text="${status.count}"></td>
  27.         <td th:text="${employee.empId}"></td>
  28.         <td th:text="${employee.empName}"></td>
  29.         <td th:text="${employee.age}"></td>
  30.         <td th:text="${employee.sex}"></td>
  31.         <td th:text="${employee.email}"></td>
  32.         <td>
  33.             <a target="_blank" href="https://www.cnblogs.com/">修改员工</a>
  34.             <a target="_blank" href="https://www.cnblogs.com/">删除员工</a>
  35.         </td>
  36.     </tr>
  37. </table>
  38. </body>
  39. </html>        [url=https://www.cnblogs.com/@{/employee/page/1}]首页[/url]    [url=https://www.cnblogs.com/@{]上一页[/url]                                                    [url=https://www.cnblogs.com/@{]下一页[/url]    [url=https://www.cnblogs.com/@{]末页[/url]
复制代码
3.4、添加访问入口

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/employee/page/1}">分页查询所有员工信息</a>
复制代码
3.5、测试效果



如上图所示,当前页为第一页,所以在分页导航中没有“首页”和“上一页”,对应的页码带有中括号且标红

如上图所示,当前页为第二页,所以在分页导航中有“首页”和“上一页”,对应的页码带有中括号且标红

如上图所示,当前页为最后一页,所以在分页导航中没有“下一页”和“末页”,对应的页码带有中括号且标红

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表