• SSM整合 Spring SprintMVC Mybatis


    目录

    1. Maven pom.xml

    2. 整合web.xml

    3. 整合SpringMVC

    4. 配置Spring

    5. 整合mybatis-config.xml


    1. Maven pom.xml

    spring四个核心包 spring-webmvc、spring-jdbc、spring-test、spring-aspects

    lombok

    jackson : POJO <-> JSON

    thymeleaf-spring5

    Mybatis 核心mybatis

    mybatis-spring

    Mybatis分页pagehelper

    连接池druid

    mysql驱动mysql-connector-java

    单元测试junit

    log4j 日志和slf4j-api

    logback日志ogback-classic

    servlet API(javax.servlet-api)在spring中前端控制器dispatchservlet继承的就是这个依赖里的httpservlet

    文件上传commons-fileupload

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

    2. 整合web.xml

    1. 编码控制器(请求响应编码)

    2. 前端控制器

    3. 监听器listener,配置Spring监听器:服务器启动的时候加载Spring的配置文件

    4. 声明spring配置文件位置

    5. HiddenHttpMethodFilter 设置处理请球方法put和delete

    5. HiddenHttpMethodFilter 设置处理请球方法put和delete
    
    
    
    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>encondingparam-name>
    11. <param-value>UTF-8param-value>
    12. init-param>
    13. <init-param>
    14. <param-name>forceResponseEncodingparam-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. <servlet>
    23. <servlet-name>DispatcherServletservlet-name>
    24. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    25. <init-param>
    26. <param-name>contextConfigLocationparam-name>
    27. <param-value>classpath:springMVC.xmlparam-value>
    28. init-param>
    29. <load-on-startup>1load-on-startup>
    30. servlet>
    31. <servlet-mapping>
    32. <servlet-name>DispatcherServletservlet-name>
    33. <url-pattern>/url-pattern>
    34. servlet-mapping>
    35. <listener>
    36. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    37. listener>
    38. <context-param>
    39. <param-name>contextConfigLocationparam-name>
    40. <param-value>classpath:spring.xmlparam-value>
    41. context-param>
    42. <filter>
    43. <filter-name>HiddenHttpMethodFilterfilter-name>
    44. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    45. filter>
    46. <filter-mapping>
    47. <filter-name>HiddenHttpMethodFilterfilter-name>
    48. <url-pattern>/*url-pattern>
    49. filter-mapping>
    50. web-app>

    3. 整合SpringMVC

    1. 扫描组件context:component-scan

    2. thymeleaf

    3. mvc:view-controller视图控制器

    4. mvc:default-servlet-handler处理静态资源,加上静态资源static后要重新在maven中打包

    5. mvc:annotation-driven开启mvc注解驱动

    6. multipartResolver 必须通过文件解析器的解析才能将通过表单上传的文件转换为MultipartFile对象

    7. 拦截器

    8. 异常处理器

    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 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. <context:component-scan base-package="com.zt">context:component-scan>
    8. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    9. <property name="order" value="1"/>
    10. <property name="characterEncoding" value="UTF-8"/>
    11. <property name="templateEngine">
    12. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    13. <property name="templateResolver">
    14. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    15. <property name="prefix" value="/WEB-INF/templates/"/>
    16. <property name="suffix" value=".html"/>
    17. <property name="templateMode" value="HTML5"/>
    18. <property name="characterEncoding" value="UTF-8" />
    19. bean>
    20. property>
    21. bean>
    22. property>
    23. bean>
    24. <mvc:default-servlet-handler>mvc:default-servlet-handler>
    25. <mvc:annotation-driven>mvc:annotation-driven>
    26. <mvc:view-controller path="/" view-name="index"/>
    27. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
    28. beans>

    4. 配置Spring

    1. 扫描组件,除控制层,(将出去控制层以外的所有组件,都交给sping.xml管理,控制层是springmvc管理)

    2. 引入数据源jdbc.properties

             

    jdbc.url=jdbc:mysql://localhost:3306/ibatis?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
    jdbc.user=root
    jdbc.password=761311
    jdbc.driver=com.mysql.cj.jdbc.Driver

    3. 配置数据源(这一部分也可以在mybatis-config中配置)

    4. 配置、开启事务管理器

    使用#Transctional注解标识的所有方法和类 进行事务管理

    5. 将SqlSessionFactoryBean配置到IOC容器中,就可以在容器中获取SqlSessionFactoryBean的对象。

    6. 获取mybatis-config.xml配置(这一部分在spring和mybatis-config中都可以配置)

    配置上面的数据源、设置typeAliasesPackage 实体类包中的别名

    7. 分页插件(这一部分也可以在mybatis-config中配置)

    8. 引入mapper文件,mapper扫描(在mybatis-config中就不用引入映射文件了)

    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:util="http://www.springframework.org/schema/util"
    5. xmlns:context="http://www.springframework.org/schema/context" 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.0.xsd
    8. http://www.springframework.org/schema/util
    9. https://www.springframework.org/schema/util/spring-util.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    12. <context:component-scan base-package="com.zt.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="transctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    17. <property name="dataSource" ref="dataSource">property>
    18. bean>
    19. <tx:annotation-driven/>
    20. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    21. <property name="driverClassName" value="${jdbc.driver}">property>
    22. <property name="url" value="${jdbc.url}">property>
    23. <property name="username" value="${jdbc.user}">property>
    24. <property name="password" value="${jdbc.password}">property>
    25. <property name="initialSize" value="10">property>
    26. <property name="maxActive" value="16">property>
    27. bean>
    28. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    29. <property name="configLocation" value="classpath:mybatis-config.xml">property>
    30. <property name="dataSource" ref="dataSource">property>
    31. <property name="typeAliasesPackage" value="com.zt.ssm.pojo">property>
    32. bean>
    33. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    34. <property name="basePackage" value="com.zt.ssm.mapper">property>
    35. bean>
    36. beans>

    5. 整合mybatis-config.xml

    1. 引入jdbc.properties

    2. setting-mapUnderscoreToCamelCase将下划线映射为驼峰 解决数据库和属性名不一致问题

    3. setting-lazyLoadingEnabled/aggressiveLazyLoading延迟加载 按需加载

    4. typeAliases设置当前实体类的别名(spring可以设置)

    5. 配置连接数据库环境

    6.  引入 MyBatis 的映射文件 : 存放SQL语句 和 POJO的映射方式

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//com.zt.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. <setting name="lazyLoadingEnabled" value="true"/>
    9. <setting name="aggressiveLazyLoading" value="false"/>
    10. settings>
    11. <plugins>
    12. <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
    13. plugins>
    14. configuration>

  • 相关阅读:
    阿里云服务器公网带宽如何修改?
    金九银十,读完这些“Java技术栈”,拿下阿里Offer没问题
    OpenGL ES Windows 搭建环境(MFC版本)
    PBR物理材质
    Cholesterol-PEG-Acid,胆固醇-聚乙二醇-羧基保持在干燥、低温环境下
    多中机器学习模型对比可视化
    Springboot整合使用JPA进行增删改查
    Android 腾讯位置服务地图简单使用
    VBA数组动态下标下界
    【Unity Material】02 - Material使用技巧
  • 原文地址:https://blog.csdn.net/qq_41950447/article/details/128033971