首页 > 其他分享 >学习笔记——SSM整合(思路、步骤)

学习笔记——SSM整合(思路、步骤)

时间:2023-01-22 22:44:40浏览次数:39  
标签:xml Spring 步骤 笔记 SSM springframework spring mybatis org

2023-01-22

一、SSM整合

1、Spring + SpringMVC

(1)容器管理对象,由DispatcherServlet管理

(2)Spring容器对象,由ContextLoaderListener管理

2、解决组件扫描的冲突问题

(1)SpringMVC只扫描Controller层

(2)Spring扫描排除Controller层

2、Spring+Mybatis

(1)关于数据源、事务管理的代码冲突问题

同意交给Spring管理

(2)Spring管理Mybatis核心对象

①SqlSessionFactory

②Mapper代理对象

二、SSM整合步骤

1、Spring + SpringMVC

(1)导入jar包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

(2)配置文件

①web.xml

a.注册CharacterEncodingFilter,解决请求乱码问题

b.注册HiddenHttpMethodFilter,支持PUT&DELETE提交【REST风格】

c.注册DispatcherServlet(前端控制器),管理springMVC容器对象

d.注册一个上下文参数(contextConfigLocation),设置spring.xml配置文件路径

e.注册ContextLoaderListener,管理spring容器对象(目的是为了加载spring的配置文件,帮我们初始化IOC容器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--    d.注册一个上下文参数(contextConfigLocation),设置spring.xml配置文件路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <!--    e.注册ContextLoaderListener,管理spring容器对象-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--    a.注册CharacterEncodingFilter,解决请求乱码问题-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<!--    b.注册HiddenHttpMethodFilter,支持PUT&DELETE提交【REST风格】-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<!--    c.注册DispatcherServlet(前端控制器),管理springMVC容器对象-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

②springMVC.xml

a.开启组件扫描(只扫描Controller)

b.装配视图解析器

c.装配视图控制器(view-controller)

d.装配"default-servlet-handler",解决静态资源加载问题

e.装配"annotation-driven",解决后续问题

解决view-controller问题、解决default-servlet-handler问题、解决Jackon配置消息转换器问题

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">

<!--    a.开启组件扫描(只扫描Controller)-->
    <context:component-scan base-package="com.hh" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--    b.装配视图解析器-->
    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="characterEncoding" value="UTF-8"></property>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <property name="characterEncoding" value="UTF-8"></property>
                        <property name="prefix" value="WEB-INF/pages/"></property>
                        <property name="suffix" value=".html"></property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
<!--    c.装配视图控制器(view-controller)-->
    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<!--    d.装配"default-servlet-handler",解决静态资源加载问题-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--    e.装配"annotation-driven",解决后续问题-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

③spring.xml

开启组件扫描(排除Controller层)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

<!--    开启组件扫描-->
    <context:component-scan base-package="com.hh">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

2、Spring+Mybatis

(1)导入jar包

①spring的jar包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.10</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.3.10</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.10</version>
        </dependency>

②mybatis的jar包

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>

③spring与mybatis整合jar包

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>

(2)配置文件

①spring.xml

a.开启组件扫描(排除Controller层)

b.加载外部属性文件

c.装配数据源(DruidDataSource)

d.装配事务管理器(DataSourceTransactionManager)

e.开启声明式事务管理注解支持

f.装配SqlSessionFactoryBean,管理SqlSessionFactory

g.装配MapperScannerConfigurer,管理Mapper代理对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <!--    a.开启组件扫描(排除Controller层)-->
    <context:component-scan base-package="com.hh">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--    b.加载外部属性文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--    c.装配数据源(DruidDataSource)-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driverClassName}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>
<!--    d.装配事务管理器(DataSourceTransactionManager)-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    e.开启声明式事务管理注解支持-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <!--    f.装配SqlSessionFactoryBean,管理SqlSessionFactory-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        设置数据源-->
        <property name="dataSource" ref="dataSource"></property>
<!--        设置mybatis-config.xml核心配置文件路径-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>

