• SSM整合


    目录

    一、ContextLoaderListener

    二、准备工作

    ⚪导入依赖

    ⚪创建表

    ⚪配置web.xml 

    ⚪配置SpringMVC.xml

    ⚪配置Spring.xml

    ⭐搭建MyBatis环境 

    ⚪创建属性文件jdbc.properties 

    ⚪配置mybatis-config.xml

    ⚪创建日志文件log4j.xml

    ⚪项目结构 

    ⚪实体类

    三、测试功能

    1.查询所有的员工信息

    ⚪创建接口EmployeeService

    ⚪创建实现类EmployeeServiceImpl 

    ⚪创建控制层组件EmployeeController 

    ⚪创建页面

    二、数据分页


    一、ContextLoaderListener

    Spring提供了监听器ContextLoaderListener,实现ServletContextListener接口

    可监听 ServletContext的状态,在web服务器的启动,读取Spring的配置文件,创建Spring的IOC容器。

    web 应用中必须在web.xml中配置

    1. <listener>
    2. <listenerclass>org.springframework.web.context.ContextLoaderListenerlistener-class>
    3. listener>
    4. <context-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:spring.xmlparam-value>
    7. context-param>

    二、准备工作

    ⚪导入依赖

    1. "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.0modelVersion>
    6. <groupId>com.atguigu.ssmgroupId>
    7. <artifactId>ssmartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <packaging>warpackaging>
    10. <properties>
    11. <spring.version>5.3.1spring.version>
    12. properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>org.springframeworkgroupId>
    16. <artifactId>spring-contextartifactId>
    17. <version>${spring.version}version>
    18. dependency>
    19. <dependency>
    20. <groupId>org.springframeworkgroupId>
    21. <artifactId>spring-beansartifactId>
    22. <version>${spring.version}version>
    23. dependency>
    24. <dependency>
    25. <groupId>org.springframeworkgroupId>
    26. <artifactId>spring-webartifactId>
    27. <version>${spring.version}version>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframeworkgroupId>
    31. <artifactId>spring-webmvcartifactId>
    32. <version>${spring.version}version>
    33. dependency>
    34. <dependency>
    35. <groupId>org.springframeworkgroupId>
    36. <artifactId>spring-jdbcartifactId>
    37. <version>${spring.version}version>
    38. dependency>
    39. <dependency>
    40. <groupId>org.springframeworkgroupId>
    41. <artifactId>spring-aspectsartifactId>
    42. <version>${spring.version}version>
    43. dependency>
    44. <dependency>
    45. <groupId>org.springframeworkgroupId>
    46. <artifactId>spring-testartifactId>
    47. <version>${spring.version}version>
    48. dependency>
    49. <dependency>
    50. <groupId>org.mybatisgroupId>
    51. <artifactId>mybatisartifactId>
    52. <version>3.5.7version>
    53. dependency>
    54. <dependency>
    55. <groupId>org.mybatisgroupId>
    56. <artifactId>mybatis-springartifactId>
    57. <version>2.0.6version>
    58. dependency>
    59. <dependency>
    60. <groupId>com.alibabagroupId>
    61. <artifactId>druidartifactId>
    62. <version>1.0.9version>
    63. dependency>
    64. <dependency>
    65. <groupId>junitgroupId>
    66. <artifactId>junitartifactId>
    67. <version>4.12version>
    68. <scope>testscope>
    69. dependency>
    70. <dependency>
    71. <groupId>mysqlgroupId>
    72. <artifactId>mysql-connector-javaartifactId>
    73. <version>8.0.16version>
    74. dependency>
    75. <dependency>
    76. <groupId>log4jgroupId>
    77. <artifactId>log4jartifactId>
    78. <version>1.2.17version>
    79. dependency>
    80. <dependency>
    81. <groupId>com.github.pagehelpergroupId>
    82. <artifactId>pagehelperartifactId>
    83. <version>5.2.0version>
    84. dependency>
    85. <dependency>
    86. <groupId>ch.qos.logbackgroupId>
    87. <artifactId>logback-classicartifactId>
    88. <version>1.2.3version>
    89. dependency>
    90. <dependency>
    91. <groupId>javax.servletgroupId>
    92. <artifactId>javax.servlet-apiartifactId>
    93. <version>3.1.0version>
    94. <scope>providedscope>
    95. dependency>
    96. <dependency>
    97. <groupId>com.fasterxml.jackson.coregroupId>
    98. <artifactId>jackson-databindartifactId>
    99. <version>2.12.1version>
    100. dependency>
    101. <dependency>
    102. <groupId>commons-fileuploadgroupId>
    103. <artifactId>commons-fileuploadartifactId>
    104. <version>1.3.1version>
    105. dependency>
    106. <dependency>
    107. <groupId>org.thymeleafgroupId>
    108. <artifactId>thymeleaf-spring5artifactId>
    109. <version>3.0.12.RELEASEversion>
    110. dependency>
    111. dependencies>
    112. project>

    ⚪创建表

    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

    ⚪配置web.xml 

    1. "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. <filter>
    7. <filter-name>CharacterEncodingFilterfilter-name>
    8. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    9. <init-param>
    10. <param-name>encodingparam-name>
    11. <param-value>UTF-8param-value>
    12. init-param>
    13. <init-param>
    14. <param-name>forceEncodingparam-name>
    15. <param-value>trueparam-value>
    16. init-param>
    17. filter>
    18. <filter-mapping>
    19. <filter-name>CharacterEncodingFilterfilter-name>
    20. <url-pattern>/*url-pattern>
    21. filter-mapping>
    22. <filter>
    23. <filter-name>HiddenHttpMethodFilterfilter-name>
    24. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    25. filter>
    26. <filter-mapping>
    27. <filter-name>HiddenHttpMethodFilterfilter-name>
    28. <url-pattern>/*url-pattern>
    29. filter-mapping>
    30. <servlet>
    31. <servlet-name>SpringMVCservlet-name>
    32. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    33. <init-param>
    34. <param-name>contextConfigLocationparam-name>
    35. <param-value>classpath:SpringMVC.xmlparam-value>
    36. init-param>
    37. <load-on-startup>1load-on-startup>
    38. servlet>
    39. <servlet-mapping>
    40. <servlet-name>SpringMVCservlet-name>
    41. <url-pattern>/url-pattern>
    42. servlet-mapping>
    43. <listener>
    44. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    45. listener>
    46. <context-param>
    47. <param-name>contextConfigLocationparam-name>
    48. <param-value>classpath:Spring.xmlparam-value>
    49. context-param>
    50. web-app>

    ⚪配置SpringMVC.xml

    1. "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
    7. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    8. http://www.springframework.org/schema/mvc
    9. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    12. <context:component-scan base-package="com.atguigu.ssm.controller">context:component-scan>
    13. <bean id="viewResolver"
    14. class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    15. <property name="order" value="1"/>
    16. <property name="characterEncoding" value="UTF-8"/>
    17. <property name="templateEngine">
    18. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    19. <property name="templateResolver">
    20. <bean
    21. class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    22. <property name="prefix" value="/WEB-INF/templates/"/>
    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. <mvc:default-servlet-handler/>
    32. <mvc:annotation-driven/>
    33. <mvc:view-controller path="/" view-name="index">mvc:view-controller>
    34. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
    35. <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">bean>
    36. beans>

    ⚪配置Spring.xml

    1. "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" xmlns:tx="http://www.springframework.org/schema/tx"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    8. http://www.springframework.org/schema/mvc
    9. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    12. <context:component-scan base-package="com.atguigu.ssm">
    13. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    14. context:component-scan>
    15. <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    16. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    17. <property name="driverClassName" value="${jdbc.driver}">property>
    18. <property name="url" value="${jdbc.url}">property>
    19. <property name="username" value="${jdbc.username}">property>
    20. <property name="password" value="${jdbc.password}">property>
    21. bean>
    22. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    23. <property name="dataSource" ref="dataSource">property>
    24. bean>
    25. <tx:annotation-driven transaction-manager="transactionManager"/>
    26. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    27. <property name="configLocation" value="classpath:mybatis-config.xml">property>
    28. <property name="dataSource" ref="dataSource">property>
    29. <property name="typeAliasesPackage" value="com.atguigu.ssm.pojo">property>
    30. <property name="plugins">
    31. <array>
    32. <bean class="com.github.pagehelper.PageInterceptor">bean>
    33. array>
    34. property>
    35. bean>
    36. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    37. <property name="basePackage" value="com.atguigu.ssm.mapper">property>
    38. bean>
    39. beans>

    ⭐搭建MyBatis环境 

    ⚪创建属性文件jdbc.properties 

    1. jdbc.driver=com.mysql.cj.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
    3. jdbc.username=root
    4. jdbc.password=123456

    ⚪配置mybatis-config.xml

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <settings>
    7. <setting name="mapUnderscoreToCamelCase" value="true"/>
    8. settings>
    9. configuration>

    ⚪创建日志文件log4j.xml

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

    ⚪项目结构 

    ⚪实体类

    1. package com.atguigu.ssm.pojo;
    2. public class Employee {
    3. private Integer empId;
    4. private String empName;
    5. private Integer age;
    6. private String gender;
    7. private String email;
    8. public Employee(Integer empId, String empName, Integer age, String gender, String email) {
    9. this.empId = empId;
    10. this.empName = empName;
    11. this.age = age;
    12. this.gender = gender;
    13. this.email = email;
    14. }
    15. public Employee() {
    16. }
    17. public Integer getEmpId() {
    18. return empId;
    19. }
    20. public void setEmpId(Integer empId) {
    21. this.empId = empId;
    22. }
    23. public String getEmpName() {
    24. return empName;
    25. }
    26. public void setEmpName(String empName) {
    27. this.empName = empName;
    28. }
    29. public Integer getAge() {
    30. return age;
    31. }
    32. public void setAge(Integer age) {
    33. this.age = age;
    34. }
    35. public String getGender() {
    36. return gender;
    37. }
    38. public void setGender(String gender) {
    39. this.gender = gender;
    40. }
    41. public String getEmail() {
    42. return email;
    43. }
    44. public void setEmail(String email) {
    45. this.email = email;
    46. }
    47. @Override
    48. public String toString() {
    49. return "Employee{" +
    50. "empId=" + empId +
    51. ", empName='" + empName + '\'' +
    52. ", age=" + age +
    53. ", gender='" + gender + '\'' +
    54. ", email='" + email + '\'' +
    55. '}';
    56. }
    57. }

    三、测试功能

    1.查询所有的员工信息

    ⚪创建接口EmployeeService

    1. public interface EmployeeService {
    2. /**
    3. * 查询所有的员工信息
    4. * @return
    5. */
    6. List getAllEmployee();
    7. }

    ⚪创建实现类EmployeeServiceImpl 

    1. @Transactional
    2. @Service
    3. public class EmployeeServiceImpl implements EmployeeService {
    4. @Autowired
    5. private EmployeeMapper employeeMapper;
    6. public List getAllEmployee() {
    7. return employeeMapper.getAllEmployee();
    8. }
    9. }

    ⚪创建控制层组件EmployeeController 

    1. @Controller
    2. public class EmployeeController {
    3. @Autowired
    4. private EmployeeService employeeService;
    5. @RequestMapping(value = "/employee",method = RequestMethod.GET)
    6. public String getAllEmployee(Model model){
    7. //查询所有的员工信息
    8. List list = employeeService.getAllEmployee();
    9. //将员工信息在请求域中共享
    10. model.addAttribute("list",list);
    11. //跳转到employee_list.html
    12. return "employee_list";
    13. }
    14. }

    ⚪创建页面

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>员工列表title>
    6. <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
    7. head>
    8. <body>
    9. <table>
    10. <tr>
    11. <th colspan="6">员工列表th>
    12. tr>
    13. <tr>
    14. <th>流水号th>
    15. <th>员工姓名th>
    16. <th>年龄th>
    17. <th>性别th>
    18. <th>邮箱th>
    19. <th>操作th>
    20. tr>
    21. <tr th:each="employee,status : ${list}">
    22. <td th:text="${status.count}">td>
    23. <td th:text="${employee.empName}">td>
    24. <td th:text="${employee.age}">td>
    25. <td th:text="${employee.gender}">td>
    26. <td th:text="${employee.email}">td>
    27. <td>
    28. <a th:href="">删除a>
    29. <a th:href="">修改a>
    30. td>
    31. tr>
    32. table>
    33. body>
    34. html>

    结果:

    二、数据分页

    1. /**
    2. * 获取员工的分页信息
    3. * @param pageNum
    4. * @return
    5. */
    6. PageInfo getEmployeePage(Integer pageNum);
    1. @RequestMapping(value = "/employee/page/{pageNum}",method = RequestMethod.GET)
    2. public String getEmployeePage(@PathVariable("pageNum") Integer pageNum,Model model){
    3. //获取员工的分页信息
    4. PageInfo page = employeeService.getEmployeePage(pageNum);
    5. //将分页数据共享到请求域中
    6. model.addAttribute("page",page);
    7. //跳转到employee_list.html
    8. return "employee_list";
    9. }
    1. public PageInfo getEmployeePage(Integer pageNum) {
    2. //开启分页功能
    3. PageHelper.startPage(pageNum,4);
    4. //查询所有的员工信息
    5. List list = employeeMapper.getAllEmployee();
    6. //获取分页相关数据
    7. PageInfo page = new PageInfo(list,5);
    8. return page;
    9. }
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:color="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>员工列表title>
    6. <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
    7. head>
    8. <body>
    9. <table>
    10. <tr>
    11. <th colspan="6">员工列表th>
    12. tr>
    13. <tr>
    14. <th>流水号th>
    15. <th>员工姓名th>
    16. <th>年龄th>
    17. <th>性别th>
    18. <th>邮箱th>
    19. <th>操作th>
    20. tr>
    21. <tr th:each="employee,status : ${page.list}">
    22. <td th:text="${status.count}">td>
    23. <td th:text="${employee.empName}">td>
    24. <td th:text="${employee.age}">td>
    25. <td th:text="${employee.gender}">td>
    26. <td th:text="${employee.email}">td>
    27. <td>
    28. <a th:href="">删除a>
    29. <a th:href="">修改a>
    30. td>
    31. tr>
    32. table>
    33. <div style="text-align: center">
    34. <a th:if="${page.hasPreviousPage}" th:href="@{/employee/page/1}">首页a>
    35. <a th:if="${page.hasPreviousPage}" th:href="@{'/employee/page' + ${page.prePage}}">上一页a>
    36. <span th:each="num : ${page.navigatepageNums}">
    37. <a th:if="${page.pageNum == num} style="color: red;" th:href="@{'/employee/page' + ${num}}" th:text="'['+${num}+']'">a>
    38. <a th:if="${page.pageNum != num} th:href="@{'/employee/page' + ${num}}" th:text="${num}">下一页a>
    39. span>
    40. <a th:if="${page.hasNextPage}" th:href="@{'/employee/page' + ${page.nextPage}}">下一页a>
    41. <a th:if="${page.hasNextPage}" th:href="@{'/employee/page' + ${page.pages}}">末页a>
    42. div>
    43. body>
    44. html>

  • 相关阅读:
    【操作系统-线程池】Pthread 线程池的设计及实现
    算法竞赛入门【码蹄集进阶塔335题】(MT2286-2290)
    贝wa儿歌(安卓)
    http 403
    PHP redis hash
    猿辅导笔试
    第一章初识Maven与Maven安装配置——尚硅谷
    Vue3快速入门
    JavaPTA练习题 7-3 身体质量指数(BMI)测算
    2021 华数杯全国大学生数学建模竞赛C题-基于神经网络预测电动汽车目标客户销售策略(三)(附带赛题解析&获奖论文及MATLAB代码)
  • 原文地址:https://blog.csdn.net/m0_52982868/article/details/126427654