<!--        设置类型别名-->
        <property name="typeAliasesPackage" value="com.hh.pojo"></property> <!-- 设置映射文件路径--> <property name="mapperLocations" value="classpath:/mapper/*.xml"></property> </bean> <!-- g.装配MapperScannerConfigurer,管理Mapper代理对象--> <mybatis-spring:scan base-package="com.hh.mapper"></mybatis-spring:scan> </beans>

②springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">

<!--    a.开启组件扫描(只扫描Controller)-->
    <context:component-scan base-package="com.hh" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--    b.装配视图解析器-->
    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="characterEncoding" value="UTF-8"></property>
            <property name="templateEngine">
                <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                    <property name="templateResolver">
                        <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                            <property name="characterEncoding" value="UTF-8"></property>
                            <property name="prefix" value="WEB-INF/pages/"></property>
                            <property name="suffix" value=".html"></property>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
<!--    c.装配视图控制器(view-controller)-->
    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<!--    d.装配"default-servlet-handler",解决静态资源加载问题-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--    e.装配"annotation-driven",解决后续问题-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

③mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
<!--        开启自动驼峰式命名映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
<!--       开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
<!--        延迟加载的属性-->
        <setting name="aggressiveLazyLoading" value="false"/>
<!--        开启二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>

<!--    添加分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

</configuration>

 

标签:xml,Spring,步骤,笔记,SSM,springframework,spring,mybatis,org
From: https://www.cnblogs.com/isDaHua/p/17064498.html

相关文章

  • JavaScript学习笔记—数组的方法
    1.非破坏性方法(1)Array.isArray():用来检查一个对象是否是数组console.log(Array.isArray([1,2,3,4]));//trueconsole.log(Array.isArray({name:"孙悟空"}));//fals......
  • 编译snowboy 笔记
    1.安装依赖sudoapt-getinstallpulseaudiosoxpython3-pyaudioswiglibatlas-base-devpython3-dev2.获取源代码gitclonehttps://github.com/Kitt-AI/snowboy.git......
  • 《RPC实战与核心原理》学习笔记Day5
    06|RPC实战:剖析gRPC源码,动手实现一个完整的RPC我们通过动态代理技术,屏蔽RPC调用的细节,从而让使用者能够面向接口编程。什么是gRPC?gRPC是由Google开发并且开源的一款......
  • 在虚拟机中安装 Ubuntu 步骤
    在虚拟机中安装Ubuntu步骤安装前的准备和基本安装设置语言环境安装常用软件1.安装前的准备和基本安装1.1安装前的准备访问http://cn.ubuntu.com/download/下载Ubuntu......
  • refresh是什么?Spring refresh的12个步骤
    文章目录​​功能分类​​​​具体步骤​​​​1.prepareRefresh​​​​2.obtainFreshBeanFactory​​​​3.prepareBeanFactory​​​​4.postProcessBeanFactory​​......
  • AC 自动机学习笔记
    前置知识:KMP,trie。一.自动机这里的自动机都指有限状态自动机(DFA)。一个DFA可以理解为一张有向图,由有限的状态(点),字母表,转移函数(边),开始状态与终止状态(起点,终点)组成。AC......
  • 学习笔记——SpringMVC拦截器的两种装配方式;SpringMVC拦截器工作原理;SpringMVC中的异
    2023-01-22 一、SpringMVC拦截器的两种装配方式1、全局装配(放置在springmvc.xml中)<!--装配拦截器--><!--全局装配--><mvc:interceptors><ref......
  • 数据结构笔记(一)
    程序=数据结构+算法数据结构(C语言版)(第2版)https://weread.qq.com/web/reader/b57320b071db572cb578fb5数据结构与算法基础(青岛大学-王卓)https://www.bilibili.com/video......
  • Elasticsearch 核心技术与实战 学习笔记
    分片的设定对于生产环境中分片的设定,需要提前做好容量规划分片数设置过小导致后续无法增加节点实现水品扩展单个分片的数据量太大,导致数据重新分配耗时分片数设......
  • 数论笔记
    ·质数素数定理:设\(x\geq1\),以\(\pi(x)\)表示不超过\(x\)的素数的个数。当\(x\rightarrow\infty\)时,\(\pi(x)\to\dfrac{x}{\ln(x)}\)质数筛法1.埃式